should help you out The QML Charts API is written based on the API used by C++ but it is not the same, for example the ChartView is a QQuickItem that does not expose the QChart unlike QChartView is a QGraphicsView(QWidget) that if it exposes the QChart, what Same is with the series. In conclusion you will not be able to use C++(Python) classes to interact with QML.
The example you show at the beginning is not an example for C++ but for QML so it cannot be translated directly into QML. It is also not possible to create a QML Series in C++/Python with QtCharts classes directly, a possible strategy is to use QQmlExpression that can evaluate QML elements and return it to C++/Python. In addition, the createSeries() method not only adds the series but also connects signals.
from enum import Enum, auto
from PySide2 import QtCore, QtGui, QtWidgets, QtQml
# https://code.qt.io/cgit/qt/qtcharts.git/tree/src/chartsqml2/declarativechart_p.h#n105
class SeriesType(Enum):
SeriesTypeLine = 0
SeriesTypeArea = auto()
SeriesTypeBar = auto()
SeriesTypeStackedBar = auto()
SeriesTypePercentBar = auto()
SeriesTypePie = auto()
SeriesTypeScatter = auto()
SeriesTypeSpline = auto()
SeriesTypeHorizontalBar = auto()
SeriesTypeHorizontalStackedBar = auto()
SeriesTypeHorizontalPercentBar = auto()
SeriesTypeBoxPlot = auto()
SeriesTypeCandlestick = auto()
class DataModel(QtCore.QObject):
def __init__(self, engine, parent=None):
super().__init__(parent)
self.m_engine = engine
@QtCore.Slot(QtCore.QObject, QtCore.QObject, QtCore.QObject, result=QtCore.QObject)
def addChartSeries(self, chart_view, chart_axis_x, chart_axis_y):
context = QtQml.QQmlContext(self.m_engine.rootContext())
context.setContextProperty("chart_view", chart_view)
context.setContextProperty("axis_x", chart_axis_x)
context.setContextProperty("axis_y", chart_axis_y)
context.setContextProperty("type", SeriesType.SeriesTypeScatter.value)
script = """chart_view.createSeries(type, "scatter series", axis_x, axis_y);"""
expression = QtQml.QQmlExpression(context, chart_view, script)
serie, valueIsUndefined = expression.evaluate()
if expression.hasError():
print(expression.error())
return
import random
mx, Mx = chart_axis_x.property("min"), chart_axis_x.property("max")
my, My = chart_axis_y.property("min"), chart_axis_y.property("max")
if not valueIsUndefined:
for _ in range(100):
x = random.uniform(mx, Mx)
y = random.uniform(my, My)
serie.append(x, y)
# https://doc.qt.io/qt-5/qml-qtcharts-scatterseries.html#borderColor-prop
serie.setProperty("borderColor", QtGui.QColor("salmon"))
# https://doc.qt.io/qt-5/qml-qtcharts-scatterseries.html#brush-prop
serie.setProperty("brush", QtGui.QBrush(QtGui.QColor("green")))
# https://doc.qt.io/qt-5/qml-qtcharts-scatterseries.html#borderColor-prop
serie.setProperty("borderWidth", 4.0)
return serie
if __name__ == "__main__":
import os
import sys
current_dir = os.path.dirname(os.path.realpath(__file__))
app = QtWidgets.QApplication(sys.argv)
engine = QtQml.QQmlApplicationEngine()
dataModel = DataModel(engine)
engine.rootContext().setContextProperty("dataModel", dataModel)
file = os.path.join(current_dir, "main.qml")
engine.load(QtCore.QUrl.fromLocalFile(file))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
import QtQuick 2.12
import QtQuick.Controls 2.12
import QtCharts 2.2
ApplicationWindow {
visible: true
width: 640
height: 480
ChartView {
anchors.fill: parent
id: bscan0
ValueAxis {
id: bscan0_xAxix
min: 0
max: 10
}
ValueAxis {
id: bscan0_yAxis
min: -100
max: 100
}
Component.onCompleted: {
var serie = dataModel.addChartSeries(bscan0, bscan0_xAxix, bscan0_yAxis)
}
}
}
import random
from PySide2 import QtCore, QtGui, QtWidgets, QtQml, QtCharts
class DataModel(QtCore.QObject):
@QtCore.Slot(QtCharts.QtCharts.QAbstractSeries)
def fill_serie(self, serie):
mx, Mx = 0, 10
my, My = -100, 100
for _ in range(100):
x = random.uniform(mx, Mx)
y = random.uniform(my, My)
serie.append(x, y)
# https://doc.qt.io/qt-5/qml-qtcharts-scatterseries.html#borderColor-prop
serie.setProperty("borderColor", QtGui.QColor("salmon"))
# https://doc.qt.io/qt-5/qml-qtcharts-scatterseries.html#brush-prop
serie.setProperty("brush", QtGui.QBrush(QtGui.QColor("green")))
# https://doc.qt.io/qt-5/qml-qtcharts-scatterseries.html#borderColor-prop
serie.setProperty("borderWidth", 4.0)
if __name__ == "__main__":
import os
import sys
current_dir = os.path.dirname(os.path.realpath(__file__))
app = QtWidgets.QApplication(sys.argv)
engine = QtQml.QQmlApplicationEngine()
dataModel = DataModel(engine)
engine.rootContext().setContextProperty("dataModel", dataModel)
file = os.path.join(current_dir, "main.qml")
engine.load(QtCore.QUrl.fromLocalFile(file))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
import QtQuick 2.12
import QtQuick.Controls 2.12
import QtCharts 2.2
ApplicationWindow {
visible: true
width: 640
height: 480
ChartView {
anchors.fill: parent
id: bscan0
ValueAxis {
id: bscan0_xAxix
min: 0
max: 10
}
ValueAxis {
id: bscan0_yAxis
min: -100
max: 100
}
Component.onCompleted: {
var serie = bscan0.createSeries(ChartView.SeriesTypeScatter, "scatter series", bscan0_xAxix, bscan0_yAxis);
dataModel.fill_serie(serie)
}
}
}