Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. loading QML file as a component from C++ to QQmlApplicationEngine
Forum Updated to NodeBB v4.3 + New Features

loading QML file as a component from C++ to QQmlApplicationEngine

Scheduled Pinned Locked Moved General and Desktop
qmlc++
2 Posts 2 Posters 1.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kafanti
    wrote on last edited by kafanti
    #1

    Hello everyone,

    I am trying to load a qml file to my QQmlApplicationEngine. I guess, since the component is visual item(rect), somehow, engine does not render it.
    Can someone please point me where am i missing?

    Thank you.

    Here is my sample code,

    /* MyItem.qml */
    import QtQuick 2.5
     
    Rectangle {
        id: top
        property string str: "#FF0000"
        color: str
        width: 10
        height: 20
        Component.onCompleted: console.log("myitem ctor", width, height, x, y, visible, color);
    }
     
    /* main.qml */
    import QtQuick 2.5
    import QtQuick.Window 2.2
     
    Window {
        visible: true
        id: root
     
        MouseArea {
            id: mouse
            anchors.fill: parent
            onClicked: {
                Qt.quit();
            }
        }
     
        Text {
            id: text
            text: qsTr("Hello World")
            anchors.centerIn: parent
        }
    }
     
    /* main.cpp */
    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlComponent>
    #include <QQmlEngine>
     
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
     
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
     
        QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/MyItem.qml")));
     
        QObject *childItem = component.create();
        QList<QObject *> rootObjects = engine.rootObjects();
        QObject *parentItem = rootObjects.first();
     
        childItem->setParent(parentItem);
     
        QQmlEngine::setObjectOwnership(childItem, QQmlEngine::CppOwnership);
     
        return app.exec();
    }
    
    p3c0P 1 Reply Last reply
    0
    • K kafanti

      Hello everyone,

      I am trying to load a qml file to my QQmlApplicationEngine. I guess, since the component is visual item(rect), somehow, engine does not render it.
      Can someone please point me where am i missing?

      Thank you.

      Here is my sample code,

      /* MyItem.qml */
      import QtQuick 2.5
       
      Rectangle {
          id: top
          property string str: "#FF0000"
          color: str
          width: 10
          height: 20
          Component.onCompleted: console.log("myitem ctor", width, height, x, y, visible, color);
      }
       
      /* main.qml */
      import QtQuick 2.5
      import QtQuick.Window 2.2
       
      Window {
          visible: true
          id: root
       
          MouseArea {
              id: mouse
              anchors.fill: parent
              onClicked: {
                  Qt.quit();
              }
          }
       
          Text {
              id: text
              text: qsTr("Hello World")
              anchors.centerIn: parent
          }
      }
       
      /* main.cpp */
      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include <QQmlComponent>
      #include <QQmlEngine>
       
      int main(int argc, char *argv[])
      {
          QGuiApplication app(argc, argv);
       
          QQmlApplicationEngine engine;
          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
       
          QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/MyItem.qml")));
       
          QObject *childItem = component.create();
          QList<QObject *> rootObjects = engine.rootObjects();
          QObject *parentItem = rootObjects.first();
       
          childItem->setParent(parentItem);
       
          QQmlEngine::setObjectOwnership(childItem, QQmlEngine::CppOwnership);
       
          return app.exec();
      }
      
      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @kafanti You need to set a visual parent using setParentItem. And this parent being the Window's invisible root item which you can get using contentItem. So something like this:

      QQuickItem *parentItem = qvariant_cast<QQuickItem*>(rootObjects.first()->property("contentItem"));
      childItem->setParentItem(parentItem); 
      

      Also cast childItem to QQuickItem instead of QObject to get access to setParentItem.

      157

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved