Rendering QGraphicsScene into QML scene texture
-
Hi All,
Happy New Year!There was a post about integration QGraphicsView into newer QtQuick
(or rather about lack of the integration and necessity of migration):
https://forum.qt.io/topic/62637/how-to-integrate-qgraphicsview-with-qml
There are mentioned 4 ways how to migrate from QGraphicsView to QtQuickbut I've just discovered another one: to render QGraphicsScene into QML texture.
It is "widely" used by Qt Chart module:
https://github.com/qt/qtcharts/blob/dev/src/chartsqml2/declarativechart.cpp
(starts from line 584)
but this is only example I found.Could someone describe it more. Some general concept of that.
-
Hi,
The general concept is that the scene is rendered in a QImage that is then converted to a texture to be used by the node.
See the renderScene and updatePaintNode functions.
Hope it helps
-
@SGaist thanks for the answer.
I'm asking because I have already implemented a complex scene with many elements (font glyphs, lines), let's say similar to linear chart with a few series of data.
Porting it to QML canvas seems to be heavy job and using javascript to manage logic rather has performance impact whether I have logic already written in C++.
Because managing those elements requires operating on flat pane coordinates, using other techniques mentioned in above post (QML native elements or reimplementing QQuickItem or QQuicaPainted item) seems to be not my case.But please correct me if I'm wrong.
-
I'm not sure I'm following you. From what you described, following the technique of the DeclarativeChart class will likely be the simple road since you have everything already working with a QGraphicsScene.
-
Then,, AFAIK, without reimplementing everything, the QQuickItem way will likely be the simplest.
-
You don't render the QImage to a texture, your render your scene on a QImage and then you make a texture of it. Qt already supports creating texture from QImage.
-
I tested it with different options what brought me to following:
UsingQQuickPaintedItem
and painting scene content (or its part) in overloadedpaint()
method gives the same effects like rendering toQImage
and using it as a texture.
But when QQuickPaintedItem has set:setRenderTarget(QQuickPaintedItem::FramebufferObject);
to drawing directly using GL paint engine- it is much faster (2-3 times)
so:void MyQuickPaintedItem::paint(QPainter* painter) { graphicsScene->render(painter, boundingRect(), boundingRect()); }
Does its job well
-
Great !
Thanks for the feedback.