圆形仪表的变化值属性(Change value property of Circular gauge

2019-09-30 10:31发布

您好我知道这是一个古老而简单的问题,但我真的不明白这一点。 如何动态地我从蟒蛇改变我的圆形仪表的价值属性的QML文件? 我想了很多,但在开始重新站。 因为我很新的Qt和Python可以有人解释我该怎么办? 我复制了QML和空Python文件在这里:

蟒蛇:

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" : "")
                    }

非常感谢帮助一个noob :)

其实有这条巨蟒:

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的连接仍然是一个问题。 蟒蛇说,上运行以下内容:

root_context = view.rootContext()setContextProperty( “valuepy”,摄氏())RuntimeError:超类的init(摄氏类型的)从未被调用。

和值是不是在我的计。 有任何想法吗?

Answer 1:

我很遗憾没能帮助你的Python语法,但是这是怎么会看在C ++

class Celsius : public QObject
{
    Q_OBJECT
    Q_PROPERTY(double temperature READ temperature WRITE setTemperature NOTIFY temperatureChanged)

public:
    explicit Celsius(double temperature = 0.6, QObject *parent = 0)
        : QObject(parent), m_temperature(temperature) {}

    double temperature() const { return m_temperature; }

    void setTemperature(double temperature) {
        if (temperature == m_temperature) return;

        if (temperature < -273) return;

        m_temperature = temperature;
        emit temperatureChanged();
    }

signals:
    void temperatureChanged();
};

也许有人PytQt诀窍可以提出一个编辑或立足于他们自己的答案。



Answer 2:

对于有同样的问题anybode,我才发现,这里似乎走右边的代码,非常感谢所有的帮扶:

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_())


文章来源: Change value property of Circular gauge