QCoreApplication::quit or QCoreApplication::exit simply stop event processing, so that a call to QCoreApplication::exec will return. In your app, you never get to exec because you are in an infinite loop. You are on the right track with your lMustQuit variable, but you never set it to true. One method would be to connect the lastWindowClosed signal to a custom function and set lMustQuit to be true. Fortunately, we can alternatively use QEventLoop which has a method QEventLoop::isRunning which tells us when exit or quit have been called. Here is a simple example:
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QEventLoop>
void ReceiveThread();
int main(int argc, char *argv[])
{
//GUI part
w.show();
QObject::connect( qApp,
SIGNAL(lastWindowClosed
()),
qApp,
SLOT(quit
()) );
// start the receive thread
ReceiveThread();
/* We only get here after quit is called. */
return 0;
}
void ReceiveThread()
{
while (loop.isRunning()) /* Returns false after exit or quit have been called. */
{
loop.processEvents();
}
}
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QEventLoop>
void ReceiveThread();
int main(int argc, char *argv[])
{
//GUI part
QApplication a(argc, argv);
QWidget w; //subclass of Qwidget
w.show();
QObject::connect( qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit()) );
// start the receive thread
ReceiveThread();
/* We only get here after quit is called. */
return 0;
}
void ReceiveThread()
{
QEventLoop loop;
while (loop.isRunning()) /* Returns false after exit or quit have been called. */
{
loop.processEvents();
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks