Qt World Summit: Register Today!

Attach QMLComponent to QQuickWindow in PySide2

  • Hey,
    running Qt 5.15.2 (PySide2) I am trying to attach a QML Component as simple as

        color: "red"
        width: 100
        height: 100
        objectName: "MY RECT"
        Component.onCompleted: console.log("created")

    to my main window in Python 3. From my QQMLApplicationEngine I get my QQuickWindow:

    window = QQuickWindow(engine.rootObjects()[0])

    Then, I load my QMLComponent and create it:

    component = QQmlComponent(engine, QUrl(".../dialog.qml"))
    object = QQuickItem(component.create())
    QQmlEngine.setObjectOwnership(object, QQmlEngine.CppOwnership)

    I see that "created" is printed as expected but my rectangle is not showing up anywhere.
    It works by just adding the Rectangle to my main window QML directly so something is fishy when calling this code.
    Any clues what I am missing here?
    Thanks a lot!

  • Hi @J0Nes ,
    Can you clarify you are trying to attach your QMLComponent to a QQuickWindow or a QMainWindow?

  • Hey @ndias,
    I am trying to attach to a QQuickWindow.
    After some more try & error I finally managed to get it working by using createWithInitialProperties like so:

    initialProperties = {"parent": window.contentItem()}
    object = component.createWithInitialProperties(initialProperties, engine.rootContext())