Important: Please read the Qt Code of Conduct -

drawText in QQuickItem

  • Hey guys,

    I'm writing a QQuickItem to display a ruler.
    So I have several lines and some text.
    I have no problem to draw the lines but I don't see a good way to draw my text next to it.

    As it seems that there is no textGeometry I would like to instantiate qml-Textelements from c++.
    In what class do I find the qml-textelements?


  • Moderators


    AFAIK it won't be easy to display text in QQuickItem instead did you consider using QQuickPaintedItem ? It uses QPainter with which you can use one of the drawText functions to draw text.

  • QQuickPaintedItem will use a texture to display my text.
    Lets say I rescale the ruler with an animation. On every render I will have to create a new texture and I think the performance will be quite bad.

  • Moderators

    Yes it does but you wont need to do it manually as the paint() will do it internally. I don't know how GPU intensive your application is but I had been using it for some of my projects which included animations and it worked fine without any noticeable performance drop.

  • Ok, so I tried to use QQuickPaintedItem now.
    My idea was to mix painting with paint() and direct drawing with updatePaintNode().
    Unfortunately paint gets never called when I implement the updatePaintNode() function.
    Is there a workaround?

  • Moderators

    If using QQuickPaintedItem it's better to use paint() rather than updatePaintNode().
    AFAIK there's no way to use QPainter in updatePaintNode().

  • @p3c0 i tried using QQuickPaintedItem and use QPainter to draw text, however seems like there's a problem when rendering the text, it appears to be pixelated or blurred in iOS (retina deisplay) devices while on Android, everything is perfect. Read somewhere that antialiasing isn't working on QQuickPaintedItem. Do you know any workaround on this? TIA.

  • Moderators

    @literA2 Seems like a bug. Check QTBUG-40489. It still unresolved. But there is a patch provided.

  • @p3c0 Thank you for the quick reply.

    I actually encountered such bug report already however I can't find the qsgpainternode.cpp file in my Qt folder. Did i miss some patch or update to download? TIA.

  • Moderators

    @literA2 Seems it has been moved to a other class. May be new changes should be added here. Better to ask there for a more details.

  • Thanks @p3c0

    Based on the documentation, QtDeclarative has already been deprecated in favor of the two new classes: Qt Quick and Qt QML. QQuickPaintedItem can be use as convenient way to port QDeclarativeItem-based classes that use the QPainter API.

    I've been searching on the internet how to render text on QSG nodes but to no avail. I'm still stuck into this problem. :(

Log in to reply