Python 圆规的变值特性
嗨,我知道这是一个古老而简单的问题,但我真的不明白。如何将圆形仪表的value属性从python动态更改为qml文件?我试了很多,但一开始又站了起来。因为我对QT和Python非常陌生,有人能告诉我怎么做吗?我在这里复制了qml和空python文件: Python:Python 圆规的变值特性,python,qt,pyqt5,gauge,Python,Qt,Pyqt5,Gauge,嗨,我知道这是一个古老而简单的问题,但我真的不明白。如何将圆形仪表的value属性从python动态更改为qml文件?我试了很多,但一开始又站了起来。因为我对QT和Python非常陌生,有人能告诉我怎么做吗?我在这里复制了qml和空python文件: Python: import sys from PyQt5.QtCore import QObject, QUrl, Qt from PyQt5.QtWidgets import QApplication from PyQt5.QtQml impo
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:)
实际上有这样一条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_())
摄氏度等级(QObject):
def _初始(自身,温度=0.6):
自身温度=温度
@财产
def温度(自身):
打印(“获取价值”)
返回自身温度
@温度调节器
def温度(自身、数值):
如果值<-273:
提升值错误(“温度不可能低于-273”)
打印(“设置值”)
自身温度=数值
旋转值=摄氏度()
打印(旋转值温度)
如果名称=“\uuuuu main\uuuuuuuu”:
app=QApplication(sys.argv)
引擎=QQmlApplicationEngine()
engine.load('dashboard.qml')
视图=QQuickView()
root_context=view.rootContext().setContextProperty(“valuepy”,摄氏度())
win=engine.rootObjects()[0]
win.textUpdated.connect(显示)
win.show()
sys.exit(app.exec_())
QML是相同的。如果打印rotatevalue.temperature,则此变量中的值是正确的,但与qml的连接仍然存在问题。Python在运行以下命令时说:
root_context=view.rootContext().setContextProperty(“valuepy”,摄氏度())
运行时错误:从未调用Celsius类型的超级类init()
这个值不在我的仪表里。有什么想法吗?很遗憾,我无法帮助您使用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();
};
类别:公共QObject
{
Q_对象
Q_属性(双温度读取温度写入设置温度通知温度更改)
公众:
显式摄氏度(双倍温度=0.6,QObject*父对象=0)
:QObject(父对象),m_温度(温度){}
双温度()常数{返回m_温度;}
空隙设定温度(双温){
如果(温度==m_温度)返回;
如果(温度<-273)返回;
m_温度=温度;
发射温度更改();
}
信号:
无效温度更改();
};
也许有人拥有PytQt的专业知识,可以提出一个编辑建议,或者根据自己的答案来回答。很遗憾,我无法帮助您使用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();
};
类别:公共QObject
{
Q_对象
Q_属性(双温度读取温度写入设置温度通知温度更改)
公众:
显式摄氏度(双倍温度=0.6,QObject*父对象=0)
:QObject(父对象),m_温度(温度){}
双温度()常数{返回m_温度;}
空隙设定温度(双温){
如果(温度==m_温度)返回;
如果(温度<-273)返回;
m_温度=温度;
发射温度更改();
}
信号:
无效温度更改();
};
也许拥有PytQt专业知识的人可以提出一个编辑建议,或者根据自己的答案来回答。对于任何有相同问题的人,我发现,下面是代码,似乎是正确的,非常感谢所有的帮助:
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_())
对于任何有同样问题的人,我发现,下面是代码,似乎是正确的,非常感谢所有的帮助:
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_())
在C++中,您将创建一个<代码> QObjult< /Cord>派生类,该类具有与QML元素的
engine.rootContext().setContextProperty()
导出该类的一个实例,我假设这也适用于Python,只是不确定属性的语法感谢Krammer先生的帮助,但我可以做我想做的,不能让它工作,有没有人有一个简单的例子可以将Python中的值写入qml?重要的部分不是“写入qml”,而是在qml端公开一个属性并绑定到该属性。我发布了一个编辑我的问题,问题是如何做到:)啊,对不起,没有看到。我假设您的QML现在像是代码>值:ValuePy。温度<代码>?在C++中,您将创建一个<代码> QObjult< /Cord>派生类,该类具有一个与QML元素的engine.rootContext().setContextProperty()
导出该类的一个实例,我假设这也适用于Python,只是不确定属性的语法感谢Krammer先生的帮助,但我可以做我想做的,不能让它工作,有没有人有一个简单的例子可以将Python中的值写入qml?重要的部分不是“写入qml”,而是在qml端公开一个属性并绑定到该属性。我发布了一个编辑我的问题,问题是如何做到:)啊,对不起,没有看到。我假设您的QML现在类似于value:valuepy.temperature
?这不仅仅是导出“rot”的单个值吗?也就是说,如果您更改rotatevalue
的属性,QML中的值是否会更改?您的意思是,它只给QML一次值,并且在程序运行和值更改时不更新?是的,它看起来是这样的,但是python可能会在这里做不同的事情。我们会尽快检查并在这里给出评论。这不仅仅是导出“rot”的单个值吗?也就是说,如果您更改rotatevalue
的属性,QML中的值是否会更改?您的意思是,当程序运行且值更改时,它只给QML一次值,而不更新?是的,它看起来是这样的,但是python可能会在这里做不同的事情。我们将尽快检查并在此处给出注释。