我试图在我的实例中MyApplication从继承QApplication或在我的WindowQML实例中继承关闭事件QQuickView。目标是在真正关闭应用程序之前要求确认退出。
在我的应用程序依赖于我在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()被正确发出,但事件没有被正确“阻止”,我的应用程序仍然关闭。
所以我有两个问题:
- 有没有办法检测
QQuickView实例的关闭事件? - 如果不可能,这种
MyApplication::event()方式是最好的行动方案吗?为什么我需要在event->ignore()这里打电话?我原以为返回true就足够了。