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

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

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?

有帮助吗?

解决方案

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.

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top