python绘制饼状图qml显示示例2

发布一下 0 0
python绘制饼状图qml显示示例2

运行需要安装pyside6。

pip install pyside6 -i https://pypi.douban.com/simple/

创建main.py文件,python代码注释如下:

import osfrom pathlib import Pathimport sysfrom PySide6.QtCore import Property, Signal, Slot, Qt, QUrlfrom PySide6.QtGui import QGuiApplication, QPen, QPainter, QColorfrom PySide6.QtQml import qmlRegisterTypefrom PySide6.QtQuick import QQuickPaintedItem, QQuickViewclass PieChart(QQuickPaintedItem):#定义PieChart类    chartCleared = Signal()#清除chart信号    nameChanged = Signal()    chartView = Signal()#显示chart信号    def __init__(self, parent=None):        QQuickPaintedItem.__init__(self, parent)        self._name = u''        self._color = QColor()    def paint(self, painter):#绘图        pen = QPen(self.color, 2)        painter.setPen(pen)        painter.setRenderHints(QPainter.Antialiasing, True)        painter.drawPie(self.boundingRect().adjusted(1, 1, -1, -1), 90 * 16, 290 * 16)    @Property(QColor)    def color(self):        return self._color    @color.setter    def color(self, value):        self._color = value    @Property(str, notify=nameChanged)    def name(self):        return self._name    @name.setter    def name(self, value):        self._name = value    @Slot()      def clearChart(self):#清除chart        self.color = Qt.transparent#设置透明色Qt.transparent        self.update()        self.chartCleared.emit()    @Slot()      def chartView(self):#显示chart        self.color = QColor("red")#设置颜色        self.update()        self.chartView.emit()if __name__ == '__main__':    app = QGuiApplication(sys.argv)    qmlRegisterType(PieChart, 'Charts', 1, 0, 'PieChart')#注册类PieChart供qml使用    view = QQuickView()    view.setResizeMode(QQuickView.SizeRootObjectToView)    qml_file = os.fspath(Path(__file__).resolve().parent / 'app.qml')    view.setSource(QUrl.fromLocalFile(qml_file))    if view.status() == QQuickView.Error:        sys.exit(-1)    view.show()    res = app.exec()    del view    sys.exit(res)

创建app.qml界面文件,代码注释如下:

import Chartsimport QtQuickItem {    width: 300; height: 200    PieChart {        id: aPieChart        anchors.centerIn: parent        width: 100; height: 100        color: "red"        onChartCleared: console.log("The chart has been cleared")    }    MouseArea {        anchors.fill: parent        onClicked: aPieChart.clearChart()//单击清除显示饼图        onDoubleClicked: aPieChart.chartView()            }    Text {        anchors {            bottom: parent.bottom;            horizontalCenter: parent.horizontalCenter;            bottomMargin: 20        }        text: "单击任意位置清除图表\r\n双击任意位置显示图表"    }}
python绘制饼状图qml显示示例2

版权声明:内容来源于互联网和用户投稿 如有侵权请联系删除

本文地址:http://0561fc.cn/70144.html