I have a program which instantiates and uses another class (subclass of Qwidget) . When I closed the widget, program does not terminate, how can I also terminate main function?
Printable View
I have a program which instantiates and uses another class (subclass of Qwidget) . When I closed the widget, program does not terminate, how can I also terminate main function?
In main program, I have a infinite loop, and I need to stop infinite loop when widget is closed by user.
But, how will understand that widget is closed? I put qApp->quit(); to destructor of QT class, but it didn't work.
usually this line
is added in main.cpp or you can use QApplication::setQuitOnLastWindowClosed
Should I rewrite quit() slot?
why? take a look at QCoreApplication::quit, moreover, you can't do this since this method is not virtual. :)
no just use the connect spirit suggest you ..
quit() is a static slot
void QCoreApplication::quit () [static slot]
no need of rewrite.. its like u want to rewrite exit(0) :)
Then I guess I should try another thing, because it does not work, after widget is closed, my program keeps working.
so, provide a compilable example which reproduces the problem to us.
here main function and function which contains infinite loop
Code:
int main(int argc, char *argv[]) { //GUI part rovkonInterface w; //subclass of Qwidget w.show(); userInterface=&w; // start the receive thread ReceiveThread(); return a.exec(); } void ReceiveThread() { rovkonInterface *w=((rovkonInterface *)userInterface); while ( lMustQuit == 0 ) { a->processEvents(); } }
btw, you can use qApp insted of this QApplication *a=((QApplication *)qtApplication);
Thanks for the "qApp" tip.
and sorry for the bad names I gave to functions. Actually there is no other thread, there is just a function with infinite loop and I added "a->processEvents();" in order to prevent freezing in widgets as you can see in code above.
Simply ReceiveThread() continues to run infinite loop when widget is closed, I need to stop this infinite loop.
Thanks anyway :)
in rovkonInterface class
is it a QDialog or QWidget ..
if QDialog we can receive
void QDialog::rejected ()
with that rejected call we can quit our application like
so u try emit your custom signal on rovkonInterface closeEvent()
reimplement closeEvent() in rovkonInterface() class
emit widgetClosed() // your custom signal
and
QObject::connect( w, SIGNAL(widgetClosed()), qApp, SLOT(quit()) );