【问题标题】:setting qml property from Python?从 Python 设置 qml 属性?
【发布时间】:2016-08-01 18:34:07
【问题描述】:

我正在尝试使用 Python、PySide 和一个示例 QML 文件来制作一个简单的 ui。 如何从我的 Python 应用程序设置 QML 控件中可用的“值”属性?截至目前,“快速拨号”出现了,但不知道如何更改它的值。

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
            }
        }
    }
}
}

【问题讨论】:

    标签: python qt qml pyside


    【解决方案1】:

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

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

    Car {
        id: car
    }
    

    然后使用它的速度:

    angle: car.speed 
    

    【讨论】:

    • 为什么不呢?此外,官方文档解释了how to do that
    • @Willy,可以以不同的方式查看数据,但是通过从 C++ 调用 QML,它会耦合到一种特定的表示形式。 Qt 会议上有一段关于最佳 QML 实践的视频。
    猜你喜欢
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 2013-12-23
    • 2014-10-25
    • 1970-01-01
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多