解决 QT 在构造函数中写的控件不显示的问题

保留现场

在新窗口中的构造函数中添加控件运行后却没有显示

探究原因

  • 新建的工程师 MainWindow 子类工程,没有设置父窗口。

  • 没有将控件的父窗口设置成自己定义的 widget。

#include<QMainWindow>
 
QMainWindow::QMainWindow(QMainWindow*parent) :
    QMainWindow(parent),
    ui(new Ui::QMainWindow)
{
     ui->setupUi(this);
     QPushButton* button_1 = new QPushButton("add");
     QPushButton* button_1 = new QPushButton("del");
}

解决方法

方法 1:给按钮控件设置父窗口:QWidget,并且把按钮添加到父窗口中。

#include<QMainWindow>
#include<QPushButton>
#include<QHBoxLayout>
 
QMainWindow::QMainWindow(QMainWindow*parent) :
    QMainWindow(parent),
    ui(new Ui::QMainWindow)
{
     ui->setupUi(this);
     QWidget* w = new QWidget();
     this->setCentralWidget(w);
     QHBoxLayout* hLayout = new QHBoxLayout();
     QPushButton* button_1 = new QPushButton("add");
     QPushButton* button_1 = new QPushButton("del");
     hLayout->addWidget(button_1);
     hLayout->addWidget(button_2);
     w->setLayout(hLayout);
}

方法 2:手动指定父窗口

#include<QMainWindow>
#include<QPushButton>
#include<QHBoxLayout>
 
QMainWindow::QMainWindow(QMainWindow*parent) :
    QMainWindow(parent),
    ui(new Ui::QMainWindow)
{
     ui->setupUi(this);
    
    
     QPushButton* button_1 = new QPushButton("add");
     QPushButton* button_1 = new QPushButton("del");
     button_1->setParent(this);
     button_2->setParent(this);
     button_2->move(300,100);
    
}