1

I want get text color from plain text. i can get fontWeight and other format with charFormat() but when i debug foreground color, it's set to no color!!?

Please Help Me ....

sample code:

QTextCursor c = textCursor();
QTextCharFormat result = c.charFormat();

if (result.fontWeight() == QFont::Bold)
    qDebug() << "bold text";  //worked
else if (result.fontWeight() == QFont::Normal)
    qDebug() << "normal text";  //worked

if (result.foreground().color() == Qt::green)
    qDebug() << "green";  //not worked !!
else if (result.foreground().color() == Qt::blue)
    qDebug() << "blue";  //not worked !!
else
    qDebug() << "no color !!";

TNX

mgh
  • 41
  • 1
  • 1
  • 3

1 Answers1

4

If you're using Qt4 you have to use the QPalette class. QPalette stores different colors for different entities on a GUI (text color, background, etc.). It is inherited from the parent widget but can be changed for every widget you have.

QPlainTextEdit *pteEdit; // your text edit
QPalette palette = pteEdit->palette();
QColor textColor = palette.color( QPalette::WindowText );

Read up on the QPalette documentation. It might be a different color role depending on the widget type and there are subtypes. For inactive text, normal text, etc.

Dariusz Scharsig
  • 1,180
  • 7
  • 11
  • mmm! thanx for your answer. but i test it and doesn't work... :( can you help me? – mgh Oct 22 '12 at 07:05