从Python设置QML属性?

3

我想使用Python、PySide和一个示例QML文件制作一个简单的用户界面。

如何从我的Python应用程序中设置QML控件中可用的"value"属性?目前,"SpeedDial"出现了,但无法弄清如何更改它的值。

Python文件:

import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtDeclarative import QDeclarativeView

# Create Qt application and the QDeclarative view
app = QApplication(sys.argv)
view = QDeclarativeView()
# Create an URL to the QML file
url = QUrl('SpeedDial.qml')

# Set the QML file and show
view.setSource(url)
view.show()
# Enter Qt main loop
sys.exit(app.exec_())
QML文件:
import QtQuick 1.0

Item  {
id: root1
property real value : 0

width: 300; height: 300

Image  { id: speed_inactive; x: -9; y: 8; opacity: 0.8; z: 3; source: "pics/speed_inactive.png"

}
Image  {
    id: needle
    x: 136; y: 86
    clip: true
    opacity: root1.opacity
    z: 3
    smooth: true
    source: "pics/needle.png"
    transform: Rotation  {
        id: needleRotation
        origin.x: 5; origin.y: 65
        angle: Math.min(Math.max(-130, root1.value*2.6 - 130), 133)

        Behavior on angle  {
            SpringAnimation  {
                spring: 1.4
                damping: .15
            }
        }
    }
}
}
1个回答

0

不应该直接从Python或C++控制QML属性。

定义一个Python类,表示具有属性speed的汽车。在QML中实例化它:

Car {
    id: car
}

然后利用它的速度:

angle: car.speed 

为什么不呢? 此外,官方文档解释了如何做到这一点 - Willy
@Willy,数据可以以不同的方式进行查看,但是通过从C++调用QML,它就与一个特定的表示形式耦合在一起了。Qt大会上有一个关于最佳QML实践的视频。 - Velkan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接