qt dialog data transfer



The dialog box appears to complete a simple or short-term task. The data interaction between the dialog and the main window is very important. This section will explain how to interact with data between the dialog and the main window. According to the previous explanation, dialog boxes are divided into two types: modal and non-modal. We will also take these two as examples to illustrate separately.

A modal dialog uses the exec() function to display it. The real meaning of the exec() function is to start a new event loop (we will cover the concept of events in detail in a later chapter). The so-called event loop can be understood as an infinite loop. After Qt opens the event loop, various events issued by the system can be monitored by the program. This event loop is equivalent to a polling function. Since it is an infinite loop, of course, where the event loop is turned on, the code will be blocked, and the following statements will not be executed. Therefore, for a modal dialog displayed using exec(), we can get the data value directly from the dialog object after the exec() function.

Take a look at the code below:

void MainWindow::open()
{
    QDialog dialog(this);
    dialog.setWindowTitle(tr("Hello, dialog!"));
    dialog.exec();
    qDebug() << dialog.result();
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

In the above code, we use exec() to display a modal dialog. The last line of code, qDebug() is similar to std::cout or Java's System.out.println(); statement, which outputs the following information to the standard output, usually the console. Using qDebug() requires importing header files. After the exec() function, we can directly get the data value of the dialog. Note that exec() starts an event loop and the code is blocked here. Since the exec() function does not return, the result() function below will not be executed. Until the dialog box is closed and the exec() function returns, at this point, we can get the data of the dialog box.

One thing to note is that if we set the dialog's property to WA_DeleteOnClose, then when the dialog is closed, the object is destroyed, and we can't use this method to get data. In this case, we can consider using the parent pointer to build the dialog box and avoid setting the WA_DeleteOnClose property; or use another method.

In fact, QDialog::exec() has a return value, and its return value is QDialog::Accepted or QDialog::Rejected. Usually we will use code like the following:

QDialog dialog(this);
if (dialog.exec() == QDialog::Accepted) {
    // do something
} else {
    // do something else
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

To determine the return value of the dialog box, that is, whether the user clicked "OK" or "Cancel". Please refer to the QDialog documentation for more details.

The modal dialog is relatively simple. If it is a non-modal dialog, the QDialog::show() function will return immediately. If we write it like this, it is impossible to get the data entered by the user. Because the show() function does not block the main thread, show() returns immediately, and the user has to execute the following code before the user has time to input. Of course, there will be no correct results. Then we should change the way to obtain data, that is to use the signal slot mechanism.

Since a modeless dialog can call QDialog::accept() or QDialog::reject() or the more generic QDialog::done() function when it is closed, we can emit a signal here. Also, if we can't find a suitable signalling point, we can rewrite the QDialog::closeEvent() function to emit the signal here. Just connect to this signal in the window that needs to receive data (here is the main window). A similar code snippet looks like this:

//!!! Qt 5
// in dialog:
void UserAgeDialog::accept()
{
    emit userAgeChanged(newAge); // newAge is an int
    QDialog::accept();
}

// in main window:
void MainWindow::showUserAgeDialog()
{
    UserAgeDialog *dialog = new UserAgeDialog(this);
    connect(dialog, &UserAgeDialog::userAgeChanged, this, &MainWindow::setUserAge);
    dialog->show();
}

// ...

void MainWindow::setUserAge(int age)
{
    userAge = age;
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

The above code is very simple and will not be repeated here. Also, the Qt 4 version of the above code should be easy to implement as well.

Don't worry if the data is still available if the dialog is closed. Because the mechanism of Qt signal and slot guarantees that when the slot function is called, we can always use the sender() function to get the sender of the signal. About the sender() function, you can find more introduction in the documentation. By the way, the existence of the sender() function allows us to use this function to implement a modeless dialog box that can only open one (by recording the mark in a dialog map when the dialog box is opened, in When the dialog box is closed, use the sender() function to determine whether it is the dialog box, and then delete it from the mapping table).

Reprinted from: https://blog.csdn.net/gusgao/article/details/48894923



The dialog box appears to complete a simple or short-term task. The data interaction between the dialog and the main window is very important. This section will explain how to interact with data between the dialog and the main window. According to the previous explanation, dialog boxes are divided into two types: modal and non-modal. We will also take these two as examples to illustrate separately.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324674056&siteId=291194637