在什么情况下,QQmlComponent::create(QQmlContext *)会返回nullptr而失败?文档只说“如果创建失败则返回nullptr”,没有更多细节。我的后端C++代码尝试从以下qml实例化一个MessageDialog:
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();有人知道为什么吗?谢谢!
发布于 2019-05-15 23:42:43
我不认为它会找到你的qml文件。假设您的"ui-components/MessageDialog.qml“位于qrc文件中,请尝试:
new QQmlComponent(&engine, QStringLiteral("qrc:/ui-components/MessageDialog.qml"));https://stackoverflow.com/questions/56140398
复制相似问题