好的,我已经设法使用PyQt5和QML进行CircularGauge加载。这是我的QML:
import QtQuick 2.0
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import QtQuick.Extras 1.4
import QtQuick.Extras.Private 1.0
Rectangle {
width: 300
height: 300
color: "#000000"
CircularGauge {
property real gauge_value: 10000.0
anchors.centerIn: parent
value: gauge_value
maximumValue: 10000.0 // Largest Value
minimumValue: 0.0 // Smallest Value
style: CircularGaugeStyle {
id: style
tickmarkStepSize: 1000.0 // Tick Marks
tickmark: Rectangle {
visible: styleData.value < 8000 || styleData.value % 1000 == 0
implicitWidth: outerRadius * 0.02
antialiasing: true
implicitHeight: outerRadius * 0.06
color: styleData.value >= 8000 ? "#ff0000" : "#ff0000"
}
minorTickmark: Rectangle {
visible: styleData.value < 8000
implicitWidth: outerRadius * 0.01
antialiasing: true
implicitHeight: outerRadius * 0.03
color: "#ff0000"
}
tickmarkLabel: Text {
font.pixelSize: Math.max(6, outerRadius * 0.1)
text: styleData.value
color: styleData.value >= 8000 ? "#ff0000" : "#ff0000"
antialiasing: true
}
needle: Rectangle {
y: outerRadius * 0.15
implicitWidth: outerRadius * 0.03
implicitHeight: outerRadius * 1.1
antialiasing: true
color: "#ff0000"
}
foreground: Item {
Rectangle {
width: outerRadius * 0.2
height: width
radius: width / 2
color: "#b2b2b2"
anchors.centerIn: parent
}
}
}
}
}
现在是我的Python脚本
import sys
import time
from PyQt5.QtCore import QObject, QUrl, Qt, pyqtProperty
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine, qmlRegisterType
from PyQt5 import QtCore, QtGui
from PyQt5.QtQuick import QQuickView
if __name__ == "__main__":
app = QApplication(sys.argv)
view = QQuickView()
view.setSource(QUrl('full_gauge.qml'))
engine = view.engine()
rot = 4000.0
engine.rootContext().setContextProperty('gauge_value', rot)
view.show()
rot = 0.0
engine.rootContext().setContextProperty('gauge_value', rot)
view.update()
view.show()
sys.exit(app.exec_())
现在我得到的是:
显然我的代码正确地绘制了图片,但我没有改变代码的值。从我可以告诉我post我可能需要定义一个类?对不起还是一点Python新手。真正伟大的是,如果我可以通过各种值来获得仪表周期。
更新: 所以从其他帖子我发现我需要上课:
class Tachometer(QObject):
def __init__(self, parent=None):
super().__init__(parent)
# Initialise the value of the properties.
self._rpm_value = rpm_value
# Define the getter of the 'rpm_value' property. The C++ type and
# Python type of the property is float.
@pyqtProperty(float)
def rpm_value(self):
print("Getting value")
return self._rpm_value
# Define the setter of the 'rpm_value' property.
@rpm_value.setter
def rpm_value(self, value):
print("Setting value")
self._rpm_value = value
现在不知怎的,我需要将它与我的QML联系起来。但是,我在这里感到困惑。首先,我需要将其添加到我的QML中吗?其次,我如何将其与我的代码联系起来,我最好的尝试是:
if __name__ == "__main__":
# Create the application instance
app = QApplication(sys.argv)
# Register the Python type. Its URI is 'Tachometer', it's v1.0 and the type
# will be called 'Tachometer' in QML.
qmlRegisterType(Tachometer, 'Tachometer', 1, 0, 'Tachometer')
# Create a QML engine.
view = QQuickView()
engine = view.engine()
# Create a component factory and load the QML script.
component = QQmlComponent(engine)
#component.loadUrl(QUrl('full_gauge.qml'))
view.setSource(QUrl('full_gauge.qml'))
# draw the window
view.show()
我在这里缺少什么?
答案 0 :(得分:1)
好的,正如我已经说过的那样,你正在改变一个错误项目的属性。实际上,你试图改变黑色矩形的属性gauge_value
女巫实际上是根项目,并且根本没有这样的属性。
我不知道如何在Python中这样做但在C ++中我会这样做:
QObject *root = dynamic_cast<QObject *>(engine.rootObjects().first());
QObject *gauge = root->findChild<QObject *>("gauge");
gauge->setProperty("gauge_value",5000);
您的CircularGauge
应该是这样的:
CircularGauge {
objectName: "gauge" // << add this
property real gauge_value: 10000.0
// the code here
}
答案 1 :(得分:0)
好的,我想出了如何更改价值。一个问题是拼写错误,但否则这似乎有效。
QML
import QtQuick 2.0
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import QtQuick.Extras 1.4
import QtQuick.Extras.Private 1.0
Rectangle {
width: 300
height: 300
color: "#000000"
CircularGauge {
objectName: "test_gauge"
property real gauge_value: 10000.0
anchors.centerIn: parent
value: gauge_value
maximumValue: 10000.0 // Largest Value
minimumValue: 0.0 // Smallest Value
style: CircularGaugeStyle {
id: style
tickmarkStepSize: 1000.0 // Tick Marks
tickmark: Rectangle {
visible: styleData.value < 8000 || styleData.value % 1000 == 0
implicitWidth: outerRadius * 0.02
antialiasing: true
implicitHeight: outerRadius * 0.06
color: styleData.value >= 8000 ? "#ff0000" : "#ff0000"
}
minorTickmark: Rectangle {
visible: styleData.value < 8000
implicitWidth: outerRadius * 0.01
antialiasing: true
implicitHeight: outerRadius * 0.03
color: "#ff0000"
}
tickmarkLabel: Text {
font.pixelSize: Math.max(6, outerRadius * 0.1)
text: styleData.value
color: styleData.value >= 8000 ? "#ff0000" : "#ff0000"
antialiasing: true
}
needle: Rectangle {
y: outerRadius * 0.15
implicitWidth: outerRadius * 0.03
implicitHeight: outerRadius * 1.1
antialiasing: true
color: "#ff0000"
}
foreground: Item {
Rectangle {
width: outerRadius * 0.2
height: width
radius: width / 2
color: "#b2b2b2"
anchors.centerIn: parent
}
}
}
}
}
的Python
#!/usr/bin/env python3
import sys
import time
from PyQt5.QtCore import QObject, QUrl, Qt, pyqtProperty, pyqtSignal
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine, qmlRegisterType, QQmlEngine, QQmlComponent
from PyQt5 import QtCore, QtGui
from PyQt5.QtQuick import QQuickView
if __name__ == "__main__":
app = QApplication(sys.argv)
view = QQuickView()
view.setSource(QUrl('full_gauge.qml'))
gauge=view.findChild(QObject,'test_gauge')
gauge.setProperty('gauge_value',4500)
view.show()
sys.exit(app.exec_())
现在我可以设置&#39;值&#39;规格。