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上),这就是我觉得有趣的地方