2012-05-30 104 views
0

我試圖強制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

設置使用setGeometry()QScrollArea的大小,然後使用area.setSizePolicy(QSizePolicy::Fixed)