簡體   English   中英

使用pyqt5更改CircularGauge

[英]Using pyqt5 to change a CircularGauge

好的,我設法使用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_())

現在我得到的是:

屏幕輸出

顯然,我的代碼可以正確繪制圖片,但是我沒有更改代碼的值。 從我在這篇文章中所講的,我可能需要定義一個類? 抱歉,還是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()

我在這里想念什么?

好的,正如我已經說過的,您正在更改錯誤項目的屬性。 實際上,您試圖更改黑色矩形巫婆的屬性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
}

好的,我想出了如何更改該值。 一個問題是拼寫錯誤,但否則似乎可行。

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
                }
           }
      }
 }
}

蟒蛇

#!/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_())

現在,我可以設置儀表的“值”。

圖片顯示了它的工作原理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM