QTreeWidgetItem setBackground after stylesheet applied has no effect
-
After i applied a style sheet to the QApplication instance i wanted to set the background and foreground colors of some items in my QTreeWidget.
When setting the background and foreground only the foreground changes and the background remains on the color set by the style sheet.The tree item calls the setBackground. The tree item has:
this->setFirstColumnSpanned(false);
this->setFlags(this->flags() & (~Qt::ItemIsSelectable));
The first column has a text and the second column is empty (there is a column, but this item does not set anything there, but i apply the colors to both columns).Thank you in advance :)
Here is a snipped from the stylesheet which sets the tree view:
/* ##################################################################################### *//* QTreeView */
QTreeView:branch:hover {
background: url('C:/OpenTwin/OpenTwin/Assets/ColorStyles/Bright/icons/transparent.png');
}QTreeView:branch:has-siblings:!adjoins-item {
border-image: url('C:/OpenTwin/OpenTwin/Assets/ColorStyles/Bright/icons/branch_line.png') 0;
}QTreeView:branch:has-siblings:adjoins-item {
border-image: url('C:/OpenTwin/OpenTwin/Assets/ColorStyles/Bright/icons/branch_more.png') 0;
}QTreeView:branch:!has-children:!has-siblings:adjoins-item {
border-image: url('C:/OpenTwin/OpenTwin/Assets/ColorStyles/Bright/icons/branch_end.png') 0;
}QTreeView:branch:has-children:!has-siblings:closed,
QTreeView:branch:closed:has-children:has-siblings {
border-image: none;
image: url('C:/OpenTwin/OpenTwin/Assets/ColorStyles/Bright/icons/branch_closed.png');
}QTreeView:branch:open:has-children:!has-siblings,
QTreeView:branch:open:has-children:has-siblings {
border-image: none;
image: url('C:/OpenTwin/OpenTwin/Assets/ColorStyles/Bright/icons/branch_open.png');
}QTreeView:branch:has-children:!has-siblings:closed:hover,
QTreeView:branch:closed:has-children:has-siblings:hover {
image: url('C:/OpenTwin/OpenTwin/Assets/ColorStyles/Bright/icons/branch_closed_focus.png');
}QTreeView:branch:open:has-children:!has-siblings:hover,
QTreeView:branch:open:has-children:has-siblings:hover {
image: url('C:/OpenTwin/OpenTwin/Assets/ColorStyles/Bright/icons/branch_open_focus.png');
}QTreeView::indicator:checked,
QListView::indicator:checked,
QTableView::indicator:checked,
QColumnView::indicator:checked {
image: url('C:/OpenTwin/OpenTwin/Assets/ColorStyles/Bright/icons/checkbox_checked.png');
}QTreeView::indicator:checked:hover,
QTreeView::indicator:checked:focus,
QTreeView::indicator:checked:pressed,
QListView::indicator:checked:hover,
QListView::indicator:checked:focus,
QListView::indicator:checked:pressed,
QTableView::indicator:checked:hover,
QTableView::indicator:checked:focus,
QTableView::indicator:checked:pressed,
QColumnView::indicator:checked:hover,
QColumnView::indicator:checked:focus,
QColumnView::indicator:checked:pressed {
image: url('C:/OpenTwin/OpenTwin/Assets/ColorStyles/Bright/icons/checkbox_checked_focus.png');
}QTreeView::indicator:unchecked,
QListView::indicator:unchecked,
QTableView::indicator:unchecked,
QColumnView::indicator:unchecked {
image: url('C:/OpenTwin/OpenTwin/Assets/ColorStyles/Bright/icons/checkbox_unchecked.png');
}QTreeView::indicator:unchecked:hover,
QTreeView::indicator:unchecked:focus,
QTreeView::indicator:unchecked:pressed,
QListView::indicator:unchecked:hover,
QListView::indicator:unchecked:focus,
QListView::indicator:unchecked:pressed,
QTableView::indicator:unchecked:hover,
QTableView::indicator:unchecked:focus,
QTableView::indicator:unchecked:pressed,
QColumnView::indicator:unchecked:hover,
QColumnView::indicator:unchecked:focus,
QColumnView::indicator:unchecked:pressed {
image: url('C:/OpenTwin/OpenTwin/Assets/ColorStyles/Bright/icons/checkbox_unchecked_focus.png');
}QTreeView::indicator:indeterminate,
QListView::indicator:indeterminate,
QTableView::indicator:indeterminate,
QColumnView::indicator:indeterminate {
image: url('C:/OpenTwin/OpenTwin/Assets/ColorStyles/Bright/icons/checkbox_indeterminate.png');
}QTreeView::indicator:indeterminate:hover,
QTreeView::indicator:indeterminate:focus,
QTreeView::indicator:indeterminate:pressed,
QListView::indicator:indeterminate:hover,
QListView::indicator:indeterminate:focus,
QListView::indicator:indeterminate:pressed,
QTableView::indicator:indeterminate:hover,
QTableView::indicator:indeterminate:focus,
QTableView::indicator:indeterminate:pressed,
QColumnView::indicator:indeterminate:hover,
QColumnView::indicator:indeterminate:focus,
QColumnView::indicator:indeterminate:pressed {
image: url('C:/OpenTwin/OpenTwin/Assets/ColorStyles/Bright/icons/checkbox_indeterminate_focus.png');
}QTreeView,
QListView,
QTableView,
QColumnView {
background-color: rgba(255,255,255,255);
border-color: rgba(0,0,0,255);
color: rgba(0,0,0,255);
gridline-color: rgba(0,0,0,255);
border-radius: 10;
}QTreeView:disabled,
QListView:disabled,
QTableView:disabled,
QColumnView:disabled {
background-color: rgba(255,255,255,255);
color: rgba(168,168,168,255);
}QTreeView:selected,
QListView:selected,
QTableView:selected,
QColumnView:selected {
background-color: rgba(0,255,0,255);
color: rgba(0,0,0,255);
}QTreeView:focus,
QListView:focus,
QTableView:focus,
QColumnView:focus {
border-color: rgba(0,255,0,255);
}QTreeView::item:pressed,
QListView::item:pressed,
QTableView::item:pressed,
QColumnView::item:pressed {
background-color: rgba(0,255,0,255);
color: rgba(0,0,0,255);
}QTreeView::item:selected:active,
QListView::item:selected:active,
QTableView::item:selected:active,
QColumnView::item:selected:active {
background-color: rgba(0,255,0,255);
color: rgba(0,0,0,255);
}QTreeView::item:selected:!active,
QListView::item:selected:!active,
QTableView::item:selected:!active,
QColumnView::item:selected:!active {
color: rgba(0,0,0,255);
background-color: rgba(255,255,255,255);
}QTreeView::item:!selected:hover,
QListView::item:!selected:hover,
QTableView::item:!selected:hover,
QColumnView::item:!selected:hover {
outline: 0;
color: rgba(255,255,255,255);
background-color: rgba(0,0,255,255);
}QTableCornerButton::section {
background-color: rgba(0,255,0,255);
color: rgba(0,0,0,255);
border: 1px solid transparent;
border-radius: 0px;
} -
The style sheet always wins - it's painted on top of the style: https://doc.qt.io/qt-6/stylesheet.html
"Style sheets are applied on top of the current widget style, meaning that your applications will look as native as possible, but any style sheet constraints will be taken into consideration. "
-
@Christian-Ehrlicher
Thank you for your quick response!
Is there a way to paint different QTreeWidgetItems in a single QTreeWidget in different custom color using stylesheets?
First i thought of something like setting an object name that i can reference in the sheet, but items don't have a name.Kind regards
-
@Rossiya95
You cannot address individual items using stylesheets because (a)QTreeWidgetItem
are not widgets (cannot be addressed in a stylesheet, do not have anobjectName()
), (b) you can reference "groups" ofQTreeWidgetItem
s via e.g.QTreeView::item:selected
but not individual ones and (c) you cannot use "variables" in stylesheets for e.g. different colors.You can address/change individual cells' background or foreground colors via the model (e.g.
data()
method), but not via stylesheet. -