Drawing to QtQuick Canvas from cpp



  • Hi, I created UI for my app using QtQuick.
    I need canvas for my gfx output so I placed QtQuick Canvas object to my QML file.
    I was able to connect QtQuick Canvas.paint signal to my cpp CCanvas.onPaint slot.
    Now I do not know how to get QtQuick Context2d equivalent in cpp.
    I found QPainter in documentation and it looks good.
    Can I get QPainter for QtQuick Canvas?
    I need context where I can draw lines, polygons, etc. (something as cairo context).



  • The canvas is specifically designed for convenient use within javascript. If you are in C++ you should instead use QQuickPaintedItem which specifically provides the same functionality through QPainter instead.



  • Thank you!
    I derived my class from QQuickPaintedItem and it works.
    However I found this statement in documentation:

    bq. QQuickPaintedItem is meant to make it easier to port old code that is using the QPainter API to the QML Scene Graph API and it should be used only for that purpose.bq.

    Does it mean I should use completely different approach for new application?



  • Not at all, you can safely use it. We have in fact removed that specific quote from the documentation in 5.2 as it is essentially incorrect. QQuickPainted item is the appropriate way to use QPainter code in Qt Quick 2 when you need it. The only warning is that QPainter code will generally be less performant than using pure Qt Quick items as it is not hardware accelerated. But a QQuickPanted item in C++ is probably a little bit faster than using Canvas.



  • thanks for the clarification


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.