QT Basic Tutorial (Dialog 2)


Preface

Continuing from the previous article, we explained modal dialog boxes and non-modal dialog boxes, so next we will explain the color dialog box, file dialog box, and font dialog box.

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

1. Color dialog box

Color Dialog is a common type of dialog box used for selecting colors. In Qt, you can use the QColorDialog class to create color dialog boxes. Here is a simple example code to create a color dialog box:

#include <QApplication>
#include <QColorDialog>
#include <QPushButton>

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

    // 创建一个颜色对话框
    QColorDialog dialog;
    dialog.setWindowTitle("Color Dialog");

    // 设置初始颜色
    dialog.setCurrentColor(Qt::red);

    // 打开颜色对话框
    int result = dialog.exec();

    if (result == QDialog::Accepted) {
    
    
        // 用户点击了选择颜色后的确认按钮
        QColor chosenColor = dialog.selectedColor();
        // 使用选择的颜色进行相应的操作
    }

    return app.exec();
}

The above code creates a simple color dialog box and sets the initial color to red. When the user selects a color in the dialog box and clicks the confirm button, the dialog box is accepted, and the code obtains the color selected by the user for subsequent operations.

running result:

Insert image description here

2. File dialog box

File Dialog is a common type of dialog box used to select files or save files. In Qt, you can use the QFileDialog class to create file dialog boxes. Here is a simple example code to create a file dialog box:

#include <QApplication>
#include <QFileDialog>
#include <QPushButton>

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

    // 创建一个文件对话框
    QFileDialog dialog;
    dialog.setWindowTitle("File Dialog");

    // 设置对话框的模式为选择文件
    dialog.setFileMode(QFileDialog::ExistingFile);

    // 打开文件对话框
    int result = dialog.exec();

    if (result == QDialog::Accepted) {
    
    
        // 用户点击了选择文件后的确认按钮
        QString selectedFile = dialog.selectedFiles().at(0);
        // 使用选择的文件进行相应的操作
    }

    return app.exec();
}

The above code creates a simple file dialog box and sets the dialog's mode to select a file. When the user selects a file and clicks the confirm button, the dialog box is accepted, and the code obtains the file path selected by the user for subsequent operations.

running result:

Insert image description here

3. Font dialog box

Font Dialog is a common type of dialog box used to select font styles. In Qt, you can use the QFontDialog class to create font dialog boxes. Here is a simple example code to create a font dialog box:

#include <QApplication>
#include <QFontDialog>
#include <QPushButton>

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

    // 创建一个字体对话框
    QFontDialog dialog;
    dialog.setWindowTitle("Font Dialog");

    // 设置对话框的模态性
    dialog.setModal(true);

    // 设置对话框的初始字体
    dialog.setCurrentFont(QFont("Arial", 12));

    // 打开字体对话框
    int result = dialog.exec();

    if (result == QDialog::Accepted) {
    
    
        // 用户点击了选择字体后的确认按钮
        QFont selectedFont = dialog.selectedFont();
        // 使用选择的字体进行相应的操作
    }

    return app.exec();
}

The above code creates a simple font dialog box and sets the dialog box to be modal. The initial font is also set to Arial font and the font size is 12. When the user selects a font style and clicks the confirm button, the dialog box is accepted and the code obtains the font selected by the user for subsequent operations.

running result:
Insert image description here

Summarize

Dialog is a common user interface component used to interact with users and obtain user input. A dialog box usually appears as a temporary window or pop-up box in an application and provides specific functions, such as selecting colors, selecting files, entering text, etc.

In Qt, common dialog boxes include modal dialog boxes (Modal Dialog) and modeless dialog boxes (Modeless Dialog). Modal dialog boxes block other parts of the application until the dialog box is closed or hidden, whereas non-modal dialog boxes do not block other parts of the application and the user can interact with the application while the dialog box is open.

一些常见的对话框类型包括:

1. File Dialog: used to select files or save files, provides the function of browsing the file system, and can usually set file type filters and initial paths.

2. Color Dialog: used to select colors and provides the functions of palette and color selector.

3. Font Dialog: Used to select font styles, providing options such as font, font size, font shape, etc.

4. Message Box: Used to display warnings, reminders, prompts and other information, and provide relevant buttons for users to choose.

5. Input Dialog: Used to obtain user input. It can obtain different types of input such as text and passwords.

This article mainly explains the three types of dialog boxes. After reading the article, you can conduct experimental verification on your own.

Guess you like

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