Show tooltips for long entries of your custom model: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Add "cleanup" tag)
(Decode HTML entity names)
Line 12: Line 12:
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.
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.


<code>QVariant MyModel::data(const QModelIndex &amp;index, int role) const
<code>QVariant MyModel::data(const QModelIndex &index, int role) const
{
{
  int row = index.row();
  int row = index.row();
Line 19: Line 19:
if (role  Qt::DisplayRole || role  Qt::ToolTipRole)
if (role  Qt::DisplayRole || role  Qt::ToolTipRole)
  {
  {
  if (row  1 &amp;amp;&amp;amp; col  1)
  if (row  1 && col  1)
  {
  {
  return QString("Qt is a cross-platform application framework that is widely used for developing application software with a graphical user interface");
  return QString("Qt is a cross-platform application framework that is widely used for developing application software with a graphical user interface");
Line 80: Line 80:
  int rectWidth = rect.width();
  int rectWidth = rect.width();


if ((itemTextWidth > rectWidth) &amp;&amp; !itemTooltip.isEmpty())
if ((itemTextWidth > rectWidth) && !itemTooltip.isEmpty())
  {
  {
  QToolTip::showText(helpEvent->globalPos(), itemTooltip, view, rect);
  QToolTip::showText(helpEvent->globalPos(), itemTooltip, view, rect);
Line 100: Line 100:
It is the viewport that receives necessary events. So the event filter is attached to the viewport.
It is the viewport that receives necessary events. So the event filter is attached to the viewport.


<code>tableView.setModel( &amp;myModel );
<code>tableView.setModel( &myModel );
tableView.viewport()->installEventFilter(new QToolTipper(&amp;tableView));
tableView.viewport()->installEventFilter(new QToolTipper(&tableView));
tableView.show();</code>
tableView.show();</code>



Revision as of 17:49, 12 March 2015

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

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
{
 int row = index.row();
 int col = index.column();

if (role  Qt::DisplayRole || role  Qt::ToolTipRole)
 {
 if (row  1 && col  1)
 {
 return QString("Qt is a cross-platform application framework that is widely used for developing application software with a graphical user interface");
 }
 else
 {
 return QString("Row%1, Column%2")
 .arg(row + 1)
 .arg(col +1);
 }
 }
 return QVariant();
}

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_
#define QTOOLTIPPER_H_

#include <QObject>
#include <QAbstractItemView>
#include <QHelpEvent>
#include <QToolTip>

class QToolTipper : public QObject
{
 Q_OBJECT
public:
 explicit QToolTipper(QObject* parent = NULL)
 : QObject(parent)
 {
 }

protected:
 bool eventFilter(QObject* obj, QEvent* event)
 {
 if (event->type() == QEvent::ToolTip)
 {
 QAbstractItemView* view = qobject_cast<QAbstractItemView*>(obj->parent());
 if (!view)
 {
 return false;
 }

QHelpEvent* helpEvent = static_cast<QHelpEvent*>(event);
 QPoint pos = helpEvent->pos();
 QModelIndex index = view->indexAt(pos);
 if (!index.isValid())
 return false;

QString itemText = view->model()->data(index).toString();
 QString itemTooltip = view->model()->data(index, Qt::ToolTipRole).toString();

 QFontMetrics fm(view->font());
 int itemTextWidth = fm.width(itemText);
 QRect rect = view->visualRect(index);
 int rectWidth = rect.width();

if ((itemTextWidth > rectWidth) && !itemTooltip.isEmpty())
 {
 QToolTip::showText(helpEvent->globalPos(), itemTooltip, view, rect);
 }
 else
 {
 QToolTip::hideText();
 }
 return true;
 }
 return false;
 }
};

#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 );
tableView.viewport()->installEventFilter(new QToolTipper(&tableView));
tableView.show();

Output

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