Congratulations to our 2022 Qt Champions!

Create instance of QQmlComponent in C++

  • 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;


    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



  • bump

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

    @QQuickView view;
    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 :(

  • 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: {


    in my main.qml

    @import QtQuick 2.0

    Rectangle {
    id: screen;

    width: 800;
    height: 480;
        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

    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;

    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);
    QObject *rect = root->findChild<QObject*>("test");
    qDebug() << rect->parent() << " " << item->parent();
    qDebug() << rect->children() << " " << item->children();@

  • 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;

    QQuickItem *root = view.rootObject();
    QQmlComponent managComp(view.engine(), "qml/QmlPlugin/Test.qml");
    QObject *managObj = managComp.create();
    QQuickItem* item = qobject_cast<QQuickItem*>(managObj);
    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.

  • Thank you, you just saved me a lot of frustration

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

  • 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

Log in to reply