Results 1 to 7 of 7

Thread: How do I display a tooltip only for elided text in a QTableView

  1. #1
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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:

    Qt Code:
    1. bool MyWidget::viewportEvent(QEvent *event) {
    2. if (event->type() == QEvent::ToolTip) {
    3. QHelpEvent *helpEvent = static_cast<QHelpEvent*>(event);
    4. QModelIndex index = indexAt(helpEvent->pos());
    5. if (index.isValid()) {
    6. QSize sizeHint = itemDelegate(index)->sizeHint(viewOptions(), index);
    7. QRect rItem(0, 0, sizeHint.width(), sizeHint.height());
    8. QRect rVisual = visualRect(index);
    9. if (rItem.width() <= rVisual.width())
    10. return false;
    11. }
    12. }
    13. return QTableView::viewportEvent(event);
    14. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How do I display a tooltip only for elided text in a QTableView

    What if you return true instead of false?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How do I display a tooltip only for elided text in a QTableView

    Call QToolTip::hideText() before returning true.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    dpace (28th March 2011)

  8. #7
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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!

Similar Threads

  1. Display ToolTip when cibling a TreeView Cell
    By mourad in forum Qt Programming
    Replies: 0
    Last Post: 2nd November 2010, 08:33
  2. Replies: 4
    Last Post: 6th April 2010, 00:46
  3. Replies: 2
    Last Post: 7th June 2009, 10:47
  4. toolTip of TreeWidgetItem display is slow
    By yunpeng880 in forum Qt Programming
    Replies: 0
    Last Post: 9th March 2009, 09:35
  5. Multi-line elided text?
    By WinchellChung in forum Newbie
    Replies: 1
    Last Post: 6th February 2008, 16:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.