• 【Qt控件之QDialog】使用及技巧


    简介

    QDialog是Qt中的一个类,继承自QWidget类,用于创建对话框窗口,可以显示模态(阻塞当前窗口)或非模态的对话框。对话框可以包含各种控件,如按钮、文本框等,用于与用户进行交互。

    主要函数说明

    • 构造函数:
      • QDialog(QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags())
    • 模态对话框相关函数:
      • void setModal(bool modal):设置对话框是否为模态(默认为非模态)。
      • int exec():以模态方式显示对话框,阻塞当前窗口的事件循环,并返回对话框的退出码。
      • void accept():关闭对话框,并设置结果码为Accepted,通常用于“确认”按钮的点击事件处理。
      • void reject():关闭对话框,并设置结果码为Rejected,通常用于“取消”按钮的点击事件处理。
    • 非模态对话框相关函数:
      • void show():以非模态方式显示对话框。
      • void hide():隐藏对话框。
      • bool isVisible() const:判断对话框是否可见。
    • 其他常用函数:
      • void setWindowTitle(const QString &title):设置对话框的标题。
      • void setFixedSize(const QSize &size):设置对话框的固定大小。

    用法及使用技巧

    1. 创建对话框对象:

      QDialog *dialog = new QDialog(parent);
      
      • 1
    2. 设置对话框的标题:

      dialog->setWindowTitle("Dialog Title");
      
      • 1
    3. 设置对话框的模态性:

      dialog->setModal(true);  // 设置为模态对话框
      
      • 1
    4. 显示模态对话框并获取结果:

      if (dialog->exec() == QDialog::Accepted) {
          // 处理对话框的结果(用户点击了“确认”按钮)
      }
      
      • 1
      • 2
      • 3
    5. 处理对话框内部按钮点击事件:

      connect(button, &QPushButton::clicked, dialog, &QDialog::accept);  // 点击按钮后关闭对话框并设置结果码为Accepted
      
      • 1
    6. 显示非模态对话框:

      dialog->show();  // 以非模态方式显示对话框
      
      • 1
    7. 自定义对话框样式:
      可以通过设置对话框的样式表(setStyleSheet函数)来定制对话框的外观。

    示例

    #include 
    
    int main(int argc, char *argv[]) {
        QApplication app(argc, argv);
    	// 创建主窗体
        QMainWindow mainWindow;
        // 创建按钮、改变尺寸以及移动位置
        QPushButton button("Open Dialog", &mainWindow);
        button.resize(100, 30);
        button.move(10, 10);
        // 显示主窗体
        mainWindow.show();
    	// 创建对话框
        QDialog dialog(&mainWindow);
        // 设置标题
        dialog.setWindowTitle("Dialog Title");
        dialog.setFixedSize(200, 100);
    
        QVBoxLayout layout(&dialog);
        QLabel label("This is a dialog", &dialog);
        label.setAlignment(Qt::AlignCenter);
        layout.addWidget(&label);
    
        QPushButton okButton("OK", &dialog);
        layout.addWidget(&okButton);
    	// 当点击按钮时,显示对话框
        QObject::connect(&button, &QPushButton::clicked, [&dialog]() {
            dialog.exec();
        });
        // 当触发点击时,对话框触发accept()函数
        QObject::connect(&okButton, &QPushButton::clicked, &dialog, &QDialog::accept);
    
        return app.exec();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    结论

    QDialog是Qt中用于创建对话框窗口的类,可以显示模态或非模态的对话框,并与用户进行交互。
    通过设置标题、模态性、对话框内部控件等,可以根据需求创建各种样式的对话框。
    QDialog的常用函数包括setModal、exec、accept等。
    通过合理使用QDialog,可以增强用户界面的交互性和功能性。

  • 相关阅读:
    数据预处理大全
    Ubuntu22.04 下安装驱动、CUDA、cudnn以及TensorRT
    Python爬虫实战案例——第四例
    Arduino驱动BNO055九轴绝对定向传感器(惯性测量传感器篇)
    metinfo 6.0.0任意文件读取漏洞复现
    leetcode第92场双周赛
    springboot实战(十一)之项目实用工具包介绍
    【游戏专区】飞机大战
    2023年第九届数维杯国际大学生数学建模挑战赛
    【PTA刷题】请编写函数,求子串(详解+代码)
  • 原文地址:https://blog.csdn.net/MrHHHHHH/article/details/133721638