为什么QQmlComponent :: create()返回nullptr?

时间:2019-05-15 00:47:06

标签: qtquick2 qqmlcomponent

在什么情况下,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();

有人知道为什么吗?谢谢!

1 个答案:

答案 0 :(得分:0)

我认为它找不到您的qml文件。假设您的“ ui-components / MessageDialog.qml”在qrc文件中,请尝试:

props