简体   繁体   中英

Qt: How to set the maximum width of a QVBoxlayout

I currently have a horziontal layout that has two vertical layouts in it. Vlayout1 and VLayout2 . Now I want to set a maximum width limit of VLayout1 so that if the form is expanded after that, only Vlayout1 expands.
Any suggestions on how I could accomplish this?

You can do a "hack" and place your layout inside a widget, for which you can define a maximum width:

QWidget *controlsRestrictorWidget = new QWidget();
QVBoxLayout *layoutVControls = new QVBoxLayout();
controlsRestrictorWidget->setLayout(layoutVControls);
controlsRestrictorWidget->setMaximumWidth(350);

It works :)

You can't set the maximum size of a QVBoxLayout . You'll probably need to set the maximum size on the widgets the layout contains. If you want one of the layout to stretch while the other one stays the same size you can try the following in your mainwindow constructor:

   QPushButton* btn1 = new QPushButton("Button1");
   QPushButton* btn2 = new QPushButton("Button2");
   QHBoxLayout* hLayout = new QHBoxLayout;
   QVBoxLayout* vLayout1 = new QVBoxLayout;
   QVBoxLayout* vLayout2 = new QVBoxLayout;

   hLayout->addLayout(vLayout1, 1);
   hLayout->addLayout(vLayout2, 0);
   vLayout1->addWidget(btn1);
   vLayout2->addWidget(btn2);

   QWidget* placeholder = new QWidget;
   placeholder->setLayout(hLayout);
   setCentralWidget(placeholder);

If you resize the window now, you'll see the layout that contains Button2 stretching whereas the layout containing Button1 stays the same size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM