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.
QVariant MyModel::data(const QModelIndex &index, int role) const<br />{<br /> int row = index.row();<br /> int col = index.column();
if (role Qt::DisplayRole || role Qt::ToolTipRole)<br /> {<br /> if (row 1 &amp;&amp; col 1)<br /> {<br /> return QString("Qt is a cross-platform application framework that is widely used for developing application software with a graphical user interface&quot;);<br /> }<br /> else<br /> {<br /> return QString("Row%1, Column%2&quot;)<br /> .arg(row + 1)<br /> .arg(col +1);<br /> }<br /> }<br /> return QVariant();<br />}
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.
#ifndef QTOOLTIPPER_H_<br />#define QTOOLTIPPER_H_
#include <QObject&gt;<br />#include <QAbstractItemView&gt;<br />#include <QHelpEvent&gt;<br />#include <QToolTip&gt;
class QToolTipper : public QObject<br />{<br /> Q_OBJECT<br />public:<br /> explicit QToolTipper(QObject* parent = NULL)<br /> : QObject(parent)<br /> {<br /> }
protected:<br /> bool eventFilter(QObject* obj, QEvent* event)<br /> {<br /> if (event->type() == QEvent::ToolTip)<br /> {<br /> QAbstractItemView* view = qobject_cast&lt;QAbstractItemView*>(obj->parent());<br /> if (!view)<br /> {<br /> return false;<br /> }
QHelpEvent* helpEvent = static_cast&lt;QHelpEvent*>(event);<br /> QPoint pos = helpEvent->pos();<br /> QModelIndex index = view->indexAt(pos);<br /> if (!index.isValid())<br /> return false;
QString itemText = view->model()<s>>data(index).toString();<br /> QString itemTooltip = view</s>>model()<s>>data(index, Qt::ToolTipRole).toString();
<br /> QFontMetrics fm(view</s>>font());<br /> int itemTextWidth = fm.width(itemText);<br /> QRect rect = view->visualRect(index);<br /> int rectWidth = rect.width();
if ((itemTextWidth > rectWidth) && !itemTooltip.isEmpty())<br /> {<br /> QToolTip::showText(helpEvent->globalPos(), itemTooltip, view, rect);<br /> }<br /> else<br /> {<br /> QToolTip::hideText();<br /> }<br /> return true;<br /> }<br /> return false;<br /> }<br />};
#endif
Installtion of the event filter
It is the viewport that receives necessary events. So the event filter is attached to the viewport.
tableView.setModel( &myModel );<br />tableView.viewport()->installEventFilter(new QToolTipper(&tableView));<br />tableView.show();
Output
Upper picture demonstrates the tool tip and the lower picture demonstrates no tool tip for short text.