[solved] signal 'destroyed' is not emited
-
Hi,
I wonder why signal 'destroyed' is not emited in this case:
@
int main(int argc, char *argv[])
{
QApplication a(argc, argv);MainController mainController; mainController.init(); emit mainController.signal_start(); return a.exec();
}
@where
@
class MainController : public QObject {Q_OBJECT private: QThread mTerminalInputThread; TerminalInput* mTerminalInput; public: void init(); signals: void signal_start(); public slots: void slot_quit(); void slot_destroyed();
};
// --- sources ---
void MainController::init(){
mTerminalInput = new TerminalInput();
mTerminalInput->moveToThread(&mTerminalInputThread);
QObject::connect(mTerminalInput, SIGNAL(destroyed()), this, SLOT(slot_destroyed()));
QObject::connect(this, SIGNAL(signal_start()), mTerminalInput, SLOT(slot_loop()));
QObject::connect(mTerminalInput, SIGNAL(signal_quit()), this, SLOT(slot_quit()));
mTerminalInputThread.start();
}void MainController::slot_quit(){
mTerminalInput->deleteLater();
mTerminalInputThread.quit();
mTerminalInputThread.wait();
QApplication::quit();
}void MainController::slot_destroyed(){
fprintf(stdout, "mTerminalInput destroyed !!!\n");
}
@and
@
class TerminalInput : public QObject {
Q_OBJECTpublic: ~TerminalInput(); signals: void signal_quit(); public slots: void slot_loop();
};
// --- sources ---
TerminalInput::~TerminalInput(){
fprintf(stdout, "~TerminalInput");
}void TerminalInput::slot_loop(){
emit signal_quit();
}
@The destructor of mTerminalInput is called, but I do not receive a signal 'destroyed'. What am I doing wrong here?
-
Hi,
I am not sure, but when you call QApplication::quit(), I think the apllication stops the event loop so you signal will not be resolved. QApplication::quit() is equivalent to call exit(0): it is a "brutal" end. Depending on the design of your application, you should look at something like "QApplication::closeAllWindows()":http://qt-project.org/doc/qt-5/qapplication.html#closeAllWindows instead.
-
Hi, tilsitt,
thanks for response. Yes, QApplication::closeAllWindows()
solved the problem in my main program, which do have windows.The 'destroyed' signal is also emited in bug reconstruction code (I have posted) when using QApplication::closeAllWindows() instead of QApplication::quit(). But this time a desctructor is not called and program stops responding. I guess, it is because there is any windows.
-
Once again it's all depending on the design of your project. I mentioned QApplication::closeAllWindows() because you are using a QApplication object in your example, so I guessed you were using windows. Another way could have been to call QApplication::quit() in MainController::slot_destroyed() but without knowing your architecture and your goals, I can't figure out if that really is what you want.
-
QApplication::closeAllWindows() works in fixed bug reconstruction code.
@int main(int argc, char *argv[])
{
QApplication a(argc, argv);QDialog dialog; // <<-- dialog.show(); // <<-- MainController mainController; mainController.init(); emit mainController.signal_start(); return a.exec();
}@
The problem is solved.