简体   繁体   English

根据内容动态调整qwidget和qlayout的大小

[英]Dynamically resizing a qwidget and qlayout based on contents

I am working on my first project in QT and have a question about dynamically sizing widgets and layouts. 我正在研究QT中的第一个项目,并且有一个关于动态调整小部件和布局大小的问题。

I am developing a qwidget that will hold a number of widgets based on a parameter given to the qwidget's constructor. 我正在开发一个qwidget,它将根据给qwidget的构造函数的参数保存许多小部件。 I am having difficulty resizing my widget as well as the layout and groupbox to fit the number of widgets passed by the constructor. 我无法调整窗口小部件以及布局和组框的大小以适应构造函数传递的窗口小部件的数量。

My qwidget currently contains a groupbox and a layout inside that groupbox. 我的qwidget目前包含一个组框和该组框内的布局。 On construction of the widget I add widgets to the layout and then try to resize the layout using the functions setSizeConstraint(),adjustSize(), and resize(). 在构建窗口小部件时,我向布局添加窗口小部件,然后尝试使用函数setSizeConstraint(),adjustSize()和resize()调整布局大小。 None of this seems to resize the widget properly however as my widgets are still overlapping each other. 然而,由于我的小部件仍然相互重叠,所以这似乎都没有正确调整小部件的大小。

My Ui is essentially this 我的Ui基本上就是这个

Qwidget QWidget的

->qGroupBox - > qGroupBox

-->qLayout - > qLayout

--->qwidget1 ---> qwidget1

--->qwidget2 ---> qwidget2

--->qwidget3 ---> qwidget3

etc. below is the constructor 以下是构造函数

    myWidget::myWidget(int numRows) :
        myUiPtr(new UiLLmyWidget)
{
    //Add widget rows
    myUiPtr->setupUi(this);
    for(int i=0; i<numRows; i++)
    {
        myRowWidget *row = new myRowWidget();
        myUiPtr->myLayout->addWidget(row);
    }

    //Attempt to resize widget (doesn't work)
    myUiPtr->myLayout->setSizeConstraint(QLayout::SetMinimumSize);
    myUiPtr->myGroupbox->adjustSize();
    this->adjustSize();
}

I would expect the widget and all of it's child groupings to resize with the number of widgets I create in the layout but this does not happen and instead I get the widgets overlapping each other. 我希望小部件和它的所有子组都可以根据我在布局中创建的小部件的数量来调整大小,但这不会发生,而是让小部件相互重叠。

you could try the following: 你可以尝试以下方法:

QGridLayout* layout = new QGridLayout();
layout->setAlignment(Qt::AlignTop);
myUiPtr->groupbox->setLayout(layout);

...
layout->addWidget(row, layout->count(), 0);

You could use a QScrollArea instead of (or inside) the Groupbox, so a small Window Size does cause the overlapping. 您可以使用QScrollArea而不是(或在Groupbox中),因此窗口大小会导致重叠。

Edit: sorry, i didnt consider the questions part of "resizing the widget". 编辑:对不起,我没有考虑“调整窗口小部件”的问题部分。

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

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