Qt中常用的对话框总结QDialog

来源:互联网 发布:基于java博客 编辑:程序博客网 时间:2024/04/30 09:46

一般对话框和QWidget看起来差别不大,不过还是有不同点的;

1.模态对话框,在模态对话框中,exec有自己的消息循环;并且把app的消息循环接管了;也就是当显示该对话框的时候不能去点击其它窗口部件;

2.如果Dialog通过exec来显示,那么可以通过accept或者reject来关闭窗口
   如果Dialog通过show来显示,那么可以通过close来关闭窗口,这个和QWidget是一样的;
   有许多的特殊dialog;打印预览,文件选择,messagebox, 颜色选择,字体选择,打印;//这些对话框都是系统的对话框

3.相同之处包括都可以在部件上面用QPainter画;


mydialog.h

#ifndef MYDIALOG_H#define MYDIALOG_H#include <QDialog>class MyDialog : public QDialog{    Q_OBJECTpublic:    explicit MyDialog(QWidget *parent = 0);    void paintEvent(QPaintEvent *);    QString _strDir;//打开的目录,如果是空的,那么打开当前项目所在文件夹signals:    public slots:    void slotButtonClick();};#endif // MYDIALOG_H

mydialog.cpp


#include "mydialog.h"#include <QPushButton>#include <QDebug>#include <QFileDialog>//文件对话框#include <QFileInfo>#include <QCalendarWidget>#include <QColorDialog>//颜色选择对话框#include <QFontDialog>//字体对话框#include <QMessageBox>//消息对话框#include <QPainter>MyDialog::MyDialog(QWidget *parent) :    QDialog(parent){    QPushButton * button = new QPushButton("Click me", this);    button->setGeometry(150, 100, 80, 30);    connect(button, SIGNAL(clicked()), this, SLOT(slotButtonClick()));}void MyDialog::slotButtonClick(){#if 0    QDialog *dig = new QDialog();    int ret;    QPushButton  *button = new QPushButton(dig);    //connect(button, SIGNAL(clicked()), dig, SLOT(accept()));//发送accept信号,来关闭窗口    connect(button, SIGNAL(clicked()), dig, SLOT(reject()));//发送reject信号,来关闭窗口/*如果Dialog通过exec来显示,那么可以通过accept或者reject来关闭窗口如果Dialog通过show来显示,那么可以通过close来关闭窗口,这个和QWidget是一样的;有许多的特殊dialog;打印预览,文件选择,messagebox, 颜色选择,字体选择,打印;//这些对话框都是系统的对话框*/    ret = dig->exec();    if(ret == QDialog::Accepted){        qDebug() << "Accepted";    }    if(ret == QDialog::Rejected){         qDebug() << "Rejected";    }   // dig.exec();//模态对话框,在模态对话框中,exec有自己的消息循环;并且把app的消息循环接管了;#endif#if 0    //保存文件的对话框    QString filename = QFileDialog::getSaveFileName(NULL, "select a file to save", _strDir, "Png file(*png)");#endif#if 0    QString filename = QFileDialog::getOpenFileName(NULL, "select a directory to open", _strDir, "Png file(*png)");#endif#if 0    QString filename = QFileDialog::getExistingDirectory();    if(filename.isEmpty()){        //没有选择        return;    }    qDebug() << filename << endl;    QFileInfo fileInfo(filename);    _strDir = fileInfo.filePath();#endif#if 0    QColorDialog colorDialog;    colorDialog.exec();    QColor color = colorDialog.selectedColor();    qDebug() << color;#endif#if 0    QFontDialog fontDialog;    fontDialog.exec();    QFont font = fontDialog.selectedFont();    qDebug() << font;#endif#if 0    QMessageBox::warning(this, "Error", "Error mesg...");#endif#if 0    QMessageBox::critical(this, "Critical", "Critical error..");#endif    int ret = QMessageBox::question(this, "this", "realy do.....", QMessageBox::Yes | QMessageBox::No | QMessageBox::YesToAll| QMessageBox::NoAll);//还可以添加选项    if (ret == QMessageBox::No){        qDebug() << "No";    }    if(ret == QMessageBox::Yes){         qDebug() << "Yes";    }    if(ret == QMessageBox::YesToAll){         qDebug() << "YesToAll";    }}/*画Dialog部件,和Widget是一样的*/void MyDialog::paintEvent(QPaintEvent *){    QPainter p(this);    p.setPen(Qt::red);    p.drawLine(QPointF(10, 10), QPointF(50, 50));    p.setBrush(Qt::yellow);    p.drawRect(QRect(100, 140, 200, 200));}#include <QApplication>int main(int argc, char *argv[]){    QApplication app(argc, argv);    MyDialog d;    d.setGeometry(400, 300, 400, 400);    d.show();    app.exec();}

1.新的对话框

2.保存文件的对话框

3.打开文件的对话框

4.颜色选择对话框

5.字体选择对话框

6.警告提示对话框

7.严重错误警告

8.询问对话框

9.用QPainter绘制的窗口(一条线,一个矩形)


0 0
原创粉丝点击