The QT console program runs through a separate thread to monitor user input exit programs (the user enters Q and presses ENTER to exit the console program).
Monitor thread header file:
#include <QThread>
class Cmykeyeventthread:public qthread
{
q_object public
:
Explicit Cmykeyeventthread (QObject *parent = 0);
Protected:
void Run ();
Monitor line Routines File:
#include "cmykeyevent.h"
#include <stdio.h>
cmykeyeventthread::cmykeyeventthread (QObject *parent): Qthread (parent)
{
}
void Cmykeyeventthread::run ()
{
char *psztip = ' Press ' Q ' exit Application.\n ";
while (true)
{
printf ('%s ', psztip);
if (getchar () = = ' Q ')
{break
;
}
}
printf ("done.");
}
Main.cpp file:
#include <QCoreApplication>
#include "cmykeyevent.h"
int main (int argc, char *argv[])
{
Qcoreapplication A (argc, argv);
Cmykeyeventthread *exitevent = new Cmykeyeventthread ();
Qobject::connect (Exitevent, SIGNAL (finished ()), &a, SLOT (Quit ()));//Connection Signal
Exitevent->start ();//Start thread return
a.exec ();
}