Qt - How to detect a closed window in a custom event loop without using slots

I am working on an OpenGL-based game using QGLWidget as a front-end. But due to all the extra overhead (plus the need to distribute heavy QT libraries!) I plan to switch to GLUT and in the process of replacing "Qt-stuff" with more standard alternatives before the big jump.

To replace the QTimers that control the frame timer and fps, I am trying to insert these functions into a simple loop that replaces the call to app.exec () as follows:

//main.cpp #include <QApplication> #include <windows.h> #include "mainwindow.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); MainWindow mainWin;//only contains a glwidget mainWin.show(); do { app.processEvents(); //call draw functions/fps based on system tick count //... Sleep(10); } while (!app.closingDown()); //<-- this doesn't terminate return 0; } 

While it works normally at runtime, however, as soon as you try to close the window (through the "X" button of the system), the window leaves, but the program freezes in the background. The problem is that I cannot find a static flag or request function that indicates that exit () or close () is being called. bool closingDown() always seems to be false, and I tried to switch the static flag to the ~MainWindow destructor and detect this, but this does not work, since it is not called until main finishes. I know that maybe I could do this by attaching to the QApps aboutToQuit() signal or, perhaps, creating a derived class from Qapp and intercepting it, but this view defeats the goal of moving away from signals / slots. Is there an easy way to find out if the ordered QApplication was closed, outside the class?

+4
source share
2 answers

You can override

 void QWidget::closeEvent ( QCloseEvent * event ) 

to set your static flag. This method calls for closed events from a window.

+4
source

I recommend adding app.sendPostedEvents(); before this call QApplication::processEvents() . This may actually solve your problem, but if not, you can at least use event handlers, as mentioned by @jon.

Nitpick: Qt is usually not formatted in all caps. The expression "QT" means you are talking about QuickTime!

+2
source

Source: https://habr.com/ru/post/1436321/


All Articles