我的目的是创建一个可滚动的控件,里面有一个 QVBoxLayout,上面有各种控件(比如按钮)。该控件放在 *.ui 表单上。在该控件的构造函数中,我编写了以下代码:
MyScrollArea::MyScrollArea(QWidget *parent) :
QScrollArea(parent)
{
// create the scrollable container
this->container = new QWidget(); // container widget member
this->container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
this->container->setContentsMargins(QMargins(0,0,0,0));
this->content = new QVBoxLayout(); // layout member
this->content->setMargin(0);
this->content->setSpacing(0);
for (int i=0; i<100; i++)
{
QPushButton * widget = new QPushButton();
widget->setText("button");
widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
this->content->addWidget(widget);
}
this->container->setLayout(this->content);
this->content->layout();
this->setWidget(this->container);
}
我的问题:按钮的大小固定,不能水平扩展。他们有一个固定的大小。我希望它们水平扩展以填充它们所在的行。如何让它们在其父容器中水平扩展?