由于Qt基础知识没有打牢,遇到问题想了半天。
情景:在mainwindow中,new一个QWidget并传入this,结果QWidget上的控件直接复制到mainwindow上
当初写代码的时候,预期得到的结果是点击mainwindow上的pushbutton,QWidget才会弹出来,搜索了一下才找到原因。
解决方式:
- setWindowFlags(Qt::Window)
- 用QDialog
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QCheckBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWidget *wgt = new QWidget(this);
// wgt->setWindowFlags(Qt::Window);
QCheckBox *box = new QCheckBox(wgt);
connect(ui->pushButton, &QPushButton::clicked, [wgt](){
wgt->show();
});
}
MainWindow::~MainWindow()
{
delete ui;
}