My approach was pretty much as such:
Chart.qml:
Window {
ChartView {
id: chartView
// ...
}
Button {
text: "save"
onClicked: manager.save(chartView)
}
}
Manager.py:
class Manager(QObject):
@pyqtSlot(QObject)
def save(self, chartview):
img = chartview.grab()
img.save(MY_DEFAULT_PATH)
The problem is, of course, that inside the Manager.save()
the chartview is equivalent of C++'s QObject*
which does not have the 'grab' method. But it could be downcasted to QChartView*
(or at least QWidget*
) which, such method, has. In C++ I would do it probably with qobject_cast.
My questions are:
- Is such downcasting doable in PyQt?
- If not - do you have any idea how could it be done differently?
Before giving the answer it is better to clarify some errors that the post has:
The QML ChartView is an Item unlike the QChartView which is a QWidget. And that Item is painted using the information from a QGraphicsScene and QChart. Considering the above, one method could be to access the QGraphicsScene and QChart, and use the
render()
method of QGraphicsScene.You can do casting using sip through the
sip.cast()
method.In this case, it is not necessary to do a downcasting either, since it is enough to indicate in the signature to:
But instead of getting too complicated you can use the grabToImage method since it is an Item: