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

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

Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top