Displaying a tooltip for QTreeWidgetItem when it's hovered without calling setTooltip() for every item

StackOverflow https://stackoverflow.com/questions/18098919

Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top