The difference between show and exec in the QT dialog box

Source: Internet
Author: User

(Note: This article in the reprint of the same time make a small number of changes. )

The Qdialog display has two functions show () and exec (). Their differences are explained in the reference documentation as follows:

Show ():
Displays a modeless dialog box. Control is immediately returned to the calling function. Whether the pop-up window is a modal dialog box depends on the value of the modal property.
(Original: Shows the dialog as a modeless dialog. Control returns immediately to the calling code. The dialog is modal or modeless according to the value of the modal property. )

EXEC ():
Displays a modal dialog box and locks the program until the user closes the dialog box. function returns a dialogcode result. During the dialog pop-up, users cannot switch to other Windows under the program until the dialog box is closed.
(Original: Shows the dialog as a modal dialog, blocking until the user closes it. The function returns a dialogcode result. The Users cannot interact with the the same application until, they close the dialog. )

---------------------------------------------------------------------------

Just a little bit of my understanding of modal and modeless dialogs:
Modal dialog box--that is, when the window is ejected, the entire program is locked, waiting until the dialog is closed. This often requires the return value of the dialog box to do the following. Such as: Confirm the window (select Yes or no).
Modeless dialog--after invoking the pop-up window, the call returns immediately, continuing with the following action. Here is just a call to the issuing of instructions, do not wait nor do any processing. such as: Find box.

Literally, show () can display modeless or modal dialog boxes (set modal values). When Modal=true is the same as exec ().
After testing, there is still a difference.
With show (), although other actions (buttons, window switches, etc.) of the program fail when the dialog box pops up, the program still returns the following code after calling the dialog box. In this way, you will not get the return value of the window. In this view, show () is only "half mode".
With exec (), after the call, the program is locked in place. Wait for the window to close.

In fact, Qdialog's show () function comes from its parent class Qwidget. and exec () is his own.

I recently particularly like to inherit the Qwidget class to do pop-up windows, its advantage is convenient, flexible, can be used as pop-up window can also be embedded in another page (Qdialog is not possible). The problem is that Qwidget does not have the EXEC () function. So the modal dialog box that you want to pop up like this is not going to happen.
Also looked at some data, there is said to use while (true) loop, there is said to receive events, but do not feel good.

Therefore, there is no better solution to be found.

Continued:
The problems left over are viewed Qte source code (not found QT) finally solved.
I looked at the exec () function of the Qdialog class. The discovery contained the same call to show (), just followed by a qapp->enter_loop () nested a new message loop to block the execution of the current event, and then the Qapp->exit_loop () was called in the Hide () function. To exit the current message loop and continue to execute the original event.

Look up the two functions in QT Help, as explained below:
Enter_loop ():
This function is discarded. It is still kept in order for the old code to continue to work. We strongly recommend that you do not use it in newly written code. This function is directly involved in the main message loop (recursively). Don't call it unless you really know what you're doing. Recommended use: Qapplication::eventloop ()->enterloop () .
Exit_loop ():
is also discarded. Recommended use: Qapplication::eventloop ()->exitloop () .
Reminder: Both operations will go into the main message loop, using caution.

Just follow the advice, but the effect is the same anyway. The original custom window was modified to add two functions, respectively, to open and close windows, and to encapsulate the invocation of EventLoop (). The code is as follows:
#include <qapplication.h>
#include <qeventloop.h>
/* Simulate Qdialog::exec (), displayed in modal dialog box.
void Mywidget::d oexec ()
{
    this->show ()
    in_loop = TRUE;
& nbsp;  //qapp->enter_loop ();
    Qapplication::eventloop ()->enterloop ();
}
/* Close window */
void Mywidget::d oclose ()
{
if (in_loop) {
In_loop = FALSE;
Qapp->exit_loop ();
        Qapplication::eventloop ()->exitloop ();
   }
    this->close ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.