QT Basic Tutorial (Dialog 1)


Preface

In this article we will explain the dialog boxes in QT.

资料合集地微信公众号:优质程序猿

1. Dialog Box Concept

In Qt, a dialog box (Dialog) is a window used to interact with the user, collect input, or display information. Dialog boxes can contain various controls (such as text boxes, buttons, check boxes, etc.) that are used to receive user input or display specific information. Qt provides several different types of dialog boxes to meet different needs.

下面是一些常见的Qt对话框类型:

1. Modal Dialog: A modal dialog is a dialog box that blocks other windows. When a modal dialog box is displayed, the user must interact with the dialog box before resuming interaction with other parts of the application. Modal dialog boxes can be created using the QDialog class.

2. Modeless Dialog: A modeless dialog box does not block other windows, and users can freely switch between the open dialog box and other parts of the application. Modeless dialog boxes can be created using the QDialog class and the show() function.

3. File Dialog: The file dialog is used to select files or save files. The QFileDialog class is provided in Qt for creating file selection dialog boxes and file saving dialog boxes.

4. Font Dialog: The Font Dialog is used to select font style, size and color. The QFontDialog class is provided in Qt for creating font selection dialog boxes.

5. Color Dialog: The color dialog is used to select colors. The QColorDialog class is provided in Qt for creating color selection dialog boxes.

Insert image description here

2. Modal dialog box

Modal Dialog is a common type of dialog box in Qt. Unlike modeless dialog boxes, modal dialog boxes, when opened, block other parts of the application until the user has finished interacting with the dialog box and closes the dialog box. This means that the user must first deal with the modal dialog box before continuing to interact with the application.
In Qt, you can use the QDialog class to create modal dialog boxes. Here are some common features and usage of modal dialog boxes:

1. Create a modal dialog box: You can create a modal dialog box by creating a subclass of QDialog or using an instance of QDialog. You can add controls, layouts, signal slots, etc. in the constructor.

2. Open the modal dialog box: Use the exec() function of the modal dialog box to open the dialog box. This function will block the program and wait for the dialog box to be closed before returning the result.

3. Process dialog results: You can interact on controls in the dialog box, such as button clicks or text input, etc. After the dialog box is closed, the user's input or selection can be processed by judging the return value of the dialog box or using signal slots.

4. Close the dialog box: You can use the accept() or reject() function to close the modal dialog box. accept() indicates that the user accepts the result of the dialog box, and reject() indicates that the user cancels the dialog box operation.

Modal dialog boxes are typically used in scenarios that require the user to provide important information, make a decision, or perform a specific task. Because modal dialog boxes block other parts of the application, they should be used with caution to avoid making the application appear unresponsive or stuck.

需要注意的是,Qt也提供了非模态对话框的选项,可以让用户在对话框打开的同时继续与应用程序进行交互。这样的对话框称为非模态对话框或模式对话框。

Sample code:

#include <QApplication>
#include <QDialog>
#include <QPushButton>

int main(int argc, char *argv[]) {
    
    
    QApplication app(argc, argv);

    // 创建一个模态对话框
    QDialog dialog;
    dialog.setWindowTitle("Modal Dialog");

    // 添加按钮
    QPushButton button("OK", &dialog);
    button.setGeometry(10, 10, 80, 30);

    // 连接按钮的点击事件
    QObject::connect(&button, &QPushButton::clicked, &dialog, &QDialog::accept);

    // 打开模态对话框
    dialog.exec();

    // 判断对话框的返回值
    if (dialog.result() == QDialog::Accepted) {
    
    
        // 用户接受对话框操作
        // 执行相应的逻辑
    } else if (dialog.result() == QDialog::Rejected) {
    
    
        // 用户取消对话框操作
        // 执行相应的逻辑
    }

    return app.exec();
}

The above code creates a simple modal dialog box that contains an "OK" button. When the user clicks the "OK" button, the dialog box is accepted, otherwise the dialog box is rejected. You can add more controls and logic according to your needs.

operation result:
Insert image description here

3. Modeless dialog box

Modeless Dialog, also known as modal dialog. Unlike modal dialog, modeless dialog does not block other parts of the application when it is opened. Users can interact with the dialog while it is open. application to interact. This kind of dialog box is usually used in scenarios that require the user to perform auxiliary operations or provide information.

In Qt, you can use the QDialog class to create modeless dialog boxes. Here is a simple example code to create a modeless dialog box:

#include <QApplication>
#include <QDialog>
#include <QPushButton>

int main(int argc, char *argv[]) {
    
    
    QApplication app(argc, argv);

    // 创建一个非模态对话框
    QDialog dialog;
    dialog.setWindowTitle("Modeless Dialog");

    // 添加按钮
    QPushButton button("Close", &dialog);
    button.setGeometry(10, 10, 80, 30);

    // 连接按钮的点击事件
    QObject::connect(&button, &QPushButton::clicked, &dialog, &QDialog::close);

    // 显示非模态对话框
    dialog.show();

    return app.exec();
}

The above code creates a simple modeless dialog box that contains a "Close" button. When the user clicks the "Close" button, the dialog box is closed. Different from modal dialog boxes, non-modal dialog boxes are displayed using the show() function instead of the exec() function.

Summarize

This article will explain it here and you can review and consolidate it later.

Guess you like

Origin blog.csdn.net/m0_49476241/article/details/132945939