1
votes

Hi guys I have to dynamically create push buttons depending on user inputs, therefore if user gives a large input number the widget containing the push buttons has to have the ability to scroll up and down. For this reason I am using QScrollArea. I generate the template in Qt designer and the UIC generates the code for me after which I add in my part which should handle dynamic creation of push buttons. However, I can not seem to get the vertical scroll bars to appear. Here is the relevant part of the code.

    verticalWidget = new QWidget(FWHMWorkflowDialog);
    verticalWidget->setObjectName(QString::fromUtf8("verticalWidget"));
    verticalWidget->setMinimumSize(QSize(150, 0));
    verticalWidget->setMaximumSize(QSize(150, 16777215));
    verticalLayout_5 = new QVBoxLayout(verticalWidget);
    verticalLayout_5->setObjectName(QString::fromUtf8("verticalLayout_5"));
    scrollArea = new QScrollArea(verticalWidget);
    scrollArea->setObjectName(QString::fromUtf8("scrollArea"));
    scrollArea->setMaximumSize(QSize(150, 16777215));
    scrollArea->setWidgetResizable(true);
    scrollAreaWidgetContents = new QWidget();
    scrollAreaWidgetContents->setObjectName(QString::fromUtf8("scrollAreaWidgetContents"));
    scrollAreaWidgetContents->setGeometry(QRect(0, 0, 130, 432));

    numberOfSlices = numberSlices;
    for (int i = 0; i < numberOfSlices; i++)
    {
        QWidget *horizontalWidget = new QWidget(scrollAreaWidgetContents);
        horizontalWidget->setMaximumSize(150,40);
        horizontalWidget->setGeometry(QRect(0, i*40, 150, 40));
        hWidgetList.push_back(horizontalWidget);

        QHBoxLayout *hLayout = new QHBoxLayout(horizontalWidget);
        hLayoutList.push_back(hLayout);
        hLayout->setSizeConstraint(QLayout::SetMinimumSize);
        hLayout->setContentsMargins(-1, 1, -1, 1);

        QPushButton *pushButton = new QPushButton(horizontalWidget);
        pushButtonList.push_back(pushButton);
        QString temp = QString("m_sliceButton").arg(i);
        pushButtonList[i]->setObjectName(temp);
        pushButtonList[i]->setGeometry(QRect(10, 20+i*40, 98, 27));
        hLayout->addWidget(pushButton);

        QCheckBox *checkBox = new QCheckBox(horizontalWidget);
        checkBoxList.push_back(checkBox);
        temp =  QString("m_checkBox").arg(i);
        checkBoxList[i]->setObjectName(temp);
        checkBoxList[i]->setEnabled(true);
        checkBoxList[i]->setGeometry(QRect(110, 20+i*40, 21, 22));

        hLayout->addWidget(checkBox);

    }

    scrollArea->setWidget(scrollAreaWidgetContents);
    //scrollArea->setWidgetResizable(true);

    verticalLayout_5->addWidget(scrollArea);

The output window always looks like the following.

enter image description here

In this example the input by the user is 25 however you can see that the 21st button is cut off and 4 other buttons are not visible.

The size window problem occurring after scroll functionality started working.

enter image description here

2
What are hWidgetList and checkBoxList?Phlucious
Also, it looks a lot like you're editing the qmake-generated ui_windowform.h file, which is strongly discouraged.Phlucious
@Phlucious they are vectors that hold pointers to the horizontal widgets and checkboxes. Also why is it discouraged to edit the ui.h file?user1084113
It'll overwrite your changes every time you rebuild the .ui file. If you expand the comment at the top of the ui.h file, it'll say something along the lines of... "/******************************************************************************** ** Form generated from reading UI file 'mainwindow.ui' ** ** Created: Thu Jan 31 10:20:44 2013 ** by: Qt User Interface Compiler version 4.8.2 ** ** WARNING! All changes made in this file will be lost when recompiling UI file! ********************************************************************************/"Phlucious

2 Answers

4
votes

You need to add your horizontalWidget to a vertical widget like so:

QVBoxLayout* vLayout = new QVBoxLayout();

for (int i = 0; i < numberOfSlices; i++)
{
    QWidget *horizontalWidget = new QWidget();
    vLayout->addWidget(horizontalWidget);
    ....
}
scrollAreaWidgetContents->setLayout(vLayout);

You second problem looks like it comes from this line:

scrollArea = new QScrollArea(verticalWidget);

You're adding scrollArea directly to verticalWidget, but to get it to lay out the way you want you need to put it in a layout. Try the following instead:

QVBoxLayout* l = new QVBoxLayout();
l->addWidget(sliceLabel); // or whatever you call it
l->addWidget(scrollArea);
l->addWidget(clearButton); // again, your name here
verticalWidget->setLayout(l);
1
votes

Try playing around with the QScrollBarPolicy.

http://doc.qt.digia.com/qt/qabstractscrollarea.html#horizontalScrollBarPolicy-prop

I'm guessing that the default behavior isn't working because there is something strange going on with layouts.