思想:在一个窗口类中声明另一继承与Qdialog的类的变量 还有在另一类中parentwidget()函数获取父类窗口,然后将其隐藏。。
窗口1:
mywin1.h
#ifndef MYWIN1_H #define MYWIN1_H #include <QMainWindow> #include"mywin2.h" namespace Ui { class myWin1; } class myWin1 : public QMainWindow { Q_OBJECT public: explicit myWin1(QWidget *parent = 0); ~myWin1(); private slots: void on_pushButton_2_clicked(); private: Ui::myWin1 *ui; myWin2 *mywind; }; #endif // MYWIN1_H
mywin1.cpp
#include "mywin1.h" #include "ui_mywin1.h" #include<mywin2.h> myWin1::myWin1(QWidget *parent) : QMainWindow(parent), ui(new Ui::myWin1) { ui->setupUi(this); } myWin1::~myWin1() { delete ui; } void myWin1::on_pushButton_2_clicked() { mywind = new myWin2(); mywind->show(); this->hide(); }
mywin2.h
#ifndef MYWIN2_H #define MYWIN2_H #include <QMainWindow> #include"mywin1.h" namespace Ui { class myWin2; } class myWin2 : public QMainWindow { Q_OBJECT public: explicit myWin2(QWidget *parent = 0); ~myWin2(); private slots: void on_pushButton_clicked(); private: Ui::myWin2 *ui; myWin1 *mywind1; }; #endif // MYWIN2_H
mywin2.cpp
#include "mywin2.h" #include "ui_mywin2.h" #include"mywin1.h" myWin2::myWin2(QWidget *parent) : QMainWindow(parent), ui(new Ui::myWin2) { ui->setupUi(this); } myWin2::~myWin2() { delete ui; } void myWin2::on_pushButton_clicked() { mywind1 = new myWin1(); mywind1->parentWidget()->show(); delete this; }