简体   繁体   English

Qt:如何设置 QVBoxlayout 的最大宽度

[英]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 . Vlayout1VLayout2 Now I want to set a maximum width limit of VLayout1 so that if the form is expanded after that, only Vlayout1 expands.现在我想设置VLayout1的最大宽度限制,以便在此之后展开表单时,只有Vlayout1展开。
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:您可以进行“hack”并将您的布局放置在小部件中,您可以为其定义最大宽度:

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 .您不能设置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.如果您现在调整窗口大小,您将看到包含Button2拉伸的布局,而包含Button1的布局保持相同大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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