【问题标题】:Ask confirmation before closing a QQuickView or QApplication在关闭 QQuickView 或 QApplication 之前询问确认
【发布时间】:2013-07-11 12:09:08
【问题描述】:

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

在我的应用程序依赖 QMainWindow 之前,我实现了这样的 closeEvent() 方法:

// MainWindow inherits from QMainWindow
void MainWindow::closeEvent(QCloseEvent *event)
{
  event->ignore();
  confirmQuit(); // ask for confirmation first
}

问题是我的类WindowQML 继承自QQuickView 永远不会在closeEvent() 方法内部传递。然后我尝试像这样重载event() 方法:

// WindowQML inherits from QQuickView
bool WindowQML::event(QEvent *event)
{
  if(event->type() == QEvent::Close)
  {
    qDebug() << "CLOSE EVENT IN QML WINDOW";
  }
}

但这个事件也从未发生过。

我试图采取的下一条路是像这样在MyApplication 中捕捉关闭事件:

// 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() 被正确发出,但事件没有被正确“阻止”,我的应用程序仍然关闭。

所以我有两个问题:

  1. 有没有办法检测QQuickView 实例的关闭事件?
  2. 如果不可能,MyApplication::event() 方式是不是最好的做法?为什么我需要在这里打电话给event-&gt;ignore()?我原以为返回 true 就足够了。

【问题讨论】:

  • 刚刚发现,你不应该忽略,而是接受关闭事件(至少在 Mac OS X 上),这就是我觉得有趣的地方

标签: qml qt5 qtquick2


【解决方案1】:

我不知道为什么QWindow 没有closeEvent 便利事件处理程序。看起来是个错误,遗憾的是在 Qt 6.0 之前无法添加。无论如何,任何 QWindow 在关闭时肯定会得到一个QCloseEvent。所以只需覆盖event 并在那里执行您的事件处理。

证明:

  1. QGuiApplication source code
  2. 这个测试程序:

    #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();
    }
    

    打印这个

    Got an event of type 17 
    Got an event of type 14 
    Got an event of type 13 
    Got an event of type 105 
    Got an event of type 13 
    Got an event of type 206 
    Got an event of type 206 
    Got an event of type 8 
    Got an event of type 207 
    Got an event of type 19 
    ... and it was a close event! 
    Got an event of type 18 
    

【讨论】:

  • peppe > 看来你是对的。我应该在我的WindowQML 类中的重载event() 方法中接收关闭事件。我已经在一个基本示例上对其进行了测试,并且可以正常工作。我将不得不调查为什么我没有在我目前的课程中得到这个事件。谢谢
  • 它似乎对我不起作用,if (!shouldClose()) { e-&gt;ignore(); return true; } 确实调用了我的代码和一切,但窗口仍然关闭。有任何想法吗?我查看了 QGuiApplication 源代码,第 1861 行对我来说很有趣。但这只是我...... :)
猜你喜欢
  • 1970-01-01
  • 2019-07-29
  • 1970-01-01
  • 1970-01-01
  • 2013-04-10
  • 1970-01-01
  • 1970-01-01
  • 2017-05-09
  • 1970-01-01
相关资源
最近更新 更多