在qqmlcomponent :: create(qqmlContext *(在什么条件下通过返回nullptr失败?该文档仅显示"如果创建失败,则返回nullptr",没有进一步的细节。我的后端C 代码试图从以下QML实例化missagedialog:
import QtQuick 2.0
import QtQuick.Dialogs 1.1
MessageDialog {
id: messageDialog
title: "My message"
text: "Fill in this message from C++"
onAccepted: {
console.log("Knew you'd see it my way!")
// Hide the dialog
visible = false
}
Component.onCompleted: visible = true
}
这是我的后端构造函数的片段:
QQmlApplicationEngine engine;
// Note that component resource is local file URL,
// not network - no need to wait before calling create()?
QQmlComponent *component =
new QQmlComponent(&engine,
QStringLiteral("ui-components/MessageDialog.qml"));
// Following call returns nullptr
QObject *childItem = component->create();
有人知道为什么吗?谢谢!
我认为它找不到您的QML文件。假设您的" UI-Components/Messagedialog.QML"在QRC文件中,请尝试:
new QQmlComponent(&engine, QStringLiteral("qrc:/ui-components/MessageDialog.qml"));