0
votes

I am working on my first project in QT and have a question about dynamically sizing widgets and layouts.

I am developing a qwidget that will hold a number of widgets based on a parameter given to the qwidget's constructor. 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. 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(). None of this seems to resize the widget properly however as my widgets are still overlapping each other.

My Ui is essentially this

Qwidget

->qGroupBox

-->qLayout

--->qwidget1

--->qwidget2

--->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.

1

1 Answers

0
votes

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.

Edit: sorry, i didnt consider the questions part of "resizing the widget".