PDA

View Full Version : setting text also changes color?



drhex
18th November 2006, 10:26
Here is a small prog that just sets a color and some text in a QTextEdit.
(QT 4.2 on X11)



#include <QApplication>
#include <QTextEdit>

int main( int argc, char **argv )
{
QApplication app( argc, argv );

QTextEdit t;
qDebug("init color: %08x", t.textColor().rgb());

t.setTextColor(0xdd9933);
qDebug("after set color: %08x", t.textColor().rgb());

t.setPlainText("Some text");
qDebug("after set text: %08x", t.textColor().rgb());

t.show();
app.exec();
return 0;
}


The console output is:


init color: ff000000
after set color: ffdd9933
after set text: ff000000


Apparently, setting the text has the side-effect of resetting the current color. Looks like a bug to me. Anyone who can interpret it as a "feature"?

munna
18th November 2006, 10:43
This is probably happening because you are using setPlainText().

Try using

textCursor()->insertText();

drhex
18th November 2006, 10:59
textCursor()->insertText() works. (or I could have set the color again after setting the text)
but why does setPlainText() change the color?

munna
18th November 2006, 12:26
From the docs




Changes the text of the text edit to the string text. Any previous text is removed.
text is interpreted as plain text.



Plain text means black as text color and probably "Arial" as font. This might even be OS specific.

drhex
18th November 2006, 15:22
The text set with setPlainText() was rendered in the color set just previously with setTextColor(), not black. I'll ask the Trolls.

drhex
22nd November 2006, 16:36
The Trolls says it will be fixed in QT 4.3 (http://www.trolltech.com/developer/task-tracker/index_html?id=109073&method=entry)