Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Attach QMLComponent to QQuickWindow in PySide2
Forum Update on Monday, May 27th 2025

Attach QMLComponent to QQuickWindow in PySide2

Scheduled Pinned Locked Moved Solved QML and Qt Quick
pyside2qmlqqmlengineparent
3 Posts 2 Posters 439 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    J0Nes
    wrote on last edited by
    #1

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

    Rectangle{
        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)
    object.setParentItem(window.contentItem())
    object.setParent(engine)
    

    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!

    1 Reply Last reply
    0
    • ndiasN Offline
      ndiasN Offline
      ndias
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • J Offline
        J Offline
        J0Nes
        wrote on last edited by
        #3

        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())
        
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved