Results 1 to 4 of 4

Thread: Not drawing the rubberband around the focus item in QListWidget

  1. #1
    Join Date
    Oct 2006
    Posts
    18
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Not drawing the rubberband around the focus item in QListWidget

    The title says it all, I don't like the rubber band (that stroked rectangle you get around an item after you click it), so I subclassed QCommonStyle to override it:
    Qt Code:
    1. class MyListWidgetStyle : public QCommonStyle
    2. {
    3. public:
    4. MyListWidgetStyle () : QCommonStyle() {}
    5. ~MyListWidgetStyle () {}
    6.  
    7. virtual void drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget = 0) const
    8. {
    9. switch(element)
    10. {
    11. case CE_FocusFrame: //surpressing these
    12. case CE_RubberBand:
    13. return;
    14. default:
    15. QApplication::style()->drawControl(element, option, painter, widget);
    16. }
    17. }
    18. };
    To copy to clipboard, switch view to plain text mode 
    And then in MyListWidget constructor's I call setStyle(new MyListWidgetStyle()). The only problem is that it doesn't do anything. In fact, drawControl is never called. What could be the reason for that? The only thing I could think of is that the application style isn't calling drawControl at all (which seems far-fetched, but okay), but when I override the entire application's style with this, it still doesn't work.
    Any ideas or suggestions?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Not drawing the rubberband around the focus item in QListWidget

    Quote Originally Posted by Antrax View Post
    Any ideas or suggestions?
    Yes, it's extremely easy to disable drawing of the focus rect by delegates. All you have to do is to subclass QItemDelegate and override drawFocus() with an empty implementation.

    Qt Code:
    1. class MyItemDelegate : public QItemDelegate
    2. {
    3. public:
    4. MyItemDelegate(QObject* parent = 0) : QItemDelegate(parent) {}
    5.  
    6. protected:
    7. void drawFocus(QPainter* painter, const QStyleOptionViewItem& option, const QRect& rect) const {}
    8. };
    9.  
    10. // usage
    11. listWidget->setItemDelegate(new MyItemDelegate(listWidget));
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    Antrax (16th October 2006)

  4. #3
    Join Date
    Oct 2006
    Posts
    18
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Not drawing the rubberband around the focus item in QListWidget

    Oh wow, that WAS simple. Thanks. I guess I'll have to understand what those delegates are, I didn't read too much about it because I have no need for anything elaborate with the model/view pattern, I just wanted a list box :-)

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Not drawing the rubberband around the focus item in QListWidget

    If you don't need focus at all, you can simply do:
    Qt Code:
    1. listWidget->setFocusPolicy(Qt::NoFocus);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

Similar Threads

  1. Reg - add item in QListWidget
    By suresh in forum Newbie
    Replies: 1
    Last Post: 1st September 2006, 17:52
  2. extract item from QListWidget
    By impeteperry in forum Qt Programming
    Replies: 2
    Last Post: 13th May 2006, 19:41
  3. keypress while editing an item in QListWidget
    By Beluvius in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2006, 09:56

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.