首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么QQmlComponent::create()返回nullptr?

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

Stack Overflow用户
提问于 2019-05-15 08:47:06
回答 1查看 108关注 0票数 0

在什么情况下,QQmlComponent::create(QQmlContext *)会返回nullptr而失败?文档只说“如果创建失败则返回nullptr”,没有更多细节。我的后端C++代码尝试从以下qml实例化一个MessageDialog:

代码语言:javascript
复制
    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
    }

下面是我的后端构造函数的一个片段:

代码语言:javascript
复制
    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();

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-15 23:42:43

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

代码语言:javascript
复制
new QQmlComponent(&engine, QStringLiteral("qrc:/ui-components/MessageDialog.qml"));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56140398

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档