Important: Please read the Qt Code of Conduct -

Is it advisable to use a QStaticText to draw a small text label in a subclass of QGraphicsItem?

  • Greetings.

    I am implementing a subclass of QGraphicsItem, which draw a customized graphic item and also draw a small text label. The text is drawn based on the lower right corner of the bounding rect of my custom item.

    My question: Is it appropriate to use QStaticText in this case?, or is there a better option?

    Some considerations:

    • This is a small text label (although it could be of any length), in practice it is only a numeric value no more than five (5) digits.

    • Routinely, the text does not change very often.

    • So far, my class has a member variable of type QStaticText (contains the text is to be drawn).
      To draw the text using the QPainter::drawStaticText() function within MyGraphIt::paint( .. )

      This last consideration arises part of my question:

      The text in the QStaticText is put in constructor of my class (and modified infrequently, by appropriate function).

      In the function MyGraphIt::paint( .. ), the painter object is given as a parameter, with several settings already established (including the font), internally I only modify the color and width of the pen.

      I wonder if, in this context, I am taking advantage of the benefits of a QStaticText?, or conversely,
      I am imposing any unnecessary overhead?

    • The other part of my question arises from considering the alternatives:

      1. Use QPainter::drawText() to paint the text. This alternative creates a problem when calculating the bounding Rect of my item. I do not know how to calculate the bounding rect of a QString, since in the context in which this should be calculated (MyGraphIt::boundingRect()) I have no information on the font and would have to build a QPainter object (I think).

      2. Use QGraphicsSimpleTextItem. At first glance, it seems that this option requires more overhead during rendering.

    Thanks in advance for any help and/or suggestions regarding this matter.

Log in to reply