Dialog box for Day07Qt 2021-09-18

Keywords: Qt

QDialog -- use of dialog box

The QDialog class is the base class for all dialog window classes. Dialog window is a top-level window that is often used to complete short interactive tasks or give user behavior tips. Dialog boxes are divided into modal dialog boxes and non modal dialog boxes. Modal dialog boxes are dialog boxes that cannot interact with other windows of the same application before closing the dialog box, such as those that pop up when creating a new project. For the modeless dialog box, you can interact with him or with other windows in the same program, such as Microsoft's find and Replace dialog box. Usually, the exec () function is called to establish a modal dialog box, the pointer variable is created through new, and the show method is called to establish a non modal dialog box.

The following describes several common standard dialog box construction methods and common properties

1. Color dialog box

	QColor color = QColorDialog::getColor(QColor(255,0,0));
	qDebug()<<"color property"<<color.red()<<color.green()<<"Color string"<<color.name()<<"Proportion of cyan"<<color.cyan();

2. Font dialog box

The following code describes how the font dialog box captures whether to confirm or cancel being clicked.

 bool ok;
        QFont Font = QFontDialog::getFont(&ok,QFont("Hua Wen Caiyun",36));
        if(ok)//Captures the selection status of the default button
        {
            qDebug()<<"Font type"<<Font.family()<<"font size"<<Font.pointSize();
        }
        else
        {
            qDebug()<<"User deselected";
        }

3. File dialog box

Parameter Description: parameter 1 specifies the parent object, solves dependencies and uses Qt memory recovery mechanism; Parameter 2 Title Parameter of the file dialog box 3 default opening path parameter 4 filter. You can specify the file type. By default, the file type will be automatically filtered after opening the folder. The return value is QString

 		QString str = QFileDialog::getOpenFileName(this,"Open file","F://");
        qDebug()<<"Print path name"<<str;

4. Warning dialog box

Warning, information, question and error dialog boxes belong to QMessageBox. The difference is that the patterns of the prompt box are different

QMessageBox::warning(this,"warning","warn");

5. Error dialog box

QMessageBox::critical(this,"error","error");

6. Information dialog box

QMessageBox::information(this,"information","infrmation");

7. Question dialog box

Introduction to function parameters: parameter 1 specifies the dependency resolution and memory recycling mechanism of the parent object, parameter 2 sets the title of the dialog window, parameter 3 sets the prompt content of the dialog window, parameter 4 sets the type of two buttons, and parameter 5 sets the default selected button. The following code describes a method of capturing button press.

if(QMessageBox::Save == QMessageBox::question(this,"put questions to","question",QMessageBox::Save | QMessageBox::Cancel, QMessageBox::Cancel))
               {
                  qDebug()<<"preservation";
               }
               else
               {
                  qDebug()<<"cancel";
               }

Use the above dialog box to create a Qt case. Click the button to open the corresponding dialog box. The code and effect are shown in the following figure:

Don't forget to add the header file of the corresponding class. Combined with the debugging and printing information, judge whether the correct effect is produced

#include "day07qdialog.h"
#include "ui_day07qdialog.h"
#include <QDialog>
#include <QMessageBox>
#include <QFileDialog>
#include <QColorDialog>
#include <QFontDialog>
#include <QDebug>
day07QDialog::day07QDialog(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::day07QDialog)
{
    ui->setupUi(this);
   //Color dialog 
    connect(ui->colorBtn,&QPushButton::clicked,[=](){
        QColor color = QColorDialog::getColor(QColor(255,0,0));
        qDebug()<<"color property"<<color.red()<<color.green()<<color.name()<<color.cyan();
    });
    //Font dialog box
    connect(ui->fontBtn,&QPushButton::clicked,[=](){
        bool ok;
        QFont Font = QFontDialog::getFont(&ok,QFont("Hua Wen Caiyun",36));
        if(ok)
        {
            qDebug()<<"Font type"<<Font.family()<<"font size"<<Font.pointSize();
        }
        else
        {
            qDebug()<<"User deselected";
        }

    });
    //File dialog
    connect(ui->fileBtn,&QPushButton::clicked,[=](){
         //File dialog box parameter 1 parent window parameter 2 Title Parameter 3 default open path parameter 4 filter return value is QString
           QString str = QFileDialog::getOpenFileName(this,"Open file","F://");
           qDebug()<<"Print path name"<<str;
    });
    connect(ui->errorBtn,&QPushButton::clicked,[=](){
        QMessageBox::critical(this,"error","error");
    });
    connect(ui->warnBtn,&QPushButton::clicked,[=](){
         QMessageBox::warning(this,"warning","warn");
    });
    connect(ui->infoBtn,&QPushButton::clicked,[=](){
         QMessageBox::information(this,"information","infrmation");
    });
    connect(ui->quesBtn,&QPushButton::clicked,[=](){
        if(QMessageBox::Save == QMessageBox::question(this,"put questions to","question",QMessageBox::Save | QMessageBox::Cancel, QMessageBox::Cancel))
               {
                  qDebug()<<"preservation";
               }
               else
               {
                  qDebug()<<"cancel";
               }
    });
}

Posted by Spikey on Sat, 18 Sep 2021 10:51:32 -0700