根据 Qt qml 类型文档
quit()
该函数会导致发出QQmlEngine::quit()信号。在使用qmlscene进行原型设计时,这会导致启动器应用程序退出;在调用此方法时要退出C++应用程序,请将QQmlEngine::quit()信号连接到QCoreApplication::quit()槽。
因此,为了退出 QML 中的 C++ 应用程序,我必须调用这个函数。
Qt.quit()
我需要关闭C++应用程序,但仅仅在QML文件里这样做只会退出QML引擎。
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给出的答案可行。
但是当我在完成时执行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()
。您不需要自己这样做。 - GrecKo