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. Create instance of QQmlComponent in C++
QtWS25 Last Chance

Create instance of QQmlComponent in C++

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 6.6k Views
  • 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.
  • L Offline
    L Offline
    Lyven
    wrote on last edited by
    #1

    Hello

    I'm trying to instantiate a component from a QML file in C++ on my scene. I read multiple tutorials but haven't got any further than the code below:

    @
    QGuiApplication app(argc, argv);
    QtQuick2ApplicationViewer mainViewer;

    mainViewer.setMainQmlFile(QStringLiteral("qml/ProofOfConcept/main.qml"));
    mainViewer.showExpanded();

    QQmlComponent component(mainViewer.engine(), "qml/ProofOfConcept/AlarmCenter.qml");
    QObject *object = component.create();@

    the qml component to be loaded is AlarmCenter.qml. I add it to the context of main.qml through it's engine and i want it to be displayed on my scene, but it's not. If i try to read the propertys

    @qDebug() << "opacity: " + QQmlProperty(object, "opacity").read().toString();@

    It actually reads the properties so the component has been created correctly. However it is nowhere to be seen on my screen when i run the code.

    What am I missing/doing wrong?

    thanks in advance

    regards

    Lieven

    1 Reply Last reply
    0
    • L Offline
      L Offline
      Lyven
      wrote on last edited by
      #2

      bump

      I'm really stuck over here, I can get it work with

      @QQuickView view;
      view.setSource(QUrl::fromLocalFile("MyItem.qml"));
      view.show();
      QObject *object = view.rootObject();@

      but then it my component erases everything underneath in my scene. I'm trying to find a solution in the qml tutorials, but I just can't initiate my component from qt :(

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Lyven
        wrote on last edited by
        #3

        I keep on trying to instansiate a QML element from C++

        I create an instance of my QML file Test.qml

        @import QtQuick 2.0

        Rectangle {
        width: 100
        height: 62

        color: "blue";
        
        Component.onCompleted: {
        
            console.debug("LOADED");
        }
        

        }@

        in my main.qml

        @import QtQuick 2.0

        Rectangle {
        id: screen;

        width: 800;
        height: 480;
        
        
        Test{
            objectName: "test";
        }
        

        }@

        I try to read out the properties by fetching the object by its object name. Afterwards i compare different values with the instance a try to show through my C++ code.

        I noticed the parent was different, so i made sure that was ok
        [item->setParent(root);]

        But i have no clue what else i can test on to find out why my qml file won't be shown through the C++ code. It is visible, it has an x&y position and a height and a width.

        I Did find out that the children of both are different, but that shouldn't be a problem i guess?

        @ QGuiApplication app(argc, argv);
        QtQuick2ApplicationViewer mainViewer;
        QObject *root;

        mainViewer.setMainQmlFile&#40;QStringLiteral("qml/ProofOfConcept/main.qml"&#41;);
        mainViewer.showExpanded();
        
        root = mainViewer.rootObject();   
        

        QQmlComponent managComp(mainViewer.engine(), "qml/ProofOfConcept/Test.qml", mainViewer.rootObject());
        QObject *managObj = managComp.create(mainViewer.rootContext());
        QQmlProperty(managObj, "z").write(5);

        QQuickItem *item = qobject_cast<QQuickItem*>(managObj);
        item->setParent(root);
        
        QObject *rect = root->findChild<QObject*>("test");
        
        qDebug() << rect->parent() << " " << item->parent();
        qDebug() << rect->children() << " " << item->children();@
        
        1 Reply Last reply
        0
        • L Offline
          L Offline
          Lyven
          wrote on last edited by
          #4

          After hours of searching i found out that the root object should be converted to a QQuickItem and then be passed to the item you want to display in the setParentItem.

          @ QQuickView view;
          view.setSource(QUrl::fromLocalFile("qml/QmlPlugin/main.qml"));
          view.show();

          QQuickItem *root = view.rootObject();
          
          
          
          
          QQmlComponent managComp(view.engine(), "qml/QmlPlugin/Test.qml");
          QObject *managObj = managComp.create();
          
          QQuickItem* item = qobject_cast<QQuickItem*>(managObj);
          
          item->setParentItem(root);
          
          qDebug() << item << root;@
          

          Don't know if the item->setParent is bugged? It also doesn't work if you try to set it up on the object itself, it has to happen on a qquickitem. also i found this very badly documented.

          1 Reply Last reply
          0
          • W Offline
            W Offline
            wooyay
            wrote on last edited by
            #5

            Thank you, you just saved me a lot of frustration

            1 Reply Last reply
            0
            • O Offline
              O Offline
              ondrejandrej
              wrote on last edited by
              #6

              Thanks!!! I spent half a day figuring it out.

              1 Reply Last reply
              0
              • L Offline
                L Offline
                Lyven
                wrote on last edited by
                #7

                No problem guys,

                However a small side note. This way of programming becomes very complex if you need to exchange data between the objects later on. It might by more simple if you can manipulate your desired end result while creating objects from javascript. (So load data from C++, then transfer it to the JS and then create the object with the data in JS).

                Good luck

                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