How to avoid change in row color on mouse hover ?
-
I have QTreeWidgetItem with different color. Whenever I hover with mouse, lines background color becomes white.
But I do not want that. Whenever I will hover, color of the line should not change.
Currently I have used qstyle sheet to enable hovering.myTree->setStyleSheet(GetTreeStyleSheet()); GetTreeStyleSheet() { QString style = "QTreeView::item:hover {" "border: 1px solid;" "height: 30px;" "color: #000000;" "}"; return style }
I tried to tackle this issue with creating EventFilter and capturing Hover event.
In hover event, I am trying to know, previous color. And trying to set that same color on mouse hover. But it is not workingbool eventFilter(QObject *watched, QEvent *event) { Q_UNUSED(watched); bool filterEvent = false; switch (event->type()) { case QEvent::HoverMove:{ QHoverEvent* hoverEvent = static_cast<QHoverEvent*>(event); QTreeWidgetItem *selectedItem = myTree->itemAt(hoverEvent->pos()); if(selectedItem){ QBrush b = selectedItem->background(0); QColor c = b.color(); // selectedItem->setBackground(0,b); // it does not impact any difference selectedItem->setForeground(0,b); // because of this, text is not visible } break; } default: break; } return filterEvent; }
How to avoid change in row color on mouse hover ?
-
Based on the code shown, it can't be told if an event filter is the most efficient way to achieve the goal, and if it works at all. Things could maybe just be solved by assigning the right style sheets to the right objects. So let's assume an event filter is the best solution: It should filter hover events over items of
myTree
, i.e. prevent them from getting delivered to the watched object.bool eventFilter(QObject *watched, QEvent *event) { Q_UNUSED(watched); if (event->type == QEvent::HoverMove) { QHoverEvent *hoverEvent = static_cast<QHoverEvent *>(event); if (myTree->itemAt(hoverEvent->pos()) return true; } return false; }
Remarks to the existing code:
- It seems to assume that the event filter is reached after
selectedItem
has seen it. It aims at correcting what has been done before. It's the other way round: The event filter is reached before the event gets delivered toselectedItem
. (filter, opposite of exhaust pipe ;-) bool filterEvent = false;
defines a variable that is never changed and returned at the end. SoeventFilter
always returnsfalse
and the event gets delivered to the object watched. Returningtrue
says "this event has been processed already and does not have to be delivered anywhere else."
- It seems to assume that the event filter is reached after
-
@Axel-Spoerl
Thanks for your reply.
I wanted to solve this issue with qstyle sheet only. But I could not do that.
So I tried different way.
If it is possible, can you try with qStyleSheet ? -
@tushu said in How to avoid change in row color on mouse hover ?:
If it is possible, can you try with qStyleSheet ?
This will not work (as you already found out)
-
@Christian-Ehrlicher
I tried but I could not solve the issue with style sheet."QTreeWidget::item:hover {" "background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 transparent, stop: 1 transparent);" "border: 1px solid #bfcde4;" "}"
And my 1st preference is Qstylesheet. But I stucked and I tried different way.
Can anyone help me ?