QScrollArea 不能按预期与 QWidget 和 QVBoxLayout 一起工作

QScrollArea not working as expected with QWidget and QVBoxLayout

所以我有这个 QFrame,它是父窗口小部件(在代码中由 this 表示)。在这个小部件中,我想将 QWidget 放置在距离顶部 10 像素的位置(距离底部 10 像素,因此它的高度为 140 像素,而父级为 160 像素)。 QWidget 将在垂直布局中有许多自定义按钮,在滚动区域中,以便当按钮的组合高度超过 QWidget's 高度(140px)时,自动滚动设置.因为滚动不是针对整个父widget,而是针对某个子widget,所以这里的滚动应该只适用于子widget。这是我的代码:

//this is a custom button class with predefined height and some formatting styles
class MyButton: public QPushButton
{

public:
    MyButton(std::string aText, QWidget *aParent);

};

MyButton::MyButton(std::string aText, QWidget *aParent): QPushButton(QString::fromStdString(aText), aParent)
{
    this->setFixedHeight(30);
    this->setCursor(Qt::PointingHandCursor);
    this->setCheckable(false);
    this->setStyleSheet("background: rgb(74,89,98);   color: black; border-radius: 0px; text-align: left; padding-left: 5px; border-bottom: 1px solid black;");
}

//this is where I position the parent widget first, and then add sub widget
this->setGeometry(x,y,width,160);
this->setStyleSheet("border-radius: 5px; background:red;");

//this is the widget which is supposed to be scrollable
QWidget *dd = new QWidget(this);
dd->setGeometry(0,10,width,140);
dd->setStyleSheet("background: blue;");

QVBoxLayout *layout = new QVBoxLayout();
dd->setLayout(layout);

for (int i = 0; i < fValues.size(); i++)
{
    MyButton *button = new MyButton(fValues[i],dd);
    layout->addWidget(button);
}

QScrollArea *scroll = new QScrollArea(this);
scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scroll->setWidget(dd);

出乎我的意料,这就是我得到的(附图)。我做错了什么,我该如何解决?

你弄乱了一堆物品。具有可滚动区域的想法是这样的:

  • 底部是父控件(例如QDialog
  • 在此之上是固定大小的可滚动区域 (QScrollArea)
  • 在此之上是一个一定大小的小部件 (QWidget),通常只有一部分可见(它应该比滚动区域大)
  • 在此之上是布局
  • 最后一个:布局管理子项(这里有几个 QPushButton

试试这个代码:

int
main( int _argc, char** _argv )
{
    QApplication app( _argc, _argv );

    QDialog * dlg = new QDialog();
    dlg->setGeometry( 100, 100, 260, 260);

    QScrollArea *scrollArea = new QScrollArea( dlg );
    scrollArea->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
    scrollArea->setWidgetResizable( true );
    scrollArea->setGeometry( 10, 10, 200, 200 );

    QWidget *widget = new QWidget();
    scrollArea->setWidget( widget );

    QVBoxLayout *layout = new QVBoxLayout();
    widget->setLayout( layout );

    for (int i = 0; i < 10; i++)
    {
        QPushButton *button = new QPushButton( QString( "%1" ).arg( i ) );
        layout->addWidget( button );
    }

    dlg->show();

    return app.exec();
}

值得一提的是 QScrollArea::setWidgetResizable,它会根据内容动态调整子窗口小部件的大小。

结果如下所示: