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

Attach QMLComponent to QQuickWindow in PySide2

Scheduled Pinned Locked Moved Solved QML and Qt Quick
pyside2qmlqqmlengineparent
3 Posts 2 Posters 433 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 21 Jul 2021, 15:34 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
    • N Offline
      N Offline
      ndias
      wrote on 22 Jul 2021, 09:11 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 22 Jul 2021, 09:37 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

        1/3

        21 Jul 2021, 15:34

        • Login

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