Qt深入浅出(八)模态对话框

模式对话框

​ 对话框可以分为模式对话框和非模式对话框两种。模式对话框是指用户只能在当前的窗体中进行操作,在该窗体没有关闭之前不能切换到其它的窗体。非模式对话框是指当前的所操作的窗体可以切换。

​ 简单来说,屏蔽同一应用程序中其它窗口事件响应的对话框,就叫做模式对话框。

​ 一般情况下,用于确认信息的操作对话框属于模式对话框。如打开文件对话框就是典型的模式对话框,而常见的查找和替换便是非模式对话框。

​ 在Qt中只要是继承至QDailog的窗口类都可以以模式对话框的方式来显示。

1 QDailog与QWidget对比

          

 

  QWidget

   1、指定父窗口的时候,不调用QWidget::show(),嵌套在父窗口中显示,与其它窗口共用消息循环。

2、指定父窗口的时候,调用QWidget::show(),仍然嵌套在父窗口中显示,与其它窗口共用消息循环。

   3、不指定父窗口的时候,调用QWidget::show(),以顶级窗口方式显示,与其它窗口共用消息循环。

   4、不能以模态窗口方式显示,没有exe()函数。

  QDailog继承至QWidget

   1、指定父窗口的时候,不调用QWidget::show(),不显示窗口。

   2、指定父窗口的时候,调用QWidget::show(),以顶级窗口方式显示,与其它窗口共用消息循环。

3、不指定父窗口的时候,调用QWidget::show(),以顶级窗口方式显示,与其它窗口共用消息循环。

   4、模态窗口显示,,调用QDailog::exec(),屏蔽其它窗口消息循环。

注意:简单来看,如果想以模式窗口显示,继承QDailog,调用QDailog::exec()即可。

2 触发一个模式对话框

​ 例如:


   
   
  1. #include "widget.h"
  2. #include <QPushButton>
  3. #include <QDialog>
  4. Widget::Widget(QWidget *parent) : QWidget(parent)
  5. {
  6. QPushButton * pb = new QPushButton("dialog", this);
  7. connect(pb, SIGNAL(clicked()), this, SLOT(showDialog));
  8. }
  9. void Widget::showDialog()
  10. {
  11. QDialog dialog;
  12. dialog.exec(); // 消息循环,阻塞等待模式窗口关闭。
  13. }

3 模式窗口exec返回值


   
   
  1. [virtual slot] int QDialog::exec()

​ 这个函数是模式窗口的关键,它会屏蔽原先的QApplication::exec的消息循环接收消息,所有的消息都被它接收走,直到它返回,其返回值为1时代表QDialog::Accepted,为0时代表QDialog::Rejected。

  • 如果想返回QDialog::Accepted,那么只需调用一下QDialog::accept槽函数


   
   
  1. [virtual slot] void QDialog::accept()
  • 如果想返回QDialog::Rejected,那么只需调用一下QDialog::reject槽函数


   
   
  1. [virtual slot] void QDialog::reject()
  • 如果想返回其它自定义的值,那么只需要调用一下QDialog::done槽函数


   
   
  1. [virtual slot] void QDialog::done(int r) //r参数来指定exec的返回值

来看一个复杂一点的例子:


   
   
  1. #include "widget.h"
  2. #include <QPushButton>
  3. #include <QHBoxLayout>
  4. #include <QDialog>
  5. #include <QDebug>
  6. Widget::Widget(QWidget *parent)
  7.   : QWidget(parent)
  8. {
  9.    initMainWidget();
  10.    initDialog();
  11. }
  12. void Widget::initMainWidget()
  13. {
  14.    QHBoxLayout * hBox = new QHBoxLayout(this);
  15.    QPushButton * pb  = new QPushButton("pb", this);
  16.    hBox->addWidget(pb);
  17.    this->setLayout(hBox);
  18.    connect(pb, SIGNAL(clicked()), this, SLOT(showDailog())); //按钮触发调用槽函数来显示对话框.
  19. }
  20. void Widget::initDialog()
  21. {
  22.    QPushButton * pbEnter  = new QPushButton("enter", this);
  23.    QPushButton * pbCancel  = new QPushButton("cancel", this);
  24.    QHBoxLayout * hBox = new QHBoxLayout(this);
  25.    _dialog = new QDialog(this);
  26.    _dialog->setLayout(hBox);
  27.    hBox->addWidget(pb_enter);
  28.    hBox->addWidget(pb_cancel);
  29.    connect(pbEnter, SIGNAL(clicked()),  _dialog, SLOT(accept()));
  30.    connect(pbCancel, SIGNAL(clicked()), _dialog, SLOT(reject()));
  31. }
  32. void Widget::showDailog()
  33. {
  34.    int ret = _dialog->exec(); //使用模式窗口显示
  35.    qDebug() << "show Dialog return value " << ret << endl;
  36. }
  37. Widget::~Widget()
  38. {
  39. }

4 常见Dialog应用

​ 常见的继承至QDialog的派生类,QFileDialog,QFontDialog,QColorDialog,QMessageBox,它们有一个共同特点,都提供了快速创建模式窗口的静态函数。

4.1 QFileDialog文件浏览框

  • 打开文件浏览框,用户选择完成后,返回对应的文件名


   
   
  1. [static] QString QFileDialog::getOpenFileName(QWidget *parent = Q_NULLPTR, const QString &caption = QString(), const QString &dir = QString(), const QString &filter = QString(), QString *selectedFilter = Q_NULLPTR, Options options = Options())
  2. /*
  3. *QWidget *parent 指定父窗口
  4. *const QString &caption 指定窗口标题
  5. *const QString &dir 指定打开的路径,模式情况下是当前路径
  6. *const QString &filter 指定文件的过滤器
  7. */
  • 保存文件浏览框,用户选择完成后,返回对应的文件名


   
   
  1. [static] QString QFileDialog::getSaveFileName(QWidget *parent = Q_NULLPTR, const QString &caption = QString(), const QString &dir = QString(), const QString &filter = QString(), QString *selectedFilter = Q_NULLPTR, Options options = Options())
  2.  /*
  3. *QWidget *parent 指定父窗口
  4. *const QString &caption 指定窗口标题
  5. *const QString &dir 指定打开的路径,模式情况下是当前路径
  6. *const QString &filter 指定文件的过滤器
  7. */

例如:

widget.h


   
   
  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. class Widget : public QWidget
  5. {
  6.    Q_OBJECT
  7. public:
  8.    Widget(QWidget *parent = 0);
  9.    ~Widget();
  10. public slots:
  11.    void showFileDailog();
  12.    void showSaveFileDialog();
  13. private:
  14.    QString _filename;
  15. };
  16. #endif // WIDGET_H

widget.cpp


   
   
  1. #include "widget.h"
  2. #include <QHBoxLayout>
  3. #include <QPushButton>
  4. #include <QFileDialog>
  5. #include <QDebug>
  6. Widget::Widget(QWidget *parent)
  7.   : QWidget(parent)
  8. {
  9.    QHBoxLayout * hBox = new QHBoxLayout(this);
  10.    QPushButton * pb  = new QPushButton("showFileDailog", this);
  11.    QPushButton * pb1  = new QPushButton("showSaveFileDailog", this);
  12.    hBox->addWidget(pb);
  13.    hBox->addWidget(pb1);
  14.    this->setLayout(hBox);
  15.    connect(pb, SIGNAL(clicked()), this, SLOT(showFileDailog())); //按钮触发调用槽函数来显示对话框.
  16.    connect(pb1, SIGNAL(clicked()), this, SLOT(showSaveFileDialog())); //按钮触发调用槽函数来显示对话框.
  17. }
  18. void Widget::showSaveFileDialog()
  19. {
  20.    _filename = QFileDialog::getSaveFileName(this, "save", _filename, "Image(*.png *.jpg)");
  21.    qDebug() << "get save filename :" << _filename << endl;
  22. }
  23. void Widget::showFileDailog()
  24. {
  25.   _filename = QFileDialog::getOpenFileName(this, "open", _filename, "Image(*.png *.jpg)");
  26.   qDebug() << "filename :" << _filename << endl;
  27. }

4.2 QFontDialog字体对话框

​ QFontDialog字体对话框用来获取一个字体格式,那么在这之前,我们需要了解一下什么是字体。

4.2.1 QFont类

​ Qt中的字体类为QFont,没有继承任何Qt基类,我们知道字是有大小、字体格式,那这些字体格式就保存在QFont对象中。

常用函数有:


   
   
  1. QString QFont::family() const; //获取字体格式
  2. int QFont::pointSize() const;  ///获取字体大小
  3. void QFont::setFamily(const QString &family); //设置字体格式
  4. void QFont::setPointSize(int pointSize); //设置字体大小

4.2.2 获取系统支持的所有字体


   
   
  1. QStringList QFontDatabase::families(WritingSystem writingSystem = Any) const

例如:


   
   
  1. QFontDatabase fontDb;
  2. qDebug() << fontDb.families() << endl;

4.2.3 QWidget的QFont对象

​ 在QWidget窗口上显示字也需要一个QFont成员对象,用来记录该窗口上显示文本信息时所有使用的字体格式。

  • QWidget获取字体对象


   
   
  1. const QFont & QWidget::font() const
  • QWidget设置窗口对应字体


   
   
  1. void QWidget::setFont(const QFont &)

例如, 改变Widget对象的字体格式


   
   
  1. #include "widget.h"
  2. #include <QLabel>
  3. #include <QDebug>
  4. Widget::Widget(QWidget *parent)
  5.   : QWidget(parent)
  6. {
  7.    QLabel* label = new QLabel("label", this);
  8.    QFont font = this->font();
  9.    font.setFamily("Verdana"); //将Widget对象的字体格式改为Verdana
  10.    font.set
  11.    this->setFont(font);
  12. }

4.2.4 使用QFontDialog获取字体

  • 字体对话框可以用来选择一种字体,返回对应的QFont对象,常用静态函数如下:


   
   
  1. [static] QFont QFontDialog::getFont(bool *ok, const QFont &initial, QWidget *parent = Q_NULLPTR, const QString &title = QString(), FontDialogOptions options = FontDialogOptions());
  2. /*
  3. *bool *ok 通过这个值来判断获取字体是否成功
  4. *QWidget *parent 指定父窗口
  5. *const QString &title 指定标题
  6. */

例如:


   
   
  1. #include "widget.h"
  2. #include <QFontDialog>
  3. #include <QHBoxLayout>
  4. #include <QPushButton>
  5. #include <QDebug>
  6. #include <QLabel>
  7. Widget::Widget(QWidget *parent)
  8.   : QWidget(parent)
  9. {
  10.    QHBoxLayout * hBox = new QHBoxLayout(this);
  11.    QPushButton * pb  = new QPushButton("showFontDailog", this);
  12.    QLabel * label = new QLabel("label", this);
  13.    hBox->addWidget(pb);
  14.    hBox->addWidget(label);
  15.    this->setLayout(hBox);
  16.    connect(pb, SIGNAL(clicked()), this, SLOT(showFontDailog())); //按钮触发调用槽函数来显示对话框.
  17. }
  18. void Widget::showFontDailog()
  19. {
  20.    bool ok;
  21.    QFont initFont = this->font();
  22.    QFont font = QFontDialog::getFont(&ok, initFont, this);
  23.    if(ok)
  24.   {
  25.        qDebug() << font << endl;
  26.        this->setFont(font);
  27.   }
  28. }

4.3 QColorDialog颜色对话框

  • QColor类

    QColor用来表示颜色,没有继承任何Qt基类,我们知道界面必然涉及到颜色相关的属性,那这些属性值都是一个个QColor对象来保存。

  • QPalette类

    QPalette调色板,由于QWidget需要多个QColor对象来保存窗口丰富的色彩,Qt又把所有不同用处的颜色封装到QPalette类中。可以通过QWidget::palette函数来获取对应窗口的调色板。

    
         
         
    1. const QPalette & QWidget::palette() const;

    然后再通过QPalette::setColor函数来设置不同用途的颜色。

    
         
         
    1. void QPalette::setColor(ColorRole role, const QColor &color);
    2. /*
    3. * ColorRole role枚举类型,其值可以是:
    4. * QPalette::Window 背景色
    5. * QPalette::WindowText 字体颜色
    6. * 等等
    7. */

    例如:

    
         
         
    1. #include "widget.h"
    2. #include <QLabel>
    3. #include <QDebug>
    4. Widget::Widget(QWidget *parent)
    5.   : QWidget(parent)
    6. {
    7.    new QLabel("label", this);
    8.    QPalette palette = this->palette();
    9.    palette.setColor(QPalette::WindowText, QColor(Qt::red));
    10.    this->setPalette(palette);
    11. }

  • QColorDialog常用静态函数getColor,用来获取一个QColor对象

    
         
         
    1. #include "widget.h"
    2. #include <QHBoxLayout>
    3. #include <QPushButton>
    4. #include <QDebug>
    5. #include <QLabel>
    6. #include <QColorDialog>
    7. Widget::Widget(QWidget *parent)
    8.   : QWidget(parent)
    9. {
    10.    QHBoxLayout * hBox = new QHBoxLayout(this);
    11.    QPushButton * pb  = new QPushButton("showColorDailog", this);
    12.    QLabel * label = new QLabel("label", this);
    13.    hBox->addWidget(pb);
    14.    hBox->addWidget(label);
    15.    this->setLayout(hBox);
    16.    connect(pb, SIGNAL(clicked()), this, SLOT(showColorDailog())); //按钮触发调用槽函数来显示对话框.
    17. }
    18. void Widget::showColorDailog()
    19. {
    20.    QPalette palette = this->palette();
    21.    QColor initColor = palette.color(QPalette::Background);
    22.    QColor color = QColorDialog::getColor(initColor, this, "getcolor");
    23.    if(color.isValid())
    24.   {
    25.        palette.setColor(QPalette::Background, color);
    26.        qDebug() << color << endl;
    27.        this->setPalette(palette);
    28.   }
    29. }

    4.4 QMessageBox消息对话框

    常用静态函数:

  • 警告对话框

    
         
         
    1. [static] StandardButton QMessageBox::warning(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton);
    2. /*
    3. * QWidget *parent 指定父窗口
    4. * const QString &title 窗口标题
    5. * const QString &text 警告内容
    6. */
  • 信息对话框

    
         
         
    1. [static] StandardButton QMessageBox::information(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton);
    2. /*
    3. * QWidget *parent 指定父窗口
    4. * const QString &title 窗口标题
    5. * const QString &text 信息内容
    6. */
    7.  

  • 疑问对话框

    
         
         
    1. [static] StandardButton QMessageBox::question(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = StandardButtons( Yes | No ), StandardButton defaultButton = NoButton);
    2. /*
    3. * QWidget *parent 指定父窗口
    4. * const QString &title 窗口标题
    5. * const QString &text 疑问内容
    6. */
发布了207 篇原创文章 · 获赞 66 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/qq_38025219/article/details/104910087
今日推荐