Last active
February 15, 2022 18:34
-
-
Save keithel/e0b47eaedeecfa2bab77756842bbed79 to your computer and use it in GitHub Desktop.
Access a ChartView's series in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
import sys | |
from PySide6.QtCore import QUrl, QObject, Property, Signal, Slot | |
from PySide6.QtWidgets import QApplication | |
from PySide6.QtQml import QmlElement, QQmlApplicationEngine | |
from PySide6.QtCharts import QAbstractSeries | |
QML_IMPORT_NAME = "name.kyzivat.piechart_example" | |
QML_IMPORT_MAJOR_VERSION = 1 | |
@QmlElement | |
class ChartHandler(QObject): | |
all_series_changed = Signal() | |
def __init__(self): | |
super().__init__() | |
self._all_series = [] | |
def get_all_series(self): | |
return self._all_series | |
@Slot(QAbstractSeries) | |
def addSeries(self, series): | |
self._all_series.append(series) | |
self.all_series_changed.emit() | |
@Slot() | |
def printSeriesInfo(self): | |
for i,s in enumerate(self._all_series): | |
print(f"Series {i} {type(s)}:") | |
for j in range(s.count()): | |
print(f"Slice label: {s.slices()[j].label()}") | |
all_series = Property("QVariantList", get_all_series, notify=all_series_changed) | |
if __name__ == '__main__': | |
app = QApplication(sys.argv) | |
engine = QQmlApplicationEngine() | |
qml_object_created = [False] | |
def handle_qml_load_errors(obj, _url): | |
qml_object_created[0] = obj is not None | |
engine.objectCreated.connect(handle_qml_load_errors) # pylint: disable=no-member | |
qml_file = Path(__file__).parent / "piechart.qml" | |
engine.load(QUrl.fromLocalFile(qml_file)) | |
if not qml_object_created[0]: | |
sys.exit(1) | |
app_errcode = app.exec() | |
sys.exit(app_errcode) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import QtQuick | |
import QtQuick.Controls | |
import QtCharts | |
import name.kyzivat.piechart_example | |
ApplicationWindow { | |
width: 400 | |
height: 300 | |
visible: true | |
ChartView { | |
id: chart | |
anchors.fill: parent | |
antialiasing: true | |
PieSeries { | |
id: pieSeries | |
startAngle: 0 | |
endAngle: 180 | |
PieSlice { label: "eaten"; value: 44.9 } | |
PieSlice { label: "not yet eaten"; value: 5.1 } | |
} | |
PieSeries { | |
id: pieSeries2 | |
startAngle: 180 | |
endAngle: 360 | |
PieSlice { label: "full"; value: 3.33; | |
} | |
PieSlice { label: "not full"; value: 6.67; | |
} | |
} | |
ChartHandler { | |
id: chartHandler | |
} | |
Component.onCompleted: { | |
for (var i = 0; i < count; i++) | |
chartHandler.addSeries(series(i)) | |
} | |
} | |
MouseArea { | |
anchors.fill: parent | |
onClicked: { | |
chartHandler.printSeriesInfo(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment