嗨,我知道这是一个古老而简单的问题,但我真的不明白。如何从python动态地将圆形量规的value属性更改为qml文件?我尝试了很多,但在开始时又站了起来。因为我对QT很新,Python有人可以解释我该怎么办?我在这里复制了qml和空的python文件:
的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" : "")
}
非常感谢帮助一个菜鸟:)
实际上有这个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是一样的。如果我打印rotatevalue.temperature,我在这个变量中有正确的值,但是与qml的连接仍然是个问题。 Python在运行以下内容时说:
root_context = view.rootContext()。setContextProperty(“valuepy”,Celsius()) RuntimeError:从未调用Celsius类型的超类 init ()。
价值不在我的指标中。有什么想法吗?
答案 0 :(得分:0)
遗憾的是,我无法帮助您使用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技术诀窍的人可以提出编辑或建立自己的答案。
答案 1 :(得分:0)
对于任何有同样问题的人,我发现,这里的代码似乎是正确的,非常感谢所有的帮助:
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_())