9_对话框.md
对话框
分类
Qt 中使用 QDialog 类实现对话框。就像主窗口一样,我们通常会设计一个类继承 QDialog。
QDialog(及其子类,以及所有 Qt::Dialog 类型的类)的对于其 parent 指针都有额外的解释:如果 parent 为 NULL,则该对话框会作为一个顶层窗口,否则则作为其父组件的子对话框(此时,其默认出现的位置是 parent 的中心)。顶层窗口在任务栏会有自己的位置,而非顶层窗口则会共享其父组件的位置。
- 模态对话框 : qt有两种-> 应用级(默认)和窗口级
- 非模态对话框
模态对话框:
#include "mainwindow.h" #include <QMenuBar> #include <QToolBar> #include <QDialog> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { setWindowTitle(tr("Main Window")); openAction = new QAction(QIcon(":/images/doc-open"), tr("&Open..."), this); openAction->setShortcut(QKeySequence::Open); openAction->setStatusTip(tr("Open an existing file")); connect(openAction, &QAction::triggered, this, &MainWindow::open); QMenu *file = this->menuBar() -> addMenu(tr("&File")); file->addAction(openAction); QToolBar *toolBar = addToolBar(tr("&File")); toolBar->addAction(openAction); } MainWindow::~MainWindow() { } void MainWindow::open() { QDialog dialog(this); dialog.setWindowTitle(tr("Hello, dialog!")); dialog.exec(); }
模态:dialog.show()
void MainWindow::open() { QDialog *dialog = new QDialog; dialog->setAttribute(Qt::WA_DeleteOnClose); dialog->setWindowTitle(tr("Hello, dialog!")); dialog->show(); }
setAttribute()函数设置对话框关闭时,自动销毁对话框。另外,QObject 还有一个deleteLater()函数,该函数会在当前事件循环结束时销毁该对话框
对话框数据传递
模态对话框
模态对话框使用了 exec()函数将其显示出来。exec()函数的真正含义是开启一个新的事件循环。事件循环,可以理解成一个无限循环。Qt 在开启了事件循环之后,系统发出的各种事件才能够被程序监听到。这个事件循环相当于一种轮询的作用。
对于使用了 exec()显示的模态对话框,我们可以在 exec()函数之后直接从对话框的对象获取到数据值。
void MainWindow::open() { QDialog dialog(this); dialog.setWindowTitle(tr("Hello, dialog!")); dialog.exec(); qDebug() << dialog.result(); }
如果我们设置 dialog 的属性为 WA_DeleteOnClose,那么当对话框
关闭时,对象被销毁,我们就不能使用这种办法获取数据了。在这种情况下,我们可以考虑使用 parent 指针的方式构建对话框,避免设置 WA_DeleteOnClose 属性;或者是利用另外的方式。
非模态对话框
QDialog::show()函数会立即返回,如果我们也这么写,就不可能取得用户输入的数据。使用信号槽机制获取信息。
非模态对话框在关闭时可以调用 QDialog::accept()或者 QDialog::reject()或者更通用的 QDialog::done()函数,所以我们可以在这里发出信号。如果找不到合适的信号发出点,我们可以重写 QDialog::closeEvent()函数,在这里发出信号。
标准对话框 QMessageBox
Qt内置的对话框:
-
QColorDialog:选择颜色;
-
QFileDialog: 选择文件或者目录;
-
QFontDialog: 选择字体;
-
QInputDialog: 允许用户输入一个值,并将其值返回;
-
QMessageBox: 模态对话框,用于显示信息、询问问题等;
-
QPageSetupDialog: 为打印机提供纸张相关的选项;
-
QPrintDialog: 打印机配置;
-
QPrintPreviewDialog: 打印预览
-
QProgressDialog: 显示操作过程
QMessageBox
-
void about(QWidget * parent, const QString & title, const QString & text)
: 显示关于对话框。这是一个最简单的对话框,其标题是 title,内容是 text,父窗口是 parent。对话框只有一个 OK 按钮。 -
void aboutQt(QWidget * parent, const QString & title = QString())
: 显示关于 Qt 对话框。该对话框用于显示有关 Qt 的信息。 -
StandardButton critical(QWidget * parent, const QString & title, const QString& text, StandardButtons buttons = Ok, StandardButton defaultButton =NoButton)
: 显示严重错误对话框。这个对话框将显示一个红色的错误符号。我们可以通过 buttons 参数指明其显示的按钮。默认情况下只有一个 Ok 按钮,我们可以使用 StandardButtons 类型指定多种按钮。 -
StandardButton information(QWidget * parent, const QString & title, constQString & text, StandardButtons buttons = Ok, StandardButton defaultButton =NoButton)
:QMessageBox::information()函数与 QMessageBox::critical()类似,不同之处在于这个对话框提供一个普通信息图标。 -
StandardButton question(QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = StandardButtons( Yes | No ), StandardButton defaultButton = NoButton)
:QMessageBox::question()函数与QMessageBox::critical()类似,不同之处在于这个对话框提供一个问号图标,并且其显示的按钮是“是”和“否”两个。 -
StandardButton warning(QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton)
:QMessageBox::warning()函数与 QMessageBox::critical()类似,不同之处在于这个对话框提供一个黄色叹号图标。
void MainWindow::about() { QMessageBox::aboutQt(this); QMessageBox::about(this, tr("About"), tr("<h1>Hello About This</h1>")); QMessageBox::critical(this, tr("Critical"), tr("<h2>error</h2>")); QMessageBox::information(this, "Information", "?????"); QMessageBox::question(this, "Question", "question"); QMessageBox::warning(this, "Warning", "warning"); if (QMessageBox::Yes == QMessageBox::question(this, "question", "Are you OK?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes)) { QMessageBox::information(this, "infor", "你一定是雷军的粉丝了"); } else { QMessageBox::information(this, "information", "Are you OK? Thank you very much."); } }
本文作者:nsfoxer's blog
本文链接:https://www.cnblogs.com/nsfoxer/p/16317438.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步