Please nominate your Qt Champions for 2021!

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

  • I want to display a tooltip for QTreeWidgetItem that's hovered. However, getting a tooltip is not a very fast operation 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?

  • Moderators

    This can be easily achieved when you return the tool tip text in the model:
    QVariant MyModel::data ( const QModelIndex & index, int role ) const
    if( role == Qt::ToolTipRole )
    return QVariant(...);
    BaseClass::data(index, role);

    Easy when you've subclassed your model. If not you can use a proxy model.

  • Moderators

    [quote author="raven-worx" date="1375869431"]This can be easily achieved when you return the tool tip text in the model[/quote]

    True, but OP is using QTreeWidget, not QTreeView.

  • So I should subclass QTreeWidget, create my custom model and call setModel() on that widget?

  • Moderators

    You can use QTreeView without subclassing, but it's a lot of work to code from Widget to View approach. If the issue is not too severe, I would vote against changing the current setup.

    ... actually coming to think about it, maybe you could subclass QTreeWidget. I don't know.

  • Moderators

    [quote author="sierdzio" date="1375870403"]True, but OP is using QTreeWidget, not QTreeView.[/quote] fault.
    But the changes are minimal anyway. Instead of subclassing your tree widget or model, subclass QTreeWidgetItem and overrride it's data() method. Analog to what i suggested for the model.

  • That's an easy solution indeed, thanks.

Log in to reply