2016-08-01 92 views
2

我想使用Python,PySide和一個示例QML文件來製作一個簡單的用戶界面。 如何從Python應用程序設置QML控件中的「value」屬性?截至目前,「SpeedDial」顯示,但無法弄清楚如何改變它的價值。從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 
      } 
     } 
    } 
} 
} 

回答

0

你不應該直接控制在Python或C QML特性++。

定義一個Python類,它將代表具有屬性speed的汽車。實例它QML這樣的:

Car { 
    id: car 
} 

,然後使用它的速度:

angle: car.speed 
+0

爲什麼不呢? 此外,官方文檔解釋[如何做到這一點](http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html) – Willy

+0

@Willy,數據可以以不同的方式查看,但通過調用QML從C++開始,它就變成了一種特定的表示形式。 Qt會議上有關於最佳QML實踐的視頻。 – Velkan