⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

Passing parameters to class inherited from QQuickPaintedItem

  • I have my own class inheriting from QQuickPaintedItem, which I use to draw some stuff at qml. I register it with function qmlRegisterType from main.cpp and it works fine, but I have to pass on to it one argument (object which is created in main.cpp file), because I want to expose some properties from this object to qml with setContextProperty function. How to do that?

  • Lifetime Qt Champion


    What about a simple setter ?

  • Yeah, but how to access to instance of class inherited from QQuickPaintedItem?

  • Lifetime Qt Champion

    Can you show how you are currently using that class ?

  • Yes, I would like to access to object of Camera class, but don't know how, 'cos qml make this object and don't know how to access it from main.cpp.

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include <QtQuick/QQuickView>
    #include <QObject>
    #include "camera.h"
    #include "serial.h"
    int main(int argc, char *argv[])
        Serial serial;
        QGuiApplication app(argc, argv);
        qmlRegisterType<Camera>("CameraMod", 1, 0, "Video");
        QQuickView view;
        view.engine()->rootContext()->setContextProperty("serial", &serial);
        return app.exec();

  • Lifetime Qt Champion

    One thing: QGuiApplication should be the first thing you create in your application. It sets up Qt's internal to work properly. You better move your Serial object creation after app.

    As for getting access to your QML object, see the Interacting with QML Objects from C++ chapter in Qt's documentation.

Log in to reply