在MainWindow的构造函数中添加如下代码:
//设置Ui
ui.setupUi(this);
//使Ui可自适应父窗口大小
QVBoxLayout* mainLayout = new QVBoxLayout;
mainLayout->addWidget(ui.verticalLayoutWidget);
setLayout(mainLayout);
或
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QVBoxLayout* mainLayout = new QVBoxLayout;
mainLayout->addWidget(ui->verticalLayoutWidget);
ui->centralWidget->setLayout(mainLayout);
}