Show tooltips for long entries of your custom model

From Qt Wiki
Revision as of 17:45, 14 January 2015 by Maintenance script (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This tutorial shows how to display tool tips for long entries of your custom model. The “long entries” in this case is a text that is truncated by the view.

This Wiki page based upon tutorial project “1_readonly” found in Qt SDK. Use it to get similar output and to play with the code.

Model

In order to provide tool tips one need to handle Qt::ToolTipRole request in a model data method. This model returns the same text for both display and tool tip roles.

If we run the application then tool tips will appear for every item. Based on the specifications the desired behaviour might be different.

Event filter, class QToolTipper

To improve the situation we apply an event filter to the view. The QToolTipper class finds the correspondence between cursor position and model data. Then it gets the text for the tool tip. The text is checked to be inside a visual rectangle. If it is not, then the tool tip is shown.

Installtion of the event filter

It is the viewport that receives necessary events. So the event filter is attached to the viewport.

Output

Upper picture demonstrates the tool tip and the lower picture demonstrates no tool tip for short text.
qtooltipper output

Categories: