更改圆形仪表的值属性 [英] Change value property of Circular gauge

查看:85
本文介绍了更改圆形仪表的值属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个古老而简单的问题,但我真的不明白.如何将圆形仪表的 value 属性从 python 动态更改为 qml 文件?我尝试了很多,但在开始时又站了起来.因为我对 QT 和 Python 很陌生,有人可以解释我该怎么做吗?我在这里复制了 qml 和空的 python 文件:

Hi I know this is a old and easy question, but I really dont get it. How can I change the value property of my circular gauge dynamicly from python to the qml file? I tried alot but standing again at the beginning. Because I am very new to QT and Python can somebody explain me how to do? I copied the qml and the empty python file here:

蟒蛇:

import sys
from PyQt5.QtCore import QObject, QUrl, Qt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5 import QtCore, QtGui



if __name__ == "__main__":
  app = QApplication(sys.argv)
  engine = QQmlApplicationEngine()
  engine.load('dashboard.qml')
  win = engine.rootObjects()[0]
  win.textUpdated.connect(show)
  win.show()
  sys.exit(app.exec_())

还有 QML:

 CircularGauge {
                    value: 66  **(Thats the value I want to change from Python)**
                    maximumValue: 1 
                    width: parent.width
                    height: parent.height * 0.7
                    y: parent.height / 2 + container.height * 0.01

                style: IconGaugeStyle {
                    id: tempGaugeStyle

                    icon: "qrc:/images/temperature-icon.png"
                    maxWarningColor: Qt.rgba(0.5, 0, 0, 1)

                    tickmarkLabel: Text {
                        color: "white"
                        visible: styleData.value === 0 ||     styleData.value === 1
                        font.pixelSize: tempGaugeStyle.toPixels(0.225)
                        text: styleData.value === 0 ? "C" : (styleData.value === 1 ? "H" : "")
                    }

非常感谢帮助菜鸟:)

实际上拥有这条蟒蛇:

class Celsius(QObject):
    def __init__(self, temperature = 0.6):
        self._temperature = temperature


    @property
    def temperature(self):
        print("Getting value")
        return self._temperature

     @temperature.setter
     def temperature(self, value):
        if value < -273:
            raise ValueError("Temperature below -273 is not possible")
        print("Setting value")
        self._temperature = value

rotatevalue = Celsius()
print(rotatevalue.temperature)


if __name__ == "__main__":
  app = QApplication(sys.argv)
  engine = QQmlApplicationEngine()
  engine.load('dashboard.qml')

  view = QQuickView()
  root_context = view.rootContext().setContextProperty("valuepy", Celsius())
  win = engine.rootObjects()[0]
  win.textUpdated.connect(show)
  win.show()
  sys.exit(app.exec_())

QML 是一样的.如果我打印rotatevalue.temperature,我在这个变量中有正确的值,但与qml的连接仍然是一个问题.Python 在运行时说:

QML is the same. If I print rotatevalue.temperature, I have the right value in this variable but the connection to the qml is still a problem. Python says on running following:

root_context = view.rootContext().setContextProperty("valuepy",Celsius())运行时错误:从未调用过摄氏类型的超类 init().

root_context = view.rootContext().setContextProperty("valuepy", Celsius()) RuntimeError: super-class init() of type Celsius was never called.

而且该值不在我的量表中.有什么想法吗?

And the value is not in my gauge. Any ideas?

推荐答案

对于任何有同样问题的人,我发现,这里的代码似乎是正确的,非常感谢所有的帮助:

For anybode having the same problem, I found out, here is the code which seems to go right, thanks a lot for all the helping:

class Celsius(QObject):
    def __init__(self, parent=None):
            super().__init__(parent)
            self.temperature = 0.3


    @pyqtProperty(int)
    def temperature(self):
        return self._temperature

    # Define the setter of the 'temperature' property.
    @temperature.setter
    def temperature(self, temperature):
        self._temperature = temperature

rotatevalue = Celsius()
print(rotatevalue.temperature)
rot = rotatevalue.temperature


if __name__ == "__main__":

  app = QApplication(sys.argv)
  qmlRegisterType(Celsius, 'Celsius', 1, 0, 'Celsius')
  view = QQuickView()
  engine = QQmlApplicationEngine()
  engine.rootContext().setContextProperty('valuepy', rot)
  engine.load('dashboard.qml')
  win = engine.rootObjects()[0]
  win.show()
  sys.exit(app.exec_())

这篇关于更改圆形仪表的值属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆