1

I have a weird qt problem: My application doesn't quit in some configurations.

The idea is to have a program, which can be started as a program with a GUI (through myWindow) or as a pure console application (controlled via myConsole, which runs its own loop in its thread to record keyboard inputs). Either way quitting is done by calling the myObject slot quitMyObject, which in turn cleans up some objects and emits the signal signalQuitapplication, which is connected to the QApplication (app) quit slot.

Unfortunately the application only quits when the the window is enabled and the quit command is entered in the console (although the slotQuitMyObject of myObject is always called). So I wonder what criterions Qt has to actually quit the main event loop and exit the program.

The code looks like this:

int main(int argc, char *argv[])

{

    bool enableWindow = false;
    QApplication app(argc, argv, enableWindow);
    MyUiAbstract* myConsole = new ConsoleUi(); // ConsoleUi inherits from MyUiAbstract, which inherits from QThread
    MyWindow* myWindow = NULL; // MyWindow inherits from QMainWindow
    if(enableWindow)
    {
        myWindow = new MyWindow();
        myWindow->show();
    }

     MyObject* myObject = new MyObject(myConsole, myWindow, ...); 

     QObject::connect(myObject, SIGNAL(signalQuitQApplication()), &app, SLOT(quit()), Qt::QueuedConnection);

     QObject::connect(myConsole, SIGNAL(signalQuitMyObject()), myObject, SLOT(slotQuitMyObject()), Qt::QueuedConnection);
     QObject::connect(myWindow, SIGNAL(signalQuitMyObject()), myObject, SLOT(slotQuitMyObject()), Qt::QueuedConnection);
     QObject::connect(myWindow, SIGNAL(signalQuitConsoleUI()), myConsole, SLOT(slotQuitMyUi()), Qt::QueuedConnection);

     return app.exec();
} 
Lol4t0
  • 12,444
  • 4
  • 29
  • 65
enam
  • 23
  • 1
  • 4
  • Does all your connections succeed? You will see warnings in runtime, if any failed. – Lol4t0 Aug 13 '12 at 18:40
  • wellll, there was a typo `signalQuitQApplication` vs `signalQuitApplication`, that hurt... now the only non-working thing is quitting the program from the GUI (strangely enough it works, if the debugger is on). – enam Aug 13 '12 at 19:54
  • Ok, I figured out the second bug: a call to `getline(...)` blocked the console thread... I 'brutally' solved this by terminating the thread, as there doesn't seem to be a simple and clean solution (s.o. else had that problem http://stackoverflow.com/questions/6408584/problem-with-getline-and-threads). The window is only for testing, so I think this is ok... – enam Aug 13 '12 at 20:51

1 Answers1

-1

Try to use that code in your MyConsole class:

#include <QApplication>
...
qApp->quit();

Also you need to close all the event loops & threads before quit.

aleks_misyuk
  • 567
  • 1
  • 4
  • 16
  • I can't call "qApp->quit" from "myConsole", because the applications central "myObject" needs to clean up all its objects first. Maybe I could make the call from the "myObject" directly, if I store the "qApp" as a member... – enam Aug 13 '12 at 19:06
  • hm... having the qApp as a member does not seem to be intended by qt... How did you intend me to make "qapp" known to my object?... Besides this: `QObject::connect(myObject, SIGNAL(signalQuitQApplication()), &app, SLOT(quit()), Qt::DirectConnection);` should have the same effect and it doesn't help. – enam Aug 13 '12 at 19:19
  • Does your code like this: class MyConsole: public QThread { Q_OBJECT protected: virtual void run(); signals: void quitSignal(); }; void MyConsole::run() { int i = 1; while (true) { std::cin >> i; if (i == 0) { emit quitSignal(); } } } int main(int argc, char *argv[]) { bool enableWindow = false; QApplication app(argc, argv, enableWindow); MyConsole* myConsole = new MyConsole(); QObject::connect(myConsole, SIGNAL(quitSignal()), &app, SLOT(quit())); myConsole->start(); return app.exec(); } – aleks_misyuk Aug 13 '12 at 19:32
  • similar, though i leave the infinite loop before emitting the signal... anyway, the problem mostly seemed to be a nasty typo now only quitting from the GUI doesn't work (see above), I apologize... I thought the compiler would catch such things :-/ – enam Aug 13 '12 at 20:04
  • Unfortunately not. In runtime you can get such warnings as: 'Object::connect: No such signal MyConsole::qui1tSignal()' or 'Object::connect: No such slot QApplication::quit1()'. – aleks_misyuk Aug 13 '12 at 20:18
  • Yeah, I didn't look at the beginning of the output, because I have some other "debug" output in it... Thanks for your effort! – enam Aug 13 '12 at 20:46
  • `connect(&obj, &hang::done, &app, &app::quit, Qt::QueuedConnection);` , maybe http://stackoverflow.com/questions/19141910/qcoreapplication-ignores-quit-signal-and-hangs will help. – Life Jul 22 '14 at 06:41