QT: Dialog boxdialog

Posted by Griven on Tue, 28 Dec 2021 12:51:08 +0100

1 basic concepts of dialog box

  • Dialog box is an indispensable part of GUI program. Many functional components that cannot or are not suitable for being placed in the main window must be set in the dialog box. The dialog box is usually a top-level window, which appears at the top of the program to realize short-term tasks or concise user interaction.
  • Qt uses the QDialog class to implement the dialog box. Like the main window, we usually design a class to inherit QDialog. QDialog (and its subclasses, as well as all classes of Qt::Dialog type) has additional explanations for its parent pointer: if the parent is NULL, the dialog box will be used as a top-level window, otherwise it will be used as a child dialog box of its parent component (at this time, the default location is the center of the parent). The difference between the top-level window and the non top-level window is that the top-level window will have its own location in the taskbar, while the non top-level window will share the location of its parent component.
  • Dialog box is divided into modal dialog box and non - modal dialog box
    1. Modal dialog box, that is, it will block the input of other windows in the same application. Modal dialog boxes are common, such as the "open file" function. You can try to open a file in Notepad. When the open file dialog box appears, we can't operate on windows other than this dialog box.
    2. In contrast to the non modal dialog box, such as the search dialog box, we can continue to edit the contents of Notepad while displaying the search dialog box.

2 standard dialog box

  • The so-called standard dialog box is a series of dialog boxes built in Qt to simplify development. In fact, many dialog boxes are common, such as opening files, setting colors, printing settings, etc. These dialog boxes are available in all
  • It is almost the same in the program, so it is not necessary to implement such a dialog box in each program. Qt's built-in dialog boxes are roughly divided into the following categories:
    QColorDialog: select color;
    QFileDialog: select a file or directory;
    QFontDialog: select font;
    QInputDialog: allows the user to enter a value and return it;
    QMessageBox: modal dialog box, used to display information, ask questions, etc; QPageSetupDialog: provides paper related options for the printer;
    QPrintDialog: printer configuration;
    QPrintPreviewDialog: print preview;
    Qpprogressdialog: displays the operation process.

Code example:

#include "widget.h"
#include <QFileDialog>
#include <QString>
#include <QDebug>
#include <QStringList>
#include <QDialog>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    button = new QPushButton("Select file",this);
    this->resize(600,500);
    button->resize(100,100);
    button->move(250,100);
    connect(button,&QPushButton::clicked,this,[](){
        //QDebug str = QFileDialog::getOpenFileName();
        QStringList str = QFileDialog::getOpenFileNames();
        qDebug()<<str;
    });   //Standard dialog box
}

Widget::~Widget()
{
}

3 modal dialog box

  • Modal dialog lQt there are two levels of modal dialog boxes:
    Application level mode when the dialog box of this mode appears, users must first interact with the dialog box until the dialog box is closed, and then they can access other windows in the program.

    Window level mode that blocks only the window associated with the dialog box, but still allows the user to interact with other windows in the program. Window level mode is especially suitable for multi window mode. The general default is application level mode.

  • In the following example, we call exec() to display the dialog box, so this is a modal dialog box. When the dialog box appears, we cannot interact with the main window until we close the dialog box.
    QDialog dialog;
    dialog.setWindowTitle(tr("Hello, dialog!"));
    dialog.exec();

Code example:

#include "widget.h"
#include <QFileDialog>
#include <QString>
#include <QDebug>
#include <QStringList>
#include <QDialog>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    button1 = new QPushButton("Module dialog button",this);
    this->resize(600,500);
    button1->resize(100,100);
    button1->move(250,200);
    connect(button1,&QPushButton::clicked,this,[](){
       QDialog dialog;//Define a dialog box
       dialog.setWindowTitle("modal dialog box");//Set dialog title
       dialog.exec();//The dialog box is displayed and blocked
    });   //modal dialog box
}

Widget::~Widget()
{
}

4 modeless dialog box

Let's try to change exec() to show() to see the modeless dialog box:

QDialog dialog(this); 
dialog.setWindowTitle(tr("Hello, dialog!")); 
dialog.show();

Did it backfire? The dialog box flashed by! This is because the show () function will not block the current thread, the dialog box will be displayed, and then the function will return immediately and the code will continue to execute. Note that the dialog is built on the stack. After the show() function is executed, the dialog is destructed beyond the scope, so the dialog disappears. If you know the reason, you can change it. Let's change the dialog to build on the heap. Of course, there is no such problem:

QDialog *dialog = new QDialog; 
dialog­>setWindowTitle(tr("Hello, dialog!")); 
dialog­>show();

If you are careful enough, you should find that there is a problem with the above code: when you click the button frequently, there is a memory leak in the dialog! Dialog uses new to allocate space on the heap, but never delete. The solution is also simple:

dialog­>setAttribute(Qt::WA_DeleteOnClose);

The setAttribute() function automatically destroys the dialog box when it is closed.

Code example:

#include "widget.h"
#include <QFileDialog>
#include <QString>
#include <QDebug>
#include <QStringList>
#include <QDialog>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    button2 = new QPushButton("Non module dialog button",this);
    this->resize(600,500);
    button2->resize(100,100);
    button2->move(350,100);
    connect(button2,&QPushButton::clicked,this,[](){
       QDialog *dialog = new QDialog;//Define a dialog box
       dialog->setWindowTitle("modeless dialog box");//Set dialog title
       dialog->setAttribute(Qt::WA_DeleteOnClose);//Automatically release dialog after closing window
       dialog->show();//display a dialog box
    });   //modeless dialog box
}

Widget::~Widget()
{
}

5 message dialog box

QMessageBox is used to display message prompts. We generally use several static functions provided by it:

  • Displays the about dialog box.
 void about(QWidget * parent, const QString & title, const QString & text)  
This is the simplest dialog box with the title title,The content is text,Parent window is parent. There is only one dialog box OK Button.
  • Displays the about Qt dialog box. This dialog box is used to display information about Qt.
void aboutQt(QWidget * parent, const QString & title = QString()): 
  • The critical error dialog box is displayed.
StandardButton critical(QWidget * parent, const QString
   & title, const QString & text, StandardButtons buttons = Ok,
   StandardButton defaultButton = NoButton):  
  The dialog box will display a red error symbol. We can pass buttons Parameter indicates the button it displays. There is only one by default Ok Button, we can use StandardButtons Type specifies multiple buttons.
  • Similar to QMessageBox::critical(), the difference is that this dialog box provides a general information icon.
StandardButton information(
          QWidget * parent, 
          const QString & title, 
          const QString & text, 
          StandardButtons buttons = Ok, 
          StandardButton defaultButton = NoButton)
  • Similar to QMessageBox::critical(), the difference is that this dialog box provides a question mark icon, and the buttons displayed are "yes" and "no".
StandardButton question(
             QWidget * parent, 
             const QString & title, 
             const QString & text, 
             StandardButtons buttons = StandardButtons( Yes | No ),              StandardButton defaultButton = NoButton)
  • Similar to QMessageBox::critical(), the difference is that this dialog box provides a yellow exclamation mark icon.
StandardButton warning(
            QWidget * parent, 
            const QString & title, 
            const QString & text, 
            StandardButtons buttons = Ok, 
            StandardButton defaultButton = NoButton)

We use QMessageBox::question() to ask a question.

  • The parent window of this dialog box is this. QMessageBox is a subclass of QDialog, which means that its initial display position will be in the parent window
    The center of the mouth.
  • The second parameter is the dialog box
  • The third parameter is what we want to display.
  • The fourth parameter is the associated key type. We can use the or operator (|) to specify the buttons that should appear in the dialog box. For example, we want a Yes and a No.
  • The last parameter specifies the button selected by default.

This function has a return value to determine which button the user clicked. According to our writing method, it should be easy to see that this is a modal dialog box, so we can directly obtain its return value. The static function of QMessageBox class has the advantages of convenient use and obvious disadvantages: it is very inflexible. We can only use a few simple forms. In order to customize the details of QMessageBox, we must use the properties of QMessageBox to set the API. If we want to make a dialog box asking whether to save, we can use the following code:

     QMessageBox msgBox; 
     msgBox.setText(tr("The document has been modified.")); 
     msgBox.setInformativeText(tr("Do you want to save your changes?"));
     msgBox.setDetailedText(tr("Differences here..."));
     msgBox.setStandardButtons(QMessageBox::Save |QMessageBox::Discard | QMessageBox::Cancel);
     msgBox.setDefaultButton(QMessageBox::Save); 
     int ret = msgBox.exec(); 
     switch (ret)
      {
       case QMessageBox::Save:qDebug() << "Save document!"; break; 
       case QMessageBox::Discard: qDebug() << "Discard changes!"; break; 
       case QMessageBox::Cancel: qDebug() << "Close document!"; break; 
      } 
   msgBox Is a stack based QMessageBox example. We set its main text information as“ The document has been modified.",informativeText Is a simple description text that will be displayed in the dialog box. Here we use a detailedText,That is, details. When we click the details button, the dialog box can automatically display more information. There are three buttons in the dialog box defined by ourselves: save, discard and cancel. Then we used exec()It becomes a modal dialog box and performs corresponding operations according to its return value.

Code example:

#include "widget.h"
#include <QFileDialog>
#include <QString>
#include <QDebug>
#include <QStringList>
#include <QDialog>
#include <QMessageBox>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    button3 = new QPushButton("About dialog buttons",this);
    this->resize(600,500);
    button3->resize(100,100);
    button3->move(350,200);
    connect(button3,&QPushButton::clicked,this,[=](){
       QMessageBox::about(this,"title","content");
    });   //About dialog box
    button4 = new QPushButton("Query dialog button",this);
    this->resize(600,500);
    button4->resize(100,100);
    button4->move(350,300);
    connect(button4,&QPushButton::clicked,this,[=](){
       QMessageBox::question(this,"Huida daily Q & A","zex Is it Han Pi?");
    });   //Query dialog box
}

Widget::~Widget()
{
}


Topics: Qt