How do I display a tooltip only for elided text in a QTableView
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
Re: How do I display a tooltip only for elided text in a QTableView
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.
Re: How do I display a tooltip only for elided text in a QTableView
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:
Code:
bool MyWidget
::viewportEvent(QEvent *event
) { if (event
->type
() == QEvent::ToolTip) { QHelpEvent *helpEvent
= static_cast<QHelpEvent
*>
(event
);
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;
}
}
}
Re: How do I display a tooltip only for elided text in a QTableView
What if you return true instead of false?
Re: How do I display a tooltip only for elided text in a QTableView
The tooltip still does not go away if I return true.
Re: How do I display a tooltip only for elided text in a QTableView
Call QToolTip::hideText() before returning true.
Re: How do I display a tooltip only for elided text in a QTableView
Ah, I was looking in that direction as well. That worked, thanks!