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).
const QString labelFontStr
= labelFontName
();
_labelFont.fromString (labelFontStr);
// virtual from QGraphicsItem
void TeacupGfxItem
::paint (QPainter* painter,
{
// ... ... ...
// *** NEITHER OF THESE HAVE ANY EFFECT ***
painter->setFont (_labelFont);
scene()->setFont (_labelFont);
const QString labelStr
= teacupLabelStr
();
static const int textFlags = Qt::AlignHCenter | Qt::AlignTop |
Qt::TextDontClip | Qt::TextSingleLine;
painter
->setPen
(QPen (Qt
::black));
painter->drawText (_labelRect, textFlags, labelStr, &drawnBoundRect);
}
QFont _labelFont;
const QString labelFontStr = labelFontName();
_labelFont.fromString (labelFontStr);
// virtual from QGraphicsItem
void TeacupGfxItem::paint (QPainter* painter,
const QStyleOptionGraphicsItem* /*options*/,
QWidget* /*widget*/ /*=NULL*/)
{
// ... ... ...
// *** NEITHER OF THESE HAVE ANY EFFECT ***
painter->setFont (_labelFont);
scene()->setFont (_labelFont);
const QString labelStr = teacupLabelStr();
static const int textFlags = Qt::AlignHCenter | Qt::AlignTop |
Qt::TextDontClip | Qt::TextSingleLine;
QRectF drawnBoundRect;
painter->setPen (QPen (Qt::black));
painter->drawText (_labelRect, textFlags, labelStr, &drawnBoundRect);
}
To copy to clipboard, switch view to plain text mode
Bookmarks