QT学习笔记----窗口的相互切换
1.ui文件中布局2.主要程序
main.cpp
[cpp]
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
[cpp]
</pre><pre class="cpp" name="code">#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
this->hide();
dialog1.show();
dialog1.exec();
this->show();
}
void MainWindow::on_pushButton_2_clicked()
{
this->close();
}
void MainWindow::on_pushButton_3_clicked()
{
this->close();
dialog2.show();
dialog2.exec();
this->show();
}
</pre><pre class="cpp" name="code">#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
this->hide();
dialog1.show();
dialog1.exec();
this->show();
}
void MainWindow::on_pushButton_2_clicked()
{
this->close();
}
void MainWindow::on_pushButton_3_clicked()
{
this->close();
dialog2.show();
dialog2.exec();
this->show();
}
dialog1.cpp
[cpp]
#include "dialog1.h"
#include "ui_dialog1.h"
Dialog1::Dialog1(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog1)
{
ui->setupUi(this);
}
Dialog1::~Dialog1()
{
delete ui;
}
void Dialog1::on_pushButton_clicked()
{
this->close();
}
#include "dialog1.h"
#include "ui_dialog1.h"
Dialog1::Dialog1(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog1)
{
ui->setupUi(this);
}
Dialog1::~Dialog1()
{
delete ui;
}
void Dialog1::on_pushButton_clicked()
{
this->close();
}
dialog2.cpp
与dialog1.cpp类似
mainwindow.h
[cpp]
ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "dialog1.h"
#include "dialog2.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
private:
Ui::MainWindow *ui;
Dialog1 dialog1;
Dialog2 dialog2;
};
#endif // MAINWINDOW_H
ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "dialog1.h"
#include "dialog2.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
private:
Ui::MainWindow *ui;
Dialog1 dialog1;
Dialog2 dialog2;
};
#endif // MAINWINDOW_H
关键点:
[cpp]
this->close();
dialog2.show();
dialog2.exec();
this->show();
this->close();
dialog2.show();
dialog2.exec();
this->show();
通过等待dialog.exec()消息,判断主界面是否回显。
运行结果:
摘自 奔跑的蜗牛(袁方的技术博客)
补充:软件开发 , 其他 ,