3

I was looking for options like QGraphicsTextItem::setPen(), or even QFont::setColor() or QFont::setForegroundColor(). None of them exists.

How can I draw colored text on QGraphicsScene? I don't want to use QGraphicsTextItem::setHtml() and use HTML to do that (not sure if that's possible, though).

I'm using Qt 4.7.4.

3 Answers 3

6
scene.addText(tr("Hello World"))->setDefaultTextColor(Qt::white);
3

Are you looking for a combination of setPlainText() and setDefaultTextColor()?

http://doc.qt.io/archives/qt-4.7/qgraphicstextitem.html#setPlainText

http://doc.qt.io/archives/qt-4.7/qgraphicstextitem.html#setDefaultTextColor

If not, I'm not sure I understand your question...

1

QGraphicsTextItem has a palette method which returns a QPalette and QPalette has the setColor method which takes a role.

void QPalette::setColor ( ColorRole role, const QColor & color )

The role you want is

QPalette::Text  The foreground color used with Base. 
This is usually the same as the WindowText, in which case it must provide 
good contrast with Window and Base.
1
  • i can't find QGraphicsTextItem::palette().
    – Donotalo
    Dec 11, 2011 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.