I have a QTreeWidget and I have a stylesheet applied to it. I would like some of the QTreeWidgetItems to have different hover and selected colors than the rest of the styesheet styled items. I colored the normal state with setData(columnNumber, Qt::ForegroundRole, colorName) but I can't change their colors for hover and selected states.
Does anyone know if it is possible to achieve this in Qt in some way?
Thanks!
AFAIK stylesheet is not a panacea for everything. You want very specific thing so you should look deeper and use something more powerful. I suggest you to use delegate. You didn't provide specification, so I provide main idea. In
QStyledItemDelegatesubclass reimplementpaint. For example:Here I set some specific properties to every second item, but you can use another specific items.
QTreeWidgetInheritsQTreeViewso use:It seems that your widget is complex, so I hope that you understand main idea and you will be able to write delegate which will be absolutely suitable for you. If you didn't work with delegates before, then check examples, it is not very complex.
http://qt-project.org/doc/qt-4.8/itemviews-stardelegate-stardelegate-h.html
http://qt-project.org/doc/qt-4.8/itemviews-stardelegate-stardelegate-cpp.html
In my answer I used next delegate:
There are many methods here but
paintis most important for you.