3

I want to display a tooltip for QTreeWidgetItem that's hovered. However, getting a tooltip is not a very fast process in my case, so I don't want to call setTooltip() for every single item. I want to do it on demand, on some event or signal. What's the easiest way to do it?

Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335

2 Answers2

5

The best solution I've found is to subclass QTreeWidgetItem, override virtual QVariant data(int column, int role) const; and return a tooltip for this item when data is called for Qt::ToolTipRole.

Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
  • lol, so, basically, what i said, but without changing to model/view. Sorry, dunno how didnt realized it had to be a data method in the widget items :S – trompa Aug 07 '13 at 16:40
  • @Trompa: Yeah, and without model at all :) – Violet Giraffe Aug 07 '13 at 17:21
  • Anyways, you should try it ;). With QStandardItemModel you would be using QStandardItems pretty much like WidgetItems and you get decoupling. – trompa Aug 08 '13 at 10:57
0

I think that it should be easier to achieve what you want if you migrate to a QTreeView/Model pattern.

QAbstractItemModel has a role for tooltips: Qt::ToolTipRole

You could subclass a Model to reimplement the

QVariant QAbstractItemModel::data ( const QModelIndex & index, int role = Qt::DisplayRole ) const [pure virtual

method.

So, when receives a Qt::TooltipRole, it calculates/recovers from an internal cache.

trompa
  • 1,967
  • 1
  • 18
  • 26
  • Note that, as QTreeWidget inherits from QTreeView, you could assign youe custom model. But it would kind of mixing apples and oranges – trompa Aug 07 '13 at 09:08
  • I've never used `QTreeView`, it seems overcomplicated for no good reason (provided that I've never had to do something that's not possible with Widget until now). Is there no way to achieve this with `QTreeWidget`? – Violet Giraffe Aug 07 '13 at 09:09
  • Would it be possible to reimplement `QtreeWidget::mouseEvent()` to get `QHoverEvent`, calculate which item the mouse is over and display a tooltip? – Violet Giraffe Aug 07 '13 at 09:12
  • There is a QEvent::ToolTip, so i figure you could filter the events through an eventfilter (not sure if installed in the treeWidget or in every treeWidgetItem). – trompa Aug 07 '13 at 09:19