How to Set custom text Color in QTextEdit?


Tags: c++,css,qt,qtextedit

Problem :

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);


Solution :

I've had success changing text color of QLabel and QPlainTextEdit by changing the Palette:

QPalette pal;
pal.setColor(QPalette::Window, bgColor);
pal.setColor(QPalette::WindowText, fgColor);
pal.setColor(QPalette::Text, fgColor);
mylabel->setPalette(pal);

    CSS Howto..

    How to fix a element (have example) on the screen using jquery /css?

    How to navigate divs in a custom built single page app? [closed]

    How to automatically update relative CSS paths for minification?

    How to build a responsive padding for list menus using CSS?

    How do you read !important in CSS?

    How to find dependand JS and CSS

    How to align a HTML definition list (
    ) horizontally without limiting term or definition in height?

    How can I use a data attribute to set a background-image in CSS?

    How to display only the first few lines of a div (clamping)?

    How to make background image fade in with css when page loads?

    how do I use the :valid and :invalid selectors for css?

    I have a nav bar that changes the src of an iframe, how can i change the active link color?

    How does this site hide the iframe scrollbar?

    how to load the dynamically changed css file

    How can i make div shape like this with css? [duplicate]

    Hide one div when showing another with JavaScript

    How to defferent row heights for different rows in a single table?

    How to position two
  • 's within a nested
      ?
  • How to Access A To String Function with PHP?

    How to align a Wordpress navigation bar to the right of a logo?

    sencha touch 2.0 : How to apply conditional background color to list item

    How to make links work, regardless of location in directory structure?

    How to add an image to an anchor tag using CSS?

    Jquery Show Hide with attribute

    How to align image as pseudo element?

    How to avoid an hover on a menu item to affect the menu items on the right

    How to auto margin-left for the message bar

    how to auto adjust table td width from the content

    How do I arrange this in CSS?

    How to make gridview in asp.net scrollable?