PDA

View Full Version : How do I display a tooltip only for elided text in a QTableView



dpace
26th March 2011, 20:46
I am able to display a tooltip over my QTableView currently by overriding data() in my model. However, I only want the tooltip to be shown if the text of the cell is elided. Is there a way to do this?

Thanks,
Dave

wysota
27th March 2011, 10:57
You'd have to intercept the tooltip event (it's actually a help event) in the view, check if the text displayed is likely to be elided and then act accordingly.

dpace
28th March 2011, 19:19
Thank you wysota.

I tried overriding QTableView::event(QEvent *event) but I don't get QEvent::ToolTip events when hovering over individual cells, just when hovering over areas of the table view with no cells or headers.

I tried overrding viewportEvent() instead and this allows me to only show tooltips for items whose text is elided. However, the tooltip does not go away if I then hover over an item whose text is not elided. It seems that eating this event is preventing the tooltip from being cleared.

Do you have any suggestions on how to resolve this?

Here is the code:


bool MyWidget::viewportEvent(QEvent *event) {
if (event->type() == QEvent::ToolTip) {
QHelpEvent *helpEvent = static_cast<QHelpEvent*>(event);
QModelIndex index = indexAt(helpEvent->pos());
if (index.isValid()) {
QSize sizeHint = itemDelegate(index)->sizeHint(viewOptions(), index);
QRect rItem(0, 0, sizeHint.width(), sizeHint.height());
QRect rVisual = visualRect(index);
if (rItem.width() <= rVisual.width())
return false;
}
}
return QTableView::viewportEvent(event);
}

wysota
28th March 2011, 19:23
What if you return true instead of false?

dpace
28th March 2011, 19:29
The tooltip still does not go away if I return true.

wysota
28th March 2011, 19:43
Call QToolTip::hideText() before returning true.

dpace
28th March 2011, 19:53
Ah, I was looking in that direction as well. That worked, thanks!