I have resisted asking this seemingly n00b question, but all my recent effort to achieve this task have failed. Here are the things I have tried already, all have failed! Could it be that my OpenSuse 11.3 sets system-wide style settings that apply to even my Qt app by default?
//I have some QTextEdit created in QDesigner -- call it myQEdit
QString str = "some content i want to display"
//trial one:
QString my_html_template = "<html><head></head><body style=\"color:__color__;\">__content__</body></html>"
myQEdit->document()->setHtml(my_html_template.replace("__color__","#99ff00").replace("__content__",str));
that fails, then i tried…
//trial two:
myQEdit->setDocument(new QTextDocument(str,this));
myQEdit->document()->setDefaultStyleSheet(" body { color:#99ff00;}");
I even tried setting the !important css flag on the color value i pass like:
but this failed too!
myQEdit->document()->setDefaultStyleSheet(" body { color:#99ff00 !important;}");
So I decided to set the color of my QTextEdit from the designer itself – by specifying my custom color in the option to set the raw html content of the QTextEdit. While i didnt change the content programmatically, the desired color was used. But the moment I set custom content like this:
myQEdit->setDocument(new QTextDocument(str));
I loose the color settings I’d set from QDesigner on the QTextEdit. So what is the correct way to achieve what am desiring? I know it can be done some way…
Finally:
After using the hint from the accepted answer below, here is how I’ve solved it:
myQEdit->setDocument(new QTextDocument(str,this));
QPalette pal;
pal.setColor(QPalette::Text, QColor::fromRgb(0,150,0));
myQEdit->setPalette(pal);
I’ve had success changing text color of
QLabelandQPlainTextEditby changing the Palette: