[英]How to quit the C++ application in Qt QML
according to the Qt qml Type documentation 根据Qt qml Type文档
quit()
放弃()
This function causes the QQmlEngine::quit() signal to be emitted.
此函数会导致发出QQmlEngine :: quit()信号。 Within the Prototyping with qmlscene, this causes the launcher application to exit;
在使用qmlscene的Prototyping中,这会导致启动器应用程序退出; to quit a C++ application when this method is called, connect the QQmlEngine::quit() signal to the QCoreApplication::quit() slot.
要在调用此方法时退出C ++应用程序,请将QQmlEngine :: quit()信号连接到QCoreApplication :: quit()插槽。
so in order to quit the C++ application in QML i have to call this 所以为了在QML中退出C ++应用程序,我必须调用它
Qt.quit()
inside the QML files, but that only quits the QML engine i need to close the C++ application also. 在QML文件中,但是只退出QML引擎我还需要关闭C ++应用程序。
here is my attempt 这是我的尝试
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QScopedPointer<NFCclass> NFC (new NFCclass);
QQmlApplicationEngine engine;
QObject::connect(engine, QQmlEngine::quit(), app, QCoreApplication::quit());
// here is my attempt at connecting based from what i have understood in the documentation of signal and slots
engine.rootContext()->setContextProperty("NFCclass", NFC.data());
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
Thank you very much if you can help me :) 非常感谢你,如果你可以帮助我:)
I think its because i dont know the object of QtCore thats why that line throws an error 我认为它是因为我不知道QtCore的对象,这就是为什么该行抛出错误
=========================================================================== edit: ================================================== =========================编辑:
Answer given by eyllanesc works. 由eyllanesc作品给出的答案。
but when i execute Qt.quit() in on completed it does not quit. 但是当我执行完Qt.quit()时它不会退出。 It works on the button though
它虽然适用于按钮
ApplicationWindow {
id:root
visible: true
width: 480
height: 640
title: qsTr("Hello World")
Component.onCompleted: {
Qt.quit()
}
Button{onClicked: Qt.quit()}
}
You have to learn to use the new connection syntax in Qt , in your case it is the following: 你必须学会在Qt中使用新的连接语法 ,在你的情况下它是如下:
QObject::connect(&engine, &QQmlApplicationEngine::quit, &QGuiApplication::quit);
Update: 更新:
A workaround for the second case is to use Qt.callLater()
第二种情况的解决方法是使用
Qt.callLater()
ApplicationWindow {
id:root
visible: true
width: 480
height: 640
title: qsTr("Hello World")
Component.onCompleted: {
Qt.callLater(Qt.quit)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.