#include <QScrollArea> #include <QPushButton> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QScrollArea * scrollArea = new QScrollArea(this); QWidget * pWgt = new QWidget; QVBoxLayout *pLayout = new QVBoxLayout();//網格佈局 for(int i = 0; i < 100; i++) { QPushButton *pBtn = new QPushButton(); pBtn->setText(QString("按鈕%1").arg(i)); pBtn->setMinimumSize(QSize(60,30)); //width height pLayout->addWidget(pBtn);//把按鈕添加到佈局控件中 } pWgt->setLayout(pLayout);
//這一句setWidget必須放在pWgt裏面的內容都準備完畢以後,不然顯示有問題
scrollArea->setWidget(pWgt);
setCentralWidget(scrollArea);佈局
}