3

I've a QTreeWidget and need to disable the mouse over highlighting on the childItems but not the click selection. The point here is, that I need to set this per Item because some are selectable. I was thinking about the QTreeWidget::itemEntered signal to check if the item should be highlighted or not but I can't get it to work because the description says

QTreeWidget mouse tracking needs to be enabled for this feature to work.

and I can't figure out how.

So my questions for are: How can I enable mouse tracking?

Is there an easier way to disable the highlighting?

Nitro.de
  • 821
  • 10
  • 27

2 Answers2

1

Simply invoke setMouseTracking() to enable mouse tracking for a specific widget.

Marco A.
  • 43,032
  • 26
  • 132
  • 246
  • do you know how to disable the highlighting too? Is there a flag, an event or something? – Nitro.de Jun 05 '15 at 08:40
  • [This](http://stackoverflow.com/a/23111163/1938163) might be a good starting point since it's a styling problem – Marco A. Jun 05 '15 at 08:42
  • Yeah i thought about stylesheet but that wont work because it's for all items QTableView::item. It's not possible to set stylesheet per item so i need another way for this =( – Nitro.de Jun 05 '15 at 08:44
  • Not sure I got it correctly, what about the focus selector? http://stackoverflow.com/a/24819698/1938163 – Marco A. Jun 05 '15 at 08:49
0

I ran into this problem (I know this is an old post, but I might as well post my solution, since it can be useful for others).

I could not properly disable the mouse feedback while keeping the mouse tracking enabled, but I could make this feedback invisible. I'm using qss stylesheets, and I set the mousehover feedback color to transparent:

MyTreeWidget::item:hover {
    background-color: transparent
}

It did the trick for me. Sadly it makes the feedback invisible all the time, rather than allowing to turn it off and on.

So as a next step, for when I needed it, I implemented my own feedback by using a delegate and overwritting the paint function. The QTreeView overwrite mouseMoveEvent and sends mouse coordinates to the delegate. This way, the delegate can adapt what it does in paint to this position. It feels pretty heavy, and a bit dirty, but it works. Delegate should also allow to have different behavior for different items.

PS: If you're using a delegate, in most cases, that should be enough without the qss change. In my case it wasn't, because I call QStyledItemDelegate::paint in my overwritten paint method, so I inherited some unwanted behavior.

Andéol Evain
  • 507
  • 2
  • 12