Qt - detection when closing QWindow

I am making a game with Qt and I am creating a custom game loop so that I can precisely control the logical timing and rendering with the maximum frame rate.

Typically, to create QWindow, you will do the following:

...
QGuiApplication app(argc,argv);
QWindow* window = new QWindow();
window.show();
app.exec();
...

And exec()will return when the window is closed.

However, I do it as follows:

QGuiApplication *app = new QGuiApplication(argc,argv);
     QWindow* window = new QWindow();
     window.show();
     while (!terminate_loop) {
          app->processEvents();
          GameUpdate();
          GameRender();
     }
     app->exit();

However, my cycle continues when the window closes. I need to determine when the window is closed, so my loop may end. I tried the following. To capture when the window was closed:

connect(app,SIGNAL(lastWindowClosed()),this,SLOT(windowClosed()));

After closing the window, it should be started, but it never starts. It makes fire when I use app->exec()event handling instead of manual.

void QTEngine::windowClosed() {
    terminate_loop = true;
}

After further study, the Qt documentation says the following:

[lastWindowClosed] exec(), ( ) .

exec(), . , QWindow ?

+3
2

, Qt . "OpenGL Window" , Qt, .

, QWindow. closeEvent() 5.4.1. QWindow close. :

class CloseEventFilter : public QObject
{
     Q_OBJECT
public:
     CloseEventFilter(QObject *parent) : QObject(parent) {}

protected: 
     bool eventFilter(QObject *obj, QEvent *event)
     {
          if (event->type() == QEvent::Close)
          { 
               // Do something interesting, emit a signal for instance.
          }

          return QObject::eventFilter(obj, event);
     }

};

, installEventFilter(), QObject, QWindow.

QGuiApplication *app = new QGuiApplication(argc,argv);
QWindow* window = new QWindow();
CloseEventFilter *closeFilter = new CloseEventFilter(window);
window->installEventFilter(closeFilter);
...
+4

. .

1) QWindow Qt::WA_DeleteOnClose. , , . QObject, , destroyed(), , destroyed() .

2) QWidget::closeEvent() QWindow .

-1

All Articles