How Can I Update a Qml Object's Property from my Python file?

前端 未结 2 1876
轻奢々
轻奢々 2020-12-06 19:18

I want to show a rectangle in Qml and I want to change the rectangle\'s properties(width, length) from my python code. In fact, there is a socket connection in the python co

2条回答
  •  再見小時候
    2020-12-06 19:38

    Try some thing like below (Not tested, but will give you an idea).

    create some objectname for rectangle as shown below:

    Rectangle {
            id: rectangle
            x: 187
            y: 92
            width: 200
            height: 200
            color: "blue"
            objectName: "myRect"
        }
    

    Interact with QML and find your child, then set the property.

        #INTERACT WITH QML
        engine = QQmlEngine()
        component = QQmlComponent(engine)
        component.loadUrl(QUrl('mainViewofHoomanApp.qml'))
        object = component.create()
    
        #FIND YOUR RECTANGLE AND SET WIDTH
        child = object.findChild(QObject,"myRect")
        child.setProperty("width", 500)  
    

提交回复
热议问题