How to Change QTreeView highlight color
I am curious if there is a way to change the default blue highlight color for a QTreeView item, when a particular situation occurs. For instance if you make a QTreeView from data contained in say a QHash, based on a value in the QHash you want the highlight color to be green instead of blue.
How would you do this?
Thanks for any help!
Re: How to Change QTreeView highlight color
I am not aware of a convenience method for that, but I am not that heavy user of model/view.
Generally speaking you should override the delegate paint() method, and insert the specific logic you want for : option.state & QStyle::State_Selected
Re: How to Change QTreeView highlight color
Re: How to Change QTreeView highlight color
And how you would use set palette in this case?
Re: How to Change QTreeView highlight color
A sorry, I didn't notice that he wants different colors for different nodes of tree (my fault).
So he have to deliver own delegate like you said.
anyway it still quite simple. Subclass QStyledItemDelegate and override paint method like that:
Code:
{
if (index.
data(someRole
).
toInt()==1 && option.
state.
testFlag(QStyle::State_Selected)) { newOption.
palette.
setBrush(QPalette::Normal,
QPalette::Highlight , someOtherBrush
);
QStyledItemDelegate::paint(painter, newOption, index);
return;
}
QStyledItemDelegate::paint(painter, option, index);
}
Re: How to Change QTreeView highlight color
This might have went over my head a little bit but I think I see what you are both saying...
So if I currently have each tree item as class TreeItem where a private variable contains the data (as mentioned a QHash/QList/QVector...). Then those items are added to the tree model class called TreeModel which is a sub-class of AbstractItemModel.
Your saying to sub-class QStyledItemDelegate... would that be the TreeItem or TreeModel? My guess is TreeItem because that seems to make more sense to me but I see where you say index.data which leads me to believe your talking about the AbstractItemModel.
Just in case your curious the TreeModel class is then attached to QTreeView with QTreeView::setModel(TreeModel).
I hope I am making myself clear but if you need anything else please let me know and thanks for any suggestions!