我试图强制容器大小为 200x100,同时添加分布在大于 200x100 区域的子控件。当我超过视口的大小时,我希望有一个滚动条,但整个容器窗口会被调整大小。
如何强制容器大小为 200x100 并且仍然获得允许我向下滚动并查看其余内容的滚动条?
#include <QApplication>
#include <QPushButton>
#include <QScrollArea>
#include <QGridLayout>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget container;
container.setGeometry(QRect(600,300,200,100));
QGridLayout layout(&container);
QPushButton *aa = new QPushButton(QString("a"));
aa->setGeometry(QRect(10,10,100,30));
layout.addWidget(aa, 0, 0);
QPushButton *b = new QPushButton(QString("b"));
b->setGeometry(QRect(10,40,100,30));
layout.addWidget(b, 1, 0);
QPushButton *c = new QPushButton(QString("c"));
c->setGeometry(QRect(10,70,100,30));
layout.addWidget(c, 2, 0);
QPushButton *d = new QPushButton(QString("d"));
d->setGeometry(QRect(10,100,100,30));
layout.addWidget(d, 3, 0);
QPushButton *e = new QPushButton(QString("e"));
e->setGeometry(QRect(10,130,100,30));
layout.addWidget(e, 4, 0);
QPushButton *f = new QPushButton(QString("f"));
f->setGeometry(QRect(10,160,100,30));
layout.addWidget(f, 5, 0);
QPushButton *g = new QPushButton(QString("g"));
g->setGeometry(QRect(10,190,100,30));
layout.addWidget(g, 6, 0);
QPushButton *h = new QPushButton(QString("h"));
h->setGeometry(QRect(10,220,100,30));
layout.addWidget(h, 7, 0);
QScrollArea area;
area.setLayout(&layout);
area.setWidget(&container);
area.show();
return a.exec();
}