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.

    Can any one please help ?

  • Hi!

    ::text selector will apply only on text. You probably want to set it on ::item ( one item in QTreeWidget).

    Try setting QTreeWidget::item { background: #F00; } only on failed items and green on succeeded.


  • Jake: Thats what my question is:

    Using stylesheet how to apply background color on only those item which has Text "Failed".

    styleSheet() api is only available to QTreeWidget not on QTreeWidgetItem.

    Can any one please help?

  • Hi,

    You can look for adding dynamic properties to stylesheets. I added a "Doc note": it may be helpful.

  • 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.

    Any other idea?

  • Hi!

    I see your problem. I did not check the docs first. Sorry.

    @myTreeWidgetItem->setBackground(0, QBrush(Qt::Red, Qt::SolidPattern));@

    This should work.
    Tested on a demo project.

    Docs "here":


  • 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.

    I will really appreciate if you can suggest something

  • Hi,

    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().

    "Here": is a sample code.

  • Sam: well the example code you pointed does not work.

    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.

    can any one please help

  • Did you get this working, I would like to get an update on this one, If there is any possibility to achieve the same effect using stylesheets.

  • Try to add custom dynamic properties "Failed", "Passed" or something else:

    background-color: red;
    background-color: green;

  • Moderators

    for people who will have the same issue in the future:

    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.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.