C++ Qpushbutton未显示

C++ Qpushbutton未显示,c++,qt,C++,Qt,我有下面的代码显示一个按钮,我是新来的qt,不明白为什么它不工作 mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include<QtWidgets> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { QWidget* wdg = new QWidget(this);

我有下面的代码显示一个按钮,我是新来的qt,不明白为什么它不工作

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QtWidgets>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
   QWidget* wdg = new QWidget(this);
   QPushButton *button = new QPushButton(wdg);
   button->setText(tr("something"));
   setCentralWidget(wdg);//line 1
   ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}
#包括“mainwindow.h”
#包括“ui_main window.h”
#包括
主窗口::主窗口(QWidget*父窗口):
QMainWindow(父级),
用户界面(新用户界面::主窗口)
{
QWidget*wdg=新的QWidget(本);
QPushButton*按钮=新的QPushButton(wdg);
按钮->设置文本(tr(“某物”);
setCentralWidget(wdg);//第1行
用户界面->设置用户界面(此);
}
MainWindow::~MainWindow()
{
删除用户界面;
}
如果我删除第1行,则会显示该按钮,但它不可单击。

ui->setupUi(此),则初始化存储在
.ui
文件中的Qt designer中创建的用户界面。它构造小部件并在内部调用
setCentralWidget
。因此,当它被调用时,中心小部件将被您设置的小部件替换

或者自己制作用户界面,忽略
ui->setupUi(此)或第一次调用
ui->setupUi(此)并根据自己的意愿进行其余初始化