Note that there are some explanatory texts on larger screens.

plurals
  1. POAsk confirmation before closing a QQuickView or QApplication
    text
    copied!<p>I am trying to catch a close event either in my <code>MyApplication</code> instance inheriting from <code>QApplication</code> or in my <code>WindowQML</code> instance inheriting from <code>QQuickView</code>. The goal is to ask confirmation to quit before really closing the application.</p> <p>Before my application was relying on <code>QMainWindow</code> where I implemented the <code>closeEvent()</code> method like this:</p> <pre><code>// MainWindow inherits from QMainWindow void MainWindow::closeEvent(QCloseEvent *event) { event-&gt;ignore(); confirmQuit(); // ask for confirmation first } </code></pre> <p>The problem is that my class <code>WindowQML</code> which inherits from <code>QQuickView</code> never pass inside the <code>closeEvent()</code> method. I then tried to overload the <code>event()</code> method like this:</p> <pre><code>// WindowQML inherits from QQuickView bool WindowQML::event(QEvent *event) { if(event-&gt;type() == QEvent::Close) { qDebug() &lt;&lt; "CLOSE EVENT IN QML WINDOW"; } } </code></pre> <p>but this event never happened either.</p> <p>The next road I tried to take was to catch the close event in <code>MyApplication</code> like this:</p> <pre><code>// We need to check for the quit event to ask confirmation in the QML view bool MyApplication::event(QEvent *event) { bool handled = false; switch (event-&gt;type()) { case QEvent::Close: qDebug() &lt;&lt; "Close event received"; event-&gt;ignore(); // mandatory? handled = true; Q_EMIT quitSignalReceived(); break; default: qDebug() &lt;&lt; "Default event received"; handled = QApplication::event(event); break; } qDebug() &lt;&lt; "Event handled set to : " &lt;&lt; handled; return handled; } </code></pre> <p>The signal <code>quitSignalReceived()</code> is emitted properly but the event is not "blocked" properly and my application still closes.</p> <p>So I have two questions:</p> <ol> <li>Is there a way to detect the closing event of a <code>QQuickView</code> instance?</li> <li>If it is not possible, is the <code>MyApplication::event()</code> way the best course of action? Why do I need to call <code>event-&gt;ignore()</code> here? I would have thought that returning <code>true</code> would be enough.</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload