无法让Scroll / Layout工作

时间:2012-05-30 16:51:39

标签: qt

我正在尝试强制容器大小为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();
}

1 个答案:

答案 0 :(得分:1)

使用QScrollArea设置setGeometry()的尺寸,然后使用area.setSizePolicy(QSizePolicy::Fixed)