Use of the QT dialog box

Source: Internet
Author: User

The dialog box is widely used in qt gui applications. The dialog box can be modal or non-modal.

For the parameter selection dialog box, the modal dialog box is generally used; for the dialog box that shows or views some content, the non-modal dialog box is generally used.

Dialog Box class qdialog, official documents: http://qt-project.org/doc/qt-4.8/qdialog.html

1. Modal Dialog Box

The modal dialog box is implemented by calling the exec () function. When the modal dialog box is used, the event loops in the dialog box. You must close the dialog box to continue the operations on the main interface.

When the modal dialog box is closed, exec () returns a value.

Prototype: int qdialog: exec ()[Slot]

Return Value usage: for example, there are two buttons in the dialog box: OK and cancel. When you click OK, close the dialog box and the parameters set in the main program acceptance dialog box, click Cancel or X in the upper right corner, close the dialog box and return immediately without executing the following program. In this case, the clicked () signal of the OK button is connected to the accept () slot, and the clicked signal of the cancel button is connected to the reject () slot. In this case, when you click OK, the accept () slot is triggered and exec () returns qdialog: canceled Ted. When you click the cancel button, the reject slot is triggered and exec () returns qdialog:: rejected.

Connect (ui. OK, signal (clicked (), this, slot (accept ()));
Connect (ui. Cancel, signal (clicked (), this, slot (reject ()));

During execution, make a judgment and click "cancel" to return. Click the parameters in the "OK" dialog box to continue execution.

1 // call the dialog box in a function 2 // A is the dialog box Class Object 3 if(a.exe C () = qdialog: rejected) 4 {5 // operation 6 return; 7} 8 // parameter 9 in the transfer dialog box // continue operation

If you do not know the meaning, you can implement the code.

2. Non-Modal Dialog Box

The show () function is called in the non-Modal Dialog Box. Of course, in the main program, when calling the non-modal dialog box, if only show () is written, the dialog box may flash through, to solve this problem, we need the Event Response slot processevents () of the global object to regularly access this dialog box, so that we can process the events on the main interface and the dialog box will not disappear.

When using this method, I use the singleshot () method of the qtimer class. The qtimer class provides clock operations.

Qtimer class official documentation: http://qt-project.org/doc/qt-4.8/qtimer.html

Singleshot prototype: void qtimer: singleshot (intMSEC, Qobject *Cycler, Const char *Member)[Static]

The first parameter is the execution interval in ms.

The second parameter is the accepted object.

The third parameter is the trigger slot

Exactly, we can trigger processevents () at every msec time so that the dialog box does not disappear. The second parameter is the dialog box object. It should be noted that the dialog box objects should be defined on the stack (that is, to define them as pointer objects), because if they are defined on the stack, the objects will be automatically cleared after the function is executed, if the object required by the second parameter is not found, the dialog box cannot be displayed.

# Include <qtimer>

Cmodallessdialog * dialog; // it is best to define the global variable dialog = new cmodallessdialog; dialog-> show (); dialog-> settitle (TR (" ")); // set the window title // some qtimer operations in the dialog box: singleshot (200, dialog, slot (qapp-> processevents (); // The interval is 200 ms.

When the dialog object is deleted, the dialog box disappears.

// If the dialog box is displayed, delete if (null! = DIALOG) {Delete dialog = NULL ;}

When the dialog box is displayed, note that if dialog! = Nulls, delete it and assign the null redistribution address.

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.