0

Under what conditions does QQmlComponent::create(QQmlContext *) fail by returning nullptr? The documentation only says " Returns nullptr if creation failed" without further detail. My backend C++ code tries to instantiate a MessageDialog from the following 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
    }

And here's a fragment of my backend constructor:



    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();

Anyone know why? Thanks!

Tomasso
  • 693
  • 2
  • 9
  • 17
  • When I change the relative path provided to the QQmlComponent constructor to an absolute path, the subsequent call to create() works. I don't know why this is. Also why doesn't the Qt system print out any kind of information when create() fails? Took me hours to track down this problem! – Tomasso May 15 '19 at 15:48

1 Answers1

0

I don't think it is finding your qml file. Presuming your "ui-components/MessageDialog.qml" is in a qrc file, try:

new QQmlComponent(&engine, QStringLiteral("qrc:/ui-components/MessageDialog.qml"));
fallerd
  • 1,512
  • 10
  • 15