Results 1 to 4 of 4

Thread: Tooltips for QListViewItems (QT3)?

  1. #1
    Join Date
    Jan 2006
    Location
    Sofia, Bulgaria
    Posts
    24
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Tooltips for QListViewItems (QT3)?

    Hi, is there any way to implement tooltips for QListViewItems (hints, etc)?
    In general, I want to do the following thing:
    I have a QListView with columns that cannot fit in the viewport and thus a horizontal scroll bar is displayed. When the user hovers an item, I want to be able to show all the text in the column in a tooltip if and only if that text is not shown in its whole (there are parts of the text that have not been displayed because the scrolling area moved in such a way that it hides parts of the text)

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

    Default Re: Tooltips for QListViewItems (QT3)?

    Use QListView::itemAt() locate an item under cursor and QToolTip for handling "tipping". See the example that comes with Qt for more info.

  3. #3
    Join Date
    Jan 2006
    Location
    Sofia, Bulgaria
    Posts
    24
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Tooltips for QListViewItems (QT3)?

    OK, and can someone tell me why this approach does not work?

    (header file)
    Qt Code:
    1. #ifndef N_LIST_VIEW_TOOLTIP__H
    2. #define N_LIST_VIEW_TOOLTIP__H
    3.  
    4. #include <qtooltip.h>
    5. #include <qlistview.h>
    6. #include <qguardedptr.h>
    7.  
    8. class n_list_view_tooltip: public QToolTip
    9. {
    10. private:
    11. QGuardedPtr< QListView > f_list_view;
    12. protected:
    13. virtual void maybeTip( const QPoint& p );
    14. public:
    15. n_list_view_tooltip( QListView * widget, QToolTipGroup * group = 0 );
    16. ~n_list_view_tooltip();
    17. QListView * list_view() const;
    18. void set_list_view( QListView * value );
    19. };//n_list_view_tooltip
    20.  
    21. #endif
    To copy to clipboard, switch view to plain text mode 

    (source file)
    Qt Code:
    1. #include "n_list_view_tooltip.h"
    2.  
    3. #include <qheader.h>
    4.  
    5. n_list_view_tooltip::n_list_view_tooltip( QListView * widget, QToolTipGroup * group ):
    6. QToolTip( widget, group ), f_list_view( widget )
    7. {
    8. }
    9.  
    10. n_list_view_tooltip::~ n_list_view_tooltip( )
    11. {
    12. if ( f_list_view )
    13. remove( f_list_view );
    14. }
    15.  
    16. QListView * n_list_view_tooltip::list_view( ) const
    17. {
    18. return f_list_view;
    19. }
    20.  
    21. void n_list_view_tooltip::set_list_view( QListView * value )
    22. {
    23. f_list_view = value;
    24. }
    25.  
    26. void n_list_view_tooltip::maybeTip( const QPoint & p )
    27. {
    28. if ( !f_list_view )
    29. {
    30. qDebug( "void n_list_view_tooltip::maybeTip( %d, %d ): f_list_view is NULL", p.x(), p.y() );
    31. return;
    32. }
    33.  
    34. if ( ! f_list_view->viewport()->rect().contains( p ) )
    35. {
    36. qDebug( "void n_list_view_tooltip::maybeTip( %d, %d ): viewport does not contain p!", p.x(), p.y() );
    37. return;
    38. }
    39.  
    40. QPoint point( f_list_view->viewport()->mapFromParent( p ) );
    41. QListViewItem * item = f_list_view->itemAt( point );
    42. if ( !item )
    43. {
    44. qDebug( "void n_list_view_tooltip::maybeTip( %d, %d ): item is NULL", p.x(), p.y() );
    45. return;
    46. }
    47. QString s = item->text( f_list_view->header()->sectionAt( point.x() ) );
    48.  
    49. QRect r( f_list_view->itemRect( item ) );
    50. r.setTop( std::max( r.top(), 0 ) );
    51. r.setLeft( std::max( r.left(), 0 ) );
    52. r.setHeight( std::max( f_list_view->viewport()->height(), r.height() ) );
    53. r.setWidth( std::min( f_list_view->viewport()->width(), r.width() ) );
    54. point = f_list_view->viewport()->mapToParent( r.topLeft() );
    55. r.setTopLeft( point );
    56. r.setHeight( std::max( f_list_view->height(), r.height() ) );
    57. r.setWidth( std::min( f_list_view->width(), r.width() ) );
    58.  
    59. qDebug( "void n_list_view_tooltip::maybeTip( %d, %d ): rect = (%d,%d,%d,%d); tip='%s'", p.x(), p.y(), r.top(), r.left(), r.width(), r.height(), ( const char * ) s.utf8() );
    60. tip( r, s );
    61. }
    To copy to clipboard, switch view to plain text mode 

    In my QListView I create an instance of this class and delete it after destruction of the class.

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

    Default Re: Tooltips for QListViewItems (QT3)?

    Does maybeTip get called at all? You might have some bugs in those series of if statements.

    Could you show how you create that QToolTip derived object?

Similar Threads

  1. Problems customizing tooltips in QListView
    By kalos80 in forum Qt Programming
    Replies: 6
    Last Post: 14th November 2007, 23:46
  2. Turning off ToolTips
    By steg90 in forum Qt Programming
    Replies: 11
    Last Post: 16th May 2007, 18:00
  3. Adding tooltips to canvas items
    By jnana in forum Qt Programming
    Replies: 2
    Last Post: 2nd March 2006, 11:53
  4. Tooltips on QActions
    By drhex in forum Newbie
    Replies: 1
    Last Post: 8th February 2006, 19:05
  5. Tooltips & Qt4 tree views
    By yogeshm02 in forum Newbie
    Replies: 7
    Last Post: 17th January 2006, 16:12

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.