Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.
Show tooltips for long entries of your custom model
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