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 Control View"),
TR ("Does 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 automatically calls the statement 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 is 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. Finally, say yourself. Use Terminate () to close the thread:
MainWindow
Connect (UI. Recvpushbutton,signal (clicked ()), This,slot (Test ()));
Connect (&tcpthread,signal (Signalrecv ()), This,slot (Recenetconnect ()));
Connect (UI. Stoppushbutton,signal (clicked ()), This,slot (Stop ()));
void Mainwindow::recenetconnect ()
{
Tcpiplink.tcpiprecvdata ();
Tcpiplink.tcpipcheck ();
DATA[0]=TCPIPLINK.RECVBUF[0];
DATA[1]=TCPIPLINK.RECVBUF[1];
DATA[2]=TCPIPLINK.RECVBUF[2];
DATA[3]=TCPIPLINK.RECVBUF[3];
DATA[4]=TCPIPLINK.RECVBUF[4];
DATA[5]=TCPIPLINK.RECVBUF[5];
DATA[6]=TCPIPLINK.RECVBUF[6];
DATA[7]=TCPIPLINK.RECVBUF[7];
DATA[8]=TCPIPLINK.RECVBUF[8];
DATA[9]=TCPIPLINK.RECVBUF[9];
DATA[10]=TCPIPLINK.RECVBUF[10];
The second assignment takes advantage of an array
DATA[0]=TCPIPLINK.DDDD[0];
DATA[1]=TCPIPLINK.DDDD[1];
DATA[2]=TCPIPLINK.DDDD[2];
DATA[3]=TCPIPLINK.DDDD[3];
DATA[4]=TCPIPLINK.DDDD[4];
DATA[5]=TCPIPLINK.DDDD[5];
S1 = Qstring::number (data[0], ' F ', 6);
S2 = Qstring::number (data[1], ' F ', 6);
S3 = Qstring::number (data[2], ' F ', 6);
S4 = Qstring::number (data[3], ' F ', 6);
S5 = Qstring::number (data[4], ' F ', 6);
S6 = Qstring::number (data[5], ' F ', 6);
S7 = Qstring::number (data[6], ' F ', 6);
S8 = Qstring::number (data[7], ' F ', 6);
S9 = Qstring::number (data[8], ' F ', 6);
S10 = Qstring::number (data[9], ' F ', 6);
S11 = Qstring::number (data[10], ' F ', 6);
Ui.lineedit->settext (S1);
Ui.lineedit_2->settext (S2);
Ui.lineedit_3->settext (S3);
Ui.lineedit_4->settext (S4);
Ui.lineedit_5->settext (S5);
Ui.lineedit_6->settext (S6);
Ui.lineedit_joint1->settext (S7);
Ui.lineedit_joint2->settext (S8);
Ui.lineedit_joint3->settext (S9);
Ui.lineedit_joint4->settext (S10);
Ui.lineedit_joint5->settext (S11);
}
void Mainwindow::test ()
{
Tcpthread.start ();
}
void Mainwindow::stop ()
{
printf ("It ' s stop!\n");
Tcpthread.terminate ();
}
Threadtcp
void threadtcp:: Run ()
{
Emit Signallink ();
Emit Signalrecv ();
while (1)
{
if (M_stopflag==false)
{
Break
}
printf ("ok!\n");
Sleep (400);
Emit Signalrecv ();
}
}
can terminate the thread and continue to receive data!!!!!!!
Quit (), exit (), Close () "reprint" of QT Learning experience