Although I have been hesitant to ask what may seem like a beginner question, all of my recent attempts at completing this task have been unsuccessful. Despite trying various methods, none have produced the desired result. Could it be that OpenSuse 11.3 applies system-wide style settings that affect my Qt app by default?
//Attempting to customize QTextEdit created in QDesigner -- let's call it myQEdit
QString str = "some content I want to display"
//First attempt:
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));
After failing with the first method, I tried...
//Second attempt:
myQEdit->setDocument(new QTextDocument(str,this));
myQEdit->document()->setDefaultStyleSheet(" body { color:#99ff00;}");
I even attempted setting the !important
css flag on the color
value as follows:
This approach did not work either!
myQEdit->document()->setDefaultStyleSheet(" body { color:#99ff00 !important;}");
Eventually, I decided to set the color of my QTextEdit
directly from the designer itself by specifying the custom color in the option to set the raw HTML content of the QTextEdit
. Although the desired color was applied without changing the content programmatically, as soon as I set custom content like this:
myQEdit->setDocument(new QTextDocument(str));
The color settings I had set from QDesigner
on the QTextEdit
were lost. What is the correct way to achieve the desired outcome? I believe there must be a solution...
Update: After following the advice provided in the accepted answer below, here is how I resolved the issue:
myQEdit->setDocument(new QTextDocument(str,this));
QPalette pal;
pal.setColor(QPalette::Text, QColor::fromRgb(0,150,0));
myQEdit->setPalette(pal);