3

I want to put some text on my UI.

I am drawing the text in a paint event of a widget using painter.

Here is the sample code, which shows how I am drawing the text:

QWidget::paintEvent(painter);
QPainter paint(this);
paint.drawText(QPoint(10,30),"Duplex");

However, the text color looks like the default theme color. How do I set the application font color to the text in a paint event?

JavaAndCSharp
  • 1,507
  • 3
  • 23
  • 47
Naruto
  • 9,476
  • 37
  • 118
  • 201

2 Answers2

11

here is the answer i got it

 QPen pen  = (QApplication::palette().text().color());

 paint.setPen(pen);
Naruto
  • 9,476
  • 37
  • 118
  • 201
  • This is THE answer, but in addition to this, if you are in a paint event the unedited painter.pen() color should be the default color as well. – Rafe Jul 13 '17 at 23:58
1

You have to use the QPainter::setBrush(QBrush &) and QPainter::setPen(QPen &) methods to change the color used to draw graphics (and incidently the text color).

The command paint.setPen(QPen(QColor(255,0,0)) will set the outline color to red and paint.setBrush(QBrush(QColor(0,255,0)) will set the fill color to green.

You can also use directly the QPainter::setPen(QColor &) methods to change the color of the outline.

Lohrun
  • 6,432
  • 1
  • 24
  • 22
  • Thanks, lohrun.. infact i wanted how to take the default text color.. i got the answer too.. i will place it – Naruto Apr 28 '10 at 13:41