How to use style sheet in qt to not to change row color on selection.
-
@Bonnie I tried what you said just and it improved my output.
Currently it is looking like this :by your current suggestion that left side blue patch gone. But still I am missing my original colors. Now instead of white strip I want to my original color
And my current style sheet is :
QString GetTreeStyle() { QString style = "QTreeView { "show-decoration-selected: 0; "}" "QTreeView::item:selected{ "color: palette(text);" "background-color: palette(base);" "border: 1px solid red;" "}"; return style; }
-
@tushu Sadly stylesheet can't get the color set by
setBackgroundColor
.
In this situation I would suggest using a custom item delegate.-
remove stylesheet contents but keep "show-decoration-selected: 0"
-
subclass
QStyledItemDelegate
and reimplementinitStyleOption
void CustomItemDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const { option->state &= ~QStyle::State_Selected; //make sure no item is flaged as selected when drawing rows QStyledItemDelegate::initStyleOption(option, index); }
- create a CustomItemDelegate object and set it to the view by
setItemDelegate
But by this method you cannot set border for a selected row anymore.
By default there's a focus frame for the focused item, you may callsetAllColumnsShowFocus(true)
to make it cover the whole row to show the "selected row". -
-
@Bonnie Perfect solution. It worked perfectly.
But I got suggestion over the output.
Current output is :Clicked row ( add_7 ) is originally without color ( white ) . But after click, it slightly becoming bluish. Can we remove that light blue color ?
2nd row is clicked.
But when I click on a coloured row, it looks fine.
Is there any way to remove that light blue color when I click on non coloured row ?
And many thanks for your efforts and perfect Solution.
-
One more favor.
You have suggested following code. BUt I am not understanding what are you doing exactly.void CustomItemDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const { option->state &= ~QStyle::State_Selected; //make sure no item is flaged as selected when drawing rows QStyledItemDelegate::initStyleOption(option, index); }
In 2nd line , you are calling the initStyleOption() again.
But what are you doing in 1st line ?
Can you explain that ? -
@tushu said in How to use style sheet in qt to not to change row color on selection.:
But what are you doing in 1st line ?
option->state &= ~QStyle::State_Selected; //make sure no item is flaged as selected when drawing rows
Switches off/clears the
QStyle::State_Selected
bit inoption->state
, leaving all other bits untouched. -
@tushu said in How to use style sheet in qt to not to change row color on selection.:
But what are you doing in 1st line ?
-
@JoeCFD
To get focus I have added following line.setAllColumnsShowFocus(true);
If I remove that line, then by default focus will be given to a particular cell ( column) where I have clicked with same light blue colour.
Moreover, If I remove it completely, then I will miss a feel of row selection. I want row selection feeling also.
-
@JonB
What I understood : When I will click on any row, it will get selected and this method will get invoke. And then , thorugh this method , I will deselect my current selection by clearing bits and calling that method again. So in the end, there will not be any selection. So no dark blue default selection of row by Qt.Sorry if my understanding is wrong.
-
@tushu
Well that is about right. Certainly the end result should indeed be "So no dark blue default selection of row by Qt."But "and calling that method again." does not happen. When the
paint()
is called by Qt to draw the item, on entryQStyleOptionViewItem *option
contains various options directing how to draw it. Among these is a bit for whether the item is selected, and the base paint will act on that by showing the item in blue or whatever.@Bonnie has given you an override for the
initStyleOption()
, whose job it is to process those options for the forthcoming paint. His override removes anyState_Selected
bit in the option flags, and only after that calls the baseQStyledItemDelegate::initStyleOption(option, index)
. So that will simply not see that the item was selected/had the draw-as-selected bit set. So it will set up for the paint to draw as though the item is not selected.Note that this does not affect that the item actually is selected as far as the rest of your or Qt code is concerned. It will stay selected (or unselected). Ut simply will not paint it to show it is selected.
-
@JonB
Now I understood. Before this also I had used this bit clearing. But did not get why was it for. Now I understood.
Thanks for your explanation.I am feeling confident in Qt because of this forum and the good people like you all, who always helped me.
Thanks a lot once again. :) -
@tushu Well, as I said, the light blue color is the focus frame, it indicates the current index of the view.
The simpliest way is just disable all the indexes in the row if you don't have any interactive operation upon them, so they can't be set to be the current index.
(If there is any interactive operation, then I can't understand why it need to be removed...)
Since you're usingQTreeWidget
, you can callQTreeWidgetItem::setDisabled
.
Note that will make the item's text gray if no foreground color is set