【发布时间】:2019-01-19 11:27:14
【问题描述】:
根据 Qt qml Type 文档
退出()
这个函数导致 QQmlEngine::quit() 信号被发出。在使用 qmlscene 进行原型设计时,这会导致启动器 申请退出;退出 C++ 应用程序时此方法 调用时,将 QQmlEngine::quit() 信号连接到 QCoreApplication::quit() 槽。
所以为了退出 QML 中的 C++ 应用程序,我必须调用它
Qt.quit()
在 QML 文件中,但这只会退出 QML 引擎,我还需要关闭 C++ 应用程序。
这是我的尝试
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();
}
如果你能帮助我,非常感谢你:)
我认为是因为我不知道 QtCore 的对象,这就是该行抛出错误的原因
================================================ ============================== 编辑:
eyllanesc works给出的答案。
但是当我在完成时执行 Qt.quit() 时它不会退出。但它适用于按钮
ApplicationWindow {
id:root
visible: true
width: 480
height: 640
title: qsTr("Hello World")
Component.onCompleted: {
Qt.quit()
}
Button{onClicked: Qt.quit()}
}
【问题讨论】:
-
QQmlApplicationEngine的quit信号已经连接到QCoreApplication::quit()。你不应该自己做。