Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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 QtQuick

    but 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.


  • Lifetime Qt Champion

    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.


  • Lifetime Qt Champion

    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.



  • I would like to migrate from QtWidgets to QML, so I'm looking for the best choice how to do this with part of my app which lays upon QGraphicsScene.


  • Lifetime Qt Champion

    Then,, AFAIK, without reimplementing everything, the QQuickItem way will likely be the simplest.



  • Do You mean this one with rendering QImage into texture?


  • Lifetime Qt Champion

    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.



  • Ok, I understood a point but I'm mixing terminology...
    Thank You a lot for the answer.



  • I tested it with different options what brought me to following:
    Using QQuickPaintedItem and painting scene content (or its part) in overloaded paint() method gives the same effects like rendering to QImage 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


  • Lifetime Qt Champion

    Great !

    Thanks for the feedback.


Log in to reply