Nominate our 2022 Qt Champions!

Dynamicly add c++class instances tu qml objects (not with listview)

  • Hi guys &girls,
    I'm writing an application that tracks the head movement from webcam and paint na image over the face. The problem is how to create and bind c++class instance to qml.
    Pseudo c++ code:
    @Class FaceItem{
    String imagePath;
    float scale;
    QVector2D position;
    QVector3D rotation;

    And now how do I create and bind a qml objects like:
    Rotation {axis: Qt.vector3d(0, 0, 1); angle: rotation_.x }

    I have tried ListView example (, but position of list element doesnt work for me, because listview does not permit to overlay two list elements(in my case images).
    Thanks for your help!

  • Have you looked at QQmlEngine::qmlRegisterSingletonType() ?

    This would allow you to define a QObject-based class from C++ with properties corresponding to each value to be passed from C++ to QML. Just emit the notify signal when each value is updated.

  • Thanks for replay.
    Yes, I have looked at this approach, but I dont understand how to connect an instance of a c++ class with generated qml item.
    Hod do I generate qml items? with javascript?
    How do I connect qml to a class instance? Does QQmlContext::setContextProperty work on dynamicly generated qml items?

  • Can you clarify the flow of the application? I'm not sure what is meant by generating of qml items and dynamically generated qml.

  • App flow if using ListView example:
    @For each frame:

    1. when new face is detected
      additem(image, position, rotation)
    2. when face is tracked

    Hope this clarifies my problem.

  • Nevermind, I got it working with ListView.
    Thank you for you help.

  • Thanks for clarifying.

    Another option is to use Qt.createComponent() and createObject() from javascript. The documentation for createComponent covers usage pretty well. The QML dynamic scene example provides a demonstration.

    As long as it works for you, I would stick with the ListView.

Log in to reply