1

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

user1084113
  • 932
  • 3
  • 15
  • 31
  • What are `hWidgetList` and `checkBoxList`? – Phlucious Jan 31 '13 at 17:52
  • Also, it looks a lot like you're editing the qmake-generated ui_windowform.h file, which is strongly discouraged. – Phlucious Jan 31 '13 at 18:00
  • @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 Jan 31 '13 at 18:18
  • 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 Jan 31 '13 at 18:22

2 Answers2

4

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);
Bill
  • 14,257
  • 4
  • 43
  • 55
  • this worked thanks! I also had to remove the line "verticalLayout_5->addWidget(scrollArea);" – user1084113 Jan 31 '13 at 18:47
  • after making the fix, the widget that displays the slices no longer occupies the entire length of the maximized window, I've tried to set Geometry of main vertical widget however this does not change anything. Attached in the main post is a screenshot. – user1084113 Jan 31 '13 at 19:45
  • Thank you very much I had my verticalWidget under a grid layout so I simply made scrollArea a child of my main dialog window instead of vertical widget and then applied the positioning(layout) of verticalWidget to scrollArea instead. – user1084113 Jan 31 '13 at 22:27
1

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.

MrFox
  • 1,244
  • 1
  • 11
  • 24
  • +1 - Talking about layouts helped me realize that the OP needed another one for the QScollArea to do its thing. – Bill Jan 31 '13 at 18:59