Results 1 to 4 of 4

Thread: QT3: mouseover effect in QListViewItem

  1. #1
    Join Date
    Jan 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default QT3: mouseover effect in QListViewItem

    I'd like to hightlight the QListViewItem pixmap when mouse is over it.
    Any one has a pointer to some code.

  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: QT3: mouseover effect in QListViewItem

    I don't have a pointer to any code but I can point you in the right direction. Use QListView::onItem() signal and connect it to a custom slot that will change some flag in the item so that it's drawn differently (an easiest way would be to use the setSelected() method). You'll probably have to subclass the item to implement that flag and custom painting of course. I take it that you know how to do it if you ask the question in this section...

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

    karye (1st April 2006)

  4. #3
    Join Date
    Jan 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: QT3: mouseover effect in QListViewItem

    Nice!
    But I need mouseOut also, or repaint all with triggerUpdate() maybe?

  5. #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: QT3: mouseover effect in QListViewItem

    onItem will surely return a null pointer if you leave the view. You can act on it then by making sure none of the items are highlighted.

    Something like that should (more or less) work:

    Qt Code:
    1. class MyListView : public QListView {
    2. Q_OBJECT
    3. public:
    4. MyListView ( QWidget * parent = 0, const char * name = 0, WFlags f = 0 );
    5. private:
    6. QListViewItem *_highlighted;
    7. protected:
    8. void drawContentsOffset ( QPainter * p, int ox, int oy, int cx, int cy, int cw, int ch );
    9. private slots:
    10. void highlightItem(QListViewItem *);
    11. }
    12.  
    13. MyListView ( QWidget * parent, const char * name, WFlags f) : QListView ( parent, name, f ) {
    14. _highlighted = 0;
    15. connect(this, SIGNAL(onItem(QListViewItem*)), this, SLOT(highlightitem(QListViewItem*)));
    16. }
    17.  
    18. void MyListView::highlightItem(QListViewItem *item){
    19. _highlighted = item;
    20. updateContents();
    21. }
    22.  
    23. void MyListView::drawContentsOffset ( QPainter * p, int ox, int oy, int cx, int cy, int cw, int ch ){
    24. // reimplement from QListView (for example copy & paste)
    25. // there should be some loop there which iterates over items
    26. // I assume that 'item' is a pointer to an item currently drawn
    27.  
    28.  
    29. QColorGroup cg = //...
    30. QColorGroup storecg = cg;
    31. if(item==_highlighted){
    32. cg.setColor(QColorGroup::Base, QColor(255,255,0)); // could be Background not Base
    33. }
    34. // do some stuff here probably
    35. // eventually there will be a call like so:
    36. item->paintCell(p, cg, ...);
    37. // then you can restore the background
    38. if(item==_highlighted){
    39. cg = storecg;
    40. }
    41. // ...
    42. }
    To copy to clipboard, switch view to plain text mode 

    Just make sure that when an item is deleted, it doesn't segfault on the _highlighted pointer (it should be ok if the code looks like the one above).

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

    karye (1st April 2006)

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.