one of the great conveniences of using the QT editing interface is the fact that QT has a rich, full range of classes and function functions that programmers can simply invoke in the process of editing the program. For a window close operation, here is a common three slots, that is, quit (), exit (), and Close (). The code editing of the System prompt dialog box is first explained when the window exits. To exit the main program, you can call the member function exit (), and you can also call the slot quit (), both of which can play a role in closing the application. Just be aware that they are called in different ways. Examples of the following programs:
{
qapplication* app;
App->exit (0);
}
Or:
{
qapplication* app;
App->quit ();
}
At this point the two are equivalent, void Qapplication::quit () is equivalent to the function call Qapplication::exit (0). At this point, if you need to give the user prompt, you only need to add Qmessagebox message judgment statement in the program, to prompt the user to determine whether to exit the application. In addition, quit () as a slot, you can also connect the form of signals and slots, in response to a signal after closing the application. Such as:
Qpushbutton *quitbutton = new Qpushbutton ("Quit");
Connect (Quitbutton, SIGNAL (clicked ()), Qapp, SLOT (Quit ()));
If you close the form of a widget such as a window instead of an application, you must call the close () function to close the part. As follows:
if (! ( Qmessagebox::information (this,tr ("CT control view"), TR ("Do-really want to log out CT control view"), tr ("Yes"), tr ("No "))))
{
This->close ();
}
Where the condition is a dialog box, based on the user's choice of whether or not to close the part, this represents the address of the current Window part object. When the user selects "Yes", the widget exits, and the exit action is canceled instead. At this point, we have a quick look at the exit or close code edits for widgets such as applications and Windows. However, if the user clicks the X button in the upper-right corner of the window, you will notice that the window will still exit without giving any hint, even if you have added a prompt dialog in the program. So why did the close operation not give the prompt dialog first as we hoped? The reason is that the close operation at this time does not cause a call to a closed or exit statement with a prompt message. When the user taps the X close window, the system automatically tells the event a specific function, void qwidget::closeevent (Qcloseevent * e), so this should be a special note. Before closing the window, you can define a message statement that prompts the user to determine whether to close the window. Such as:
void Mainwindow::closeevent (Qcloseevent * Event)
{
Switch (qmessagebox::information (this, tr ("CT contr OL view "),
Tr (" Do-really want to log out CT Control View? "),
Tr (" Yes "), tr (" No "),
0, 1)
{
Case 0:
event->accept ();
Break
Case 1:
Default:
Event->ignore ();
Break
}
}
After compiling the program, the system calls the statement automatically when the user responds to the close action. Perhaps this time you will also ask whether the above function function is to define it as a slot, or to define a member function. The answer is either. On the one hand, the slot itself is a special member function, in addition to the signal connection, its function and function are no different. On the other hand, no additional connection signals and slots need to be set at this time, and once the function is added, the window will immediately respond to the close action. Although the closeevent (qcloseevent * Event) function function is added, the window will be closed with a prompt dialog box. But sometimes it happens that when you click Confirm to close, you'll see a dialog box that appears two times in the prompt dialog box. So what's the reason? In fact, it is very simple, because closeevent (qcloseevent * Event) will only respond to the action of close, two times the dialog box is undoubtedly added in the custom closing function and add a prompt dialog box statement, confirm close, respond to the close operation, The Closeevent (qcloseevent * Event) function will be called immediately by the system. Therefore, the solution is to define only a closed function with a prompt dialog box, which corresponds to the closeevent (qcloseevent * Event) function for different closing operations. Finally, if you want to close the main window or the main program, all open independent child windows can be closed at the same time. Then this time is generally in the main.cpp file to connect the signal void qapplication::lastwindowclosed (); For a detailed description of lastwindowclosed (), this is not explained here, you can use QT when you need to use Assistant See how they use the FA. Such as:
int main (int argc, char * * argv)
{
Qapplication A (argc, argv);
Abmainwindow *MW = new Abmainwindow ();
Mw->setcaption ("Qt example-addressbook");
A.setmainwidget (MW);
Mw->show ();
A.connect (&a, SIGNAL (lastwindowclosed ()), &a, SLOT (Quit ()));
int result = A.exec ();
Delete MW;
return result;
}
This way, the system closes the other child window programs that are open while the main window part is closed. It is worth noting that it is not possible to use A.connect (&a, SIGNAL (lastwindowclosed ()), &a, SLOT (Quit ())) to implement this function. Before you do this, you must pay particular attention to whether you have set the main window part, that is, the function function called Setmainwidget, so that when the main window part is closed, the other child widgets that have been opened at the moment of closing are not answered. Originally from: http://www.cnblogs.com/carfield/archive/2011/12/26/2299115.html
Quit (), exit (), and close () event capture for QT