Guide to customizing text color within QTextEdit

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

Answer №1

I've found a great way to customize the appearance of text in QLabel and QPlainTextEdit by adjusting the Palette settings:

QPalette customPalette;
customPalette.setColor(QPalette::Window, backgroundColor);
customPalette.setColor(QPalette::WindowText, foregroundColor);
customPalette.setColor(QPalette::Text, foregroundColor);
myLabel->setPalette(customPalette);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I don't understand the significance of the following code: "for (const auto &s : strs) {}."

Could you explain the purpose of the colon in for (const auto &s : strs)? Also, what is the meaning behind this line of code? vector<string> &strs; for (const auto &s : strs){ // } ...

Exploring the Potential of Boostrap 4 SCSS as a Mixin Toolbox

I discovered a helpful guide on using bootstrap at , but since v4 removed LESS, I'm struggling to achieve the same results with SCSS. It's quite useful and I can't seem to figure out the correct way to import it. ...

"Upon clicking, toggle the addition or removal of the overflow hidden

I'm currently working on a function to add and remove the CSS property .css('overflow-y','hidden') using an onclick event, which I have successfully implemented. However, I am encountering an issue when attempting to remove this CS ...

What is the purpose of a single-row border with a top and bottom border that

Just to let you know, I'm new here so please be patient with me ;) I'm having an issue styling my foreach loop. My goal is to have a single line border appear at the top and bottom of each row when hovered over. However, the problem is that when ...

employing animation and iterating through each one for dynamic effect

Javascript $(document).ready(function() { $(".container").animate({left:'120px'}, 6000).queue(function(next){ $(".child").css('display', 'none'); }); }); The provided code snippet demonstrates animating a single box and t ...

Sneaky spam and ads embedded within Joomla template

Today, while examining the source code of a Joomla site I am developing, I stumbled upon some hidden links that seem to be spam. I have spent an hour searching through various template files but have been unable to locate them. The hidden links in questio ...

Avoid unintentional scrolling beyond the padding area

Here is an example code where the overflowing content that is scrolled, scrolls into the top/bottom padding area. Is there a way to prevent this while still keeping the scrollbar right next to the right edge? .parent { height: 200px; width: 200px; ...

Interacting through C++ Interfaces

In my attempt to utilize C++ in a similar way to Java/C# interfaces, I have created classes that solely contain pure virtual methods. Here is an example: class IMyInterface { public: virtual int someFunc() = 0; virtual ~IMyInterface() {} }; Every ...

Is there a way to have my grid not apply grid-gap if a specific area is vacant?

I am working with a grid layout that has defined grid areas and a grid-gap, but some areas may not always have content. How can I prevent a double gap between items when an area is empty without changing the grid-template? For example: .grid { displa ...

Yet another IE8 CSS dropdown in shambles

This particular PyroCMS site is quite dynamic, which is why using jsfiddle for posting doesn't work as smoothly. The menu seems to be functioning properly in all major current browsers and their versions, except for IE7, which is not supported. Howeve ...

Identifying Browsers using CSS

I am struggling to understand why my HTML5 website appears differently in each browser. It seems like a CSS issue, but I can't pinpoint the exact problem. Here are screenshots of how the site looks on different browsers: Chrome: Safari: Internet E ...

Storing JNI environment in a multi-threaded program's cache

My android application is utilizing multiple threads, including the UI thread and networking thread. I now need both threads to access native functions, some of which will also need to call java functions. To achieve this, each thread requires a unique JN ...

Getting rid of the IE pseudo-element that shows up on the right edge of a password textbox

My form consists of two text boxes for username and password entry. While entering text in these boxes, I noticed that Internet Explorer (IE) has specific pseudo-elements for the Username and Password fields: Username textbox: <input class="form- ...

The CSS classes for the dropzonejs are not being properly updated for editing purposes

I'm currently facing an issue where I am attempting to include an editable area inside a dropzone, but for some reason, the editable area is not visible within the dropzone and the CSS classes are not being applied. <a href="#" editable-text="us ...

Strategies for aligning a link on top of another div with absolute positioning

Looking for some assistance with my HTML and CSS code. Specifically, I need the position of the #header id to be set as absolute: <div class="main"> <a href="www.website.com"> <div id="header" class="other"> </div> #heade ...

Why is it that the exception::what function does not simply provide a return value of type

During my attempt to create a custom std::exception class, I encountered confusion on how to correctly implement the what() function. For instance, consider the following: class int_error : std::exception { int param; public: int_error(int param) ...

two sections lined up beside each other

Currently, I have two sections positioned on separate lines. Each section consists of multiple lines of text, and I am looking to align them next to each other with a 10px margin between them. Would appreciate any assistance in achieving this setup. Thank ...

Would it be beneficial to assign a single class name to all elements with matching styles within a CSS framework?

Currently, I am in the process of creating my own CSS library or framework. However, I have encountered an issue where all 10 li tags share the same classes, such as .pl{ padding-left:10px} .mr{ margin-right:10px}, along with other necessary attributes. Wh ...

Prevent images from displaying on smaller screens by utilizing Bootstrap's responsive design

Here is a snippet of code that utilizes Bootstrap 4 to display text and images: <div class="container"> <div class="row"> <div class="col-xl-12"> <img src="~/img/img1.png" style="height:60.3px;width:750px" clas ...

The input field is stretching beyond its intended length

My issue arises from having a div containing a listbox (select), textbox (input text), and button (input submit) inside it, with width:250px set for the entire div and width:100% for each element within. Logically, this should fill the space inside the div ...