Qt滚动区不会添加滚动条 [英] Qt Scroll Area does not add in scroll bars

查看:233
本文介绍了Qt滚动区不会添加滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须动态创建按钮,取决于用户输入,因此,如果用户给出一个大的输入数字,包含按钮的小部件必须能够向上和向下滚动。为此,我使用QScrollArea。我在Qt设计器中生成模板,UIC为我生成代码,之后我添加在我的部分,应该处理动态创建按钮。但是,我似乎不能得到垂直滚动条出现。下面是代码的相关部分。

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.

>

在此示例中,用户的输入为25,但您可以看到第21个按钮被切断,其他4个按钮不可见。

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.

推荐答案

您需要将horizo​​ntalWidget添加到垂直窗口小部件, p>

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);

您正在将scrollArea直接添加到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);

这篇关于Qt滚动区不会添加滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆