一 QDialog窗口介绍
QDialog窗口和Widget窗口类似,都是以桌面的方式进行显示窗口的,只是有些许控件不同,请看详细代码。
头文件MyQDialog.h
#ifndef MYQDIALOG_H
#define MYQDIALOG_H
#include<QDialog>
class MyQDialog : public QDialog
{
Q_OBJECT
public:
explicit MyQDialog(QWidget *parent=0);
void paintEvent(QPaintEvent *);
signals:
public slots:
void slotOpenNewWindow();
};
#endif // MYQDIALOG_H
源文件MyQDialog.cpp
#include "myqdialog.h"
#include<QApplication>
#include<QPushButton>
#include<QDebug>
#include<QFileDialog> //文件选择对话框
#include<QFileInfo>
#include<QColorDialog> //颜色选择对话框
#include<QFontDialog> //字体选择对话框
#include<QColor>
#include<QLabel>
#include<QMessageBox> //提示框
#include<QPainter>
MyQDialog::MyQDialog(QWidget *parent) :
QDialog(parent)
{
QPushButton *button = new QPushButton("Click me", this);
connect(button, SIGNAL(clicked()), this, SLOT(slotOpenNewWindow()));
}
void MyQDialog::slotOpenNewWindow()
{
#if 0
QDialog *dlg = new QDialog();
QPushButton *button1 = new QPushButton("New Button", dlg);
connect(button1, SIGNAL(clicked()), dlg, SLOT(accept())); //关闭窗口
int num = dlg->exec(); //此时打开一个新窗口,且旧窗口不能被操作,因为之处出于死循环之中,exec是有返回值的
//如果Dialog是通过exec来显示的,那么可以通过accept或者reject来关闭窗口,比QWidget多了一个exec函数来显示窗口
//如果Dialog是通过show来显示的,那么可以通过close来关闭窗口,这个和QWidget一样的
// dlg->show(); //打开一个新窗口,新旧窗口之间相互独立
if(num == QDialog::Accepted)
{
qDebug()<<"Accecpted";
}