0

I have subclassed QAbstractTableModel and in data() function I am displaying an image in last column of each row and a tooltip on mouse hover.

QVariant MyTableModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
    return QVariant();

if (role == Qt::DisplayRole)
{
    switch (index.column())
    {
     // few cases
    default:
        return QVariant();
    }
}
else if (role == Qt::CheckStateRole && index.column() == 0)
{
    int state= tableData.at(index.row()).state;
    if (state)
        return Qt::Checked;
    else
        return Qt::Unchecked;
}
else if (role == Qt::DecorationRole && index.column() == 7 && index.row() > 1)
{
    QPixmap pixMap(fileName);
    return pixMap;
}
else if (role == Qt::ToolTipRole && index.column() == 7 && index.row() > 1)
{
    return QString("Delete");
}
else
    return QVariant();
}

Tooltip text is displayed fine on each row but when I move cursor from last column of a row to another last column just below it(or any row below it) tooltip remains on the upper row.

This issue does not persist if cursor is moved to any other cell before moving to last column of another row. Thanks for help.

wazza
  • 313
  • 3
  • 19
  • Is this the full method? Or is there something after the last else? Can you share some screen of your table row to show the tooltip effect you experience? What does the model columnCount return? – Dusteh Nov 23 '16 at 08:05
  • Yeah this is full method except few cases in Display role which I have removed and added a comment // few cases. model columnCount returns 8 as there are 8 columns and I have delete buttons in last columns. Please find a rough mockup of my UI https://imagebin.ca/v/32xi2VQ3ShBW – wazza Nov 23 '16 at 11:01
  • I've looked into the problem and managed to reproduce it on a simple example. Check the answer for more info. – Dusteh Nov 23 '16 at 23:49

1 Answers1

1

When data displayed as a tooltip is the same for different items in the view the tooltip position isn't adjusted. Not sure if this is a design feature or bug - would need to look into Qt sources.

You can fix that quite easily though. In Qt documentation you can find such information:

Note that, if you want to show tooltips in an item view, the model/view architecture provides functionality to set an item's tool tip; e.g., the QTableWidgetItem::setToolTip() function. However, if you want to provide custom tool tips in an item view, you must intercept the help event in the QAbstractItemView::viewportEvent() function and handle it yourself.

Hence, subclassing your view and overriding the viewportEvent method should solve your problem.

Dusteh
  • 1,496
  • 16
  • 21
  • Thanks for the information. Currently I am using QTableView. As per my understanding, instead of QTableView I need to write a class subclassing QAbstractItemView and use it and I need to implement viewportEvent() on my own and do the needful in this event. Am I on right track ? – wazza Nov 24 '16 at 10:28
  • 1
    If this is the only thing you want to achieve it would be probably enough to sublass QTableView since it inherits from QAbstractItemView. – Dusteh Nov 24 '16 at 11:19