Modify default widget text color with applied stylesheet of QTreeWidget items - qt

I have a QTreeWidget (however this question relates to any kind of widget) in which I have items which, under certain circumstances change their foreground color to red using item.setForeground(0,QBrush(QColor("red"))).
Later they can change their foreground color back to black. But the problem is that if the widget has been set some stylesheet which has changed the foreground color to other than black, I am in trouble. My question is how to revert the color of an item to the default color used by the widget for text color given the applied stylesheets?
For example if I apply a dark stylesheet which makes widget background dark and default text color white, first I can see white items, then they change to red and then they become black. But I want them white again. But I do not know how to find that it is white (and not black) color they should change to.
I tried this:
1) if I use item.setForeground(QtGui.QBrush()) which I hoped would use empty and therefore default brush, I always get black text
2) if I query the text color treeWidget.palette().text().color() I always get the same color regardless of the stylesheet

This question doesn't really apply to "any kind of widget", because there is no guarantee that setting the foreground colour will always work. To quote from the docs for QPalette:
Warning: Some styles do not use the palette for all drawing, for
instance, if they make use of native theme engines. This is the case
for both the Windows XP, Windows Vista, and the Mac OS X styles.
However, if the question is restricted to model items (such as QTreeWidgetItem), you can clear the current settings like this:
item.setData(column, QtCore.Qt.ForegroundRole, None)
On platforms that do allow changes via the palette, you could get the default palette like this:
palette = QtGui.qApp.style().standardPalette()
which should then allow you to restore the original values.

Related

Apply opacity to theme color keeeping the tie to the original color

In Google Slide, I would like to use a theme color and apply opacity to it.
I know I can do this by adding a new color and setting the transparency, the problem is that when I change the theme color, the new transparent color will not change.
Up until today, I thought this was just a missing feature in Google Slides, but a template I bought from a marketplace, has this exact feature, i.e. there is a shape which has a theme color and is also transparent, the problem is that I cannot change the transparency without incurring in the above mentioned problem.
My question is, is there a way to change the transparency and also keep the color tied to the theme color?
As an example, what I want to achieve is: create shape_1 and give it theme_color_1 (let's say it is #0029FFFF). Set transparency of shape_1 to 50% (so the color is #0029FF80). Change theme_color_1 (say to #44FF32FF). I want shape_1 to have new color #44FF3280.

Resetting an attribute to a default style value in XAML Xamarin.Forms

I have a label which looks like this:
<Label Style="{StaticResource myStyle}" x:Name="TestLabel">
The bound style sets the background color of the label to red color.
Then later on in the code, I do this:
TestLabel.SetValue(Label.BackgroundColorProperty, Color.Green);
This sets the background color of the label to green.
But then, I need to clear the green color which I set manually, and I need to reset it to the default value, dictated by the bound style (in this case red).
So I do this:
TestLabel.ClearValue(Label.BackgroundColorProperty);
But instead reverting back to red, the background color gets removed completely and becomes transparent (which is the default setting for a label, without any styles attached to it).
(One solution that offers itself would be TestLabel.SetValue(Label.BackgroundColorProperty, Color.Red);, but I cannot do this, since I need the color to revert back to whatever the style attached to it says it is, since different labels have different styles attached)
The best way to set or reset attribute use trigger instead of manual set.
https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/triggers/

Changing the Shadow Color of a Disabled QComboBox also changes the color of Active Text

I'm currently trying to change the style of of disabled QComboBox, including the shadow drawn behind the text. Annoyingly enough, it seems the text shadow cannot be styled in the style sheet, which would be my preferred solution, but that's not the issue at hand.
I've have managed to change the shadow color in my code using setColor( QPalette::Disabled, QPalette::Light, QColor( #, #, # ) on the QComboBox's palette. While this "works". I come into the issue that the text color for the active state (but not the list view) also takes on that color, completely overriding changes made by the style sheet. I was under the impression that style sheet was meant to trump the palette.
Ideally, the fix for this would be a 100% style sheet solution, but google searches for me have had no positive results as far as setting the shadow color in the style sheet. I did find something about an etch-disable-text property, but that seems to only be psuedo science.
Calling setColor( QPalette::Active, QPalette::ButtonText, QColor( #, #, # ) ) on my palette before assigning it to by QComboBox resolves the issue, but I'm still looking for a style sheet solution.

Disabled QGroupBox title shadow (aka etching)

Look thoroughly at the disabled QGroupBox title (see attached image).
You'll notice a tiny white 1-pixel shadow under the title's letters.
It's barely noticeable on default style sheet, but it can be much more annoying if you set dark background and text color.
What can I do to disable this shadow, or at least change its color?
What style are you using? It looks like the 'basic' Windows style. If you want to get rid of the text shadow, you can implement your own style and change the way the text is drawn for disabled group boxes. Read more about about QStyle and how to create a custom style here. The link is for Qt 5.1, but the principle is the same for Qt 4 as well.
An easier way would be to simply change the palette for the QGroupBox object. Change the color identified by color group QPalette::Disabled and color role QPalette::Light to any color with the alpha channel set to 0, e.g. QColor(0, 0, 0, 0). This will effectively disable the text shadow. However, it will also disable the shadow of the lines so it might not be what you want.
I've found a solution:
Unfortunately, you can't remove disabled text shadow (aka etching), but you can change its color using dirty workaround:
It looks like shadow effect always takes its color from the ColorGroup "Disabled" and the ColorRole "Light" of the current palette. So, you just set this color to the background color of your widget:
QPalette p = myWidget->palette();
p.setColor(QPalette::Disabled, QPalette::Light, QColor(0,0,0)); <- place your widget bg color here
myWidget->setPalette(p);
I've found this solution here

Displaying a QStandardItem with its foreground color in a QTreeView even when it is selected

Using Qt 4.6.3 on Linux/X11.
I have a QTreeView widget which uses a QStandardItemModel as its model, with 4 columns and hundreds of rows. Most of the items in the list are to be displayed with a standard color, but a few need to be of a different color. I can change the colors of those few items easily with QStandardItem::setForeground().
However, that only affects the color of the item when it is not selected. When I select a colored item, its background color changes to blue (which is ok), and the text color changes to white (which is not ok). I tried using a stylesheet to affect the foreground color of selected items (with selector QTreeView::item:selected), but it affects all items.
I would like items for which I called item->setForeground(Qt::red) to remain red even when they are selected, and other items to use the default set of colors (which they already do). How can that be done?
The colors being used are (I assume) those for the QPalette's Hightlight and HighlightedText roles. Unfortunately, I don't know of any way to set those on an individual standard item.
However, since standard items are used in the model/view framework, you have another option. You should be able to create a delegate to paint the view however you want to. I would recommend inheriting from the styled delegate, and calling the parent class's functionality as much as possible. Likely, you'll only need to change a few parameters in the cases where an item is selected and has a non-standard foreground color.

Resources