[SOLVED] Access engine root context in dynamically created QQuickPaintedItem

Issue

Is it possible to access engine root context in dynamically created QQuickPaintedItem, without a global reference ?

class LiveStream(QQuickPaintedItem):

    def __init__(self):
        QQuickPaintedItem.__init__(self)
        self.backend = root_ctx.contextProperty("backend") # <- I want to remove/change this global reference
        # self.getRootContext() <- Is there such a function, I cant find such a function on documentaion 
        ...

if __name__ == "__main__":
    app = QApplication()
    engine = QQmlApplicationEngine()
    url = QUrl.fromLocalFile(MAIN_QML)
    root_ctx = engine.rootContext()
    main = MainWindow(root_ctx, engine)
    root_ctx.setContextProperty("backend", main)
    qmlRegisterType(LiveStream, "video", 1, 0, "LiveStream")
    engine.load(url)

On QML side i do this

function add_stream(camera, pane, pane_id) {
    Qt.createQmlObject(`
    import video 1.0
    LiveStream {
        width: ${pane.width}
        height: ${pane.height}
        stream_id: '${camera.id}'
        stream_name: '${camera.name}'
        anchors.fill: parent
    }`, pane);
}

I am very new to pyside or qt/qml so any help would be very nice.
Thanks

Solution

Add a Qt property to Livestream and reference this on qml side.

Answered By – Christoph

Answer Checked By – Gilberto Lyons (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *