Item view (styleSheet bgcolor)

hi all,
I have QTreeWidget and QTreeWidgetItem. I already have a stylesheet applied to my tree widget making background color of all items Yellow, myTree.setStyleShett("QTreeWidget::item{background-color:yellow}")

Now on a button click some of the QtreeWidgetItem text changes and I want to change the background color from yellow to GREEN of those Item whose text changed to "Passed" and RED to those items whose text become "Failed"

I tried using setBackground(col,myGreenBrush) of those item whose text are Passed but it does not do any thing, I found styleSheet(QTreeWidget::item::text....) But I dont know how to use "::text" thing to compare my text in styleSheet.

Sam, This is really kool i like this but unfortunately This wont help me because QTreeWidgetItem is not derived from QOBject to have any property.

Just to rephrase my problem, In my QtreeWidget there are only 20 items (items are QtreewidgetItem) and out of 20 item 2 items text are "failed" 15 items text are "passed" , I simply want to change background color of at least 2 failed items using StyleSheet.

Jake: Thanx for looking into this but I have already tried that and mentioned in my first post that it does not work if an styleSheet is already Applied to QTreeWidget.

it works on your demo because there is no stylesheet applied there before you call setBackground() if you already have applied setStyleSheet(QTreeWidget::item{background-color:yellow}) to make all item yellow, Now if you do QTreeWidgetItem *item=treeWidget.topLevelItem(2);item->setBackground(...) it has no effect.

I am not sure if this is bug or what but it does not work, but if change the background using stylesheet itself on mouse hover background changes again to new color but this is happening only through stylesheet.
and this is reason why I am looking for a way to compare if any item of my treewidget has text "Pass" i can only change the bacground color of that item to Green using STYLESHEET ONLY.

If you are using stylesheets just for styling a QTreeWidgetItem, then i would suggest to use a delegate to get the result for the above scenario. You can write a class that inherits QStyledItemDelegate then override the paintEvent().

Item setBackground() does not do any thing if an stylesheet is already applied. (if you do ui->treeWidget->setStyleSheet("QTreeWIdget::item{background-color=blue;}") then non of the yellow red green painting of paint function in delegate class has any effect on it.

and this what I am fighting with, I have to have my styleSheet on my items because of many reason specially gradient.

and on some condition I want to change the background color of few items, even using style delegat it does not help.

It seems that whenever you use a stylesheet definition for items
@
QTreeWIdget::item {
...
}
@
they will have a higher priority than the one you have set (e.g. via the style option object or via the model and it's background role).

So the only solution to this will be not to style any border and/or background via stylesheet using the ::item selector.

---- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ----
If you have a question please use the forum so others can benefit from the solution in the future