My custom drawing of a QGraphicsItem includes text using user-configurable fonts (previously picked with QFontDialog).

Attempting to assign the font to either the provided QPainter OR to the QGraphicsScene within my subclass’ QGaphicsItem::paint() method has no effect. The plan is to render a few different text pieces using distinct fonts. Is it possible to do this in QGraphicsItem::paint()? (virtual method override). [See code excerpt below].

I know I could deploy child QGraphicsTextItems or QGraphicsSimpleTextItems. I will if I have to. (Do I have to?).

(We're using Qt 4.8.5 on Windows).

Qt Code:
  1. QFont _labelFont;
  2. const QString labelFontStr = labelFontName();
  3. _labelFont.fromString (labelFontStr);
  4.  
  5. // virtual from QGraphicsItem
  6. void TeacupGfxItem::paint (QPainter* painter,
  7. const QStyleOptionGraphicsItem* /*options*/,
  8. QWidget* /*widget*/ /*=NULL*/)
  9. {
  10. // ... ... ...
  11.  
  12. // *** NEITHER OF THESE HAVE ANY EFFECT ***
  13. painter->setFont (_labelFont);
  14. scene()->setFont (_labelFont);
  15.  
  16. const QString labelStr = teacupLabelStr();
  17. static const int textFlags = Qt::AlignHCenter | Qt::AlignTop |
  18. Qt::TextDontClip | Qt::TextSingleLine;
  19. QRectF drawnBoundRect;
  20. painter->setPen (QPen (Qt::black));
  21. painter->drawText (_labelRect, textFlags, labelStr, &drawnBoundRect);
  22. }
To copy to clipboard, switch view to plain text mode