Почему QQmlComponent :: create () возвращает nullptr? - PullRequest
0 голосов
/ 15 мая 2019

При каких условиях QQmlComponent :: create (QQmlContext *) завершается ошибкой, возвращая nullptr?В документации сказано только «Возвращает nullptr, если создание не удалось» без дополнительных подробностей.Мой бэкэнд-код C ++ пытается создать экземпляр MessageDialog из следующего qml:



    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 голосов
/ 15 мая 2019

Не думаю, что он находит ваш файл qml.Предполагая, что ваш "ui-components / MessageDialog.qml" находится в файле qrc, попробуйте:

new QQmlComponent(&engine, QStringLiteral("qrc:/ui-components/MessageDialog.qml"));
...