Qt: How to set max QVBoxlayout width

I currently have a horizontal layout in which there are two vertical layouts. Vlayout1 and VLayout2 . Now I want to set the maximum width limit for Vlayout1 so that if the form expands after that, only Vlayout1 .
Any suggestions on how I could do this?

+6
source share
2 answers

You can β€œhack” and place your layout inside the widget

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

Works:)

+6
source

You cannot set a maximum size for QVBoxLayout . You will probably need to set the maximum size in the widgets that the layout contains. If you want one of the layouts to stretch and the other 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 will see a layout containing the stretch Button2 , while a layout containing Button1 will remain the same size.

0
source

Source: https://habr.com/ru/post/958911/


All Articles