Qml 在关闭QQuickView或QApplication之前要求确认

Qml 在关闭QQuickView或QApplication之前要求确认,qml,qt5,qtquick2,Qml,Qt5,Qtquick2,我试图在继承自QApplication的MyApplication实例中或继承自QQuickView的WindowQML实例中捕获关闭事件。目标是在真正关闭应用程序之前请求确认退出 在我的应用程序依赖于QMainWindow之前,我实现了closeEvent()方法,如下所示: // MainWindow inherits from QMainWindow void MainWindow::closeEvent(QCloseEvent *event) { event->ignore()

我试图在继承自
QApplication
MyApplication
实例中或继承自
QQuickView
WindowQML
实例中捕获关闭事件。目标是在真正关闭应用程序之前请求确认退出

在我的应用程序依赖于
QMainWindow
之前,我实现了
closeEvent()
方法,如下所示:

// MainWindow inherits from QMainWindow
void MainWindow::closeEvent(QCloseEvent *event)
{
  event->ignore();
  confirmQuit(); // ask for confirmation first
}
// WindowQML inherits from QQuickView
bool WindowQML::event(QEvent *event)
{
  if(event->type() == QEvent::Close)
  {
    qDebug() << "CLOSE EVENT IN QML WINDOW";
  }
}
// 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->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 to : " << handled;
  return handled;
}
问题是继承自
QQuickView
的类
WindowQML
从未在
closeEvent()
方法中传递。然后我尝试像这样重载
event()
方法:

// MainWindow inherits from QMainWindow
void MainWindow::closeEvent(QCloseEvent *event)
{
  event->ignore();
  confirmQuit(); // ask for confirmation first
}
// WindowQML inherits from QQuickView
bool WindowQML::event(QEvent *event)
{
  if(event->type() == QEvent::Close)
  {
    qDebug() << "CLOSE EVENT IN QML WINDOW";
  }
}
// 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->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 to : " << handled;
  return handled;
}
信号
quitSignalReceived()
正确发出,但事件没有正确“阻止”,我的应用程序仍然关闭

所以我有两个问题:

  • 是否有方法检测
    QQuickView
    实例的关闭事件
  • 如果不可能,那么
    MyApplication::event()
    方式是否是最佳的行动方案?为什么我需要在这里调用
    event->ignore()
    ?我本以为返回
    true
    就足够了

  • 我不知道为什么
    QWindow
    没有
    closeEvent
    便利事件处理程序。看起来是个错误,遗憾的是,它不能在Qt6.0之前添加。无论如何,任何QWindow在关闭时都肯定会得到一个
    QCloseEvent
    。因此,只需覆盖
    事件
    ,并在那里执行事件处理

    证据:

  • 本测试程序:

    #include <QtGui>
    
    class Window : public QWindow
    {
    protected:
        bool event(QEvent *e) Q_DECL_OVERRIDE
        {
            int type = e->type();
            qDebug() << "Got an event of type" << type;
            if (type == QEvent::Close)
                qDebug() << "... and it was a 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();
    }
    

  • 看起来你是对的。我应该在我的
    WindowQML
    类中重载的
    event()
    方法中接收关闭事件。我已经在一个基本的例子上测试了它,它是有效的。我必须调查一下为什么我不能在我现在的课堂上得到这个事件。ThanksIt似乎不适合我,
    如果(!shouldClose()){e->ignore();return true;}
    调用了我的代码,但窗口仍然关闭。有什么想法吗?我看了QGUI应用程序源代码,第1861行在我看来很有趣。但那只是我…:)刚发现,你不应该忽视,但要接受关闭事件(至少在MacOSX上),这就是我觉得有趣的地方