Change the value property of the circular gauge

advertisements

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:

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

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

Thanks a lot for helping a noob :)

Actually having this python:

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 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()) RuntimeError: super-class init() of type Celsius was never called.

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


I am unfortunately not able to help you with the Python syntax but this is how that would look in 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();
};

Maybe someone with PytQt know-how can propose an edit or base their own answer on that.