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

26th March 2011, 19: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?


27th March 2011, 09: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.

28th March 2011, 18: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);

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

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

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

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