qml - Ask confirmation before closing a QQuickView or QApplication -


i trying catch close event either in myapplication instance inheriting qapplication or in windowqml instance inheriting qquickview. goal ask confirmation quit before closing application.

before application relying on qmainwindow implemented closeevent() method this:

// mainwindow inherits qmainwindow void mainwindow::closeevent(qcloseevent *event) {   event->ignore();   confirmquit(); // ask confirmation first } 

the problem class windowqml inherits qquickview never pass inside closeevent() method. tried overload event() method this:

// windowqml inherits qquickview bool windowqml::event(qevent *event) {   if(event->type() == qevent::close)   {     qdebug() << "close event in qml window";   } } 

but event never happened either.

the next road tried take catch close event in myapplication this:

// need check quit event ask confirmation in qml view bool myapplication::event(qevent *event) {   bool handled = false;    switch (event->type())   {     case qevent::close:       qdebug() << "close event received";       event->ignore(); // mandatory?       handled = true;       q_emit quitsignalreceived();       break;      default:     qdebug() << "default event received";       handled = qapplication::event(event);       break;   }      qdebug() << "event handled set : " << handled;   return handled; } 

the signal quitsignalreceived() emitted event not "blocked" , application still closes.

so have 2 questions:

  1. is there way detect closing event of qquickview instance?
  2. if not possible, myapplication::event() way best course of action? why need call event->ignore() here? have thought returning true enough.

i don't know why qwindow hasn't closeevent convenience event handler. looks mistake, , sadly can't added before qt 6.0. anyhow, qwindow gets qcloseevent when gets closed. override event , perform event handling there.

proofs:

  1. qguiapplication source code
  2. this test program:

    #include <qtgui>  class window : public qwindow { protected:     bool event(qevent *e) q_decl_override     {         int type = e->type();         qdebug() << "got event of type" << type;         if (type == qevent::close)             qdebug() << "... , close event!";          return qwindow::event(e);     } };  int main(int argc, char **argv)  {     qguiapplication app(argc, argv);     window w;     w.create();     w.show();     return app.exec(); } 

    prints this

    got event of type 17  got event of type 14  got event of type 13  got event of type 105  got event of type 13  got event of type 206  got event of type 206  got event of type 8  got event of type 207  got event of type 19  ... , close event!  got event of type 18  

Comments