Results 1 to 3 of 3

Thread: Paint selection in QStyledItemDelegate's subclass

  1. #1
    Join Date
    Mar 2010
    Posts
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Paint selection in QStyledItemDelegate's subclass

    Hi there

    For a list in my application I use a delegate that is derived from QStyledItemDelegate. In the paint method I'd like to handle the selection of the items in the list.

    Now I'm doing it like this:
    Qt Code:
    1. void MembersListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    2. if(!index.isValid())
    3. return;
    4.  
    5. painter->save();
    6. painter->setRenderHint(QPainter::Antialiasing);
    7.  
    8. QStyleOptionViewItemV4 opt = option;
    9. QStyledItemDelegate::initStyleOption(&opt, index);
    10. QRect rect = opt.rect;
    11.  
    12. // handle selection
    13. if(option.state & QStyle::State_Selected){
    14. painter->save();
    15.  
    16. QBrush selectionBrush(Qt::green);
    17. painter->setBrush(selectionBrush);
    18. painter->drawRoundedRect(rect.adjusted(1,1,-1,-1), 5, 5);
    19.  
    20. painter->restore();
    21. }
    22.  
    23. // ...
    24. painter->restore();
    25. }
    To copy to clipboard, switch view to plain text mode 

    But the selection doesn't look exactly the same as the default one which is used when no delegate is set. How can I use the standard selection in my delegate.

    Btw: I'm using Qt on Symbian (Nokia N8) where the background of the list has black color and the selection is a green rounded rect with some kind of gradient.

    Thanks!
    Using Qt 4.7
    Developping on Win 7 and XP
    Using Qt Creator, Eclipse and Visual Studio
    Target Platforms Win, Linux and soon OS X

  2. #2
    Join Date
    Mar 2010
    Posts
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Paint selection in QStyledItemDelegate's subclass

    I figured it out:

    In the paint function of my delegate I first call the paint function of the QStyledItemDelegate.
    And then I just don't paint any selection myself anymore.

    Qt Code:
    1. void MembersListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    2. if(!index.isValid())
    3. return;
    4.  
    5. QStyledItemDelegate::paint(painter, option, index);
    6.  
    7. painter->save();
    8. painter->setRenderHint(QPainter::Antialiasing);
    9.  
    10. QStyleOptionViewItemV4 opt = option;
    11. QStyledItemDelegate::initStyleOption(&opt, index);
    12. QRect rect = opt.rect;
    13.  
    14. // ... All my custom painting stuff here
    15.  
    16.  
    17. painter->restore();
    18. }
    To copy to clipboard, switch view to plain text mode 



    Cheers Luke
    Using Qt 4.7
    Developping on Win 7 and XP
    Using Qt Creator, Eclipse and Visual Studio
    Target Platforms Win, Linux and soon OS X

  3. #3
    Join Date
    Apr 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Paint selection in QStyledItemDelegate's subclass

    I have solved my problem by setting mouseTracking option in my QTableView.
    P.S. Sorry for my english.

Similar Threads

  1. QStyledItemDelegate and editor pointer
    By Nero in forum Qt Programming
    Replies: 10
    Last Post: 20th September 2010, 18:42
  2. Replies: 8
    Last Post: 12th February 2010, 02:41
  3. Question about subclassing QStyledItemDelegate
    By dpimka in forum Qt Programming
    Replies: 4
    Last Post: 10th July 2009, 17:09
  4. Setting Format in a QStyledItemDelegate
    By Airswoop1 in forum Qt Programming
    Replies: 0
    Last Post: 1st June 2009, 22:01
  5. How to paint a selection rectangle on a pixmap?
    By SkripT in forum Qt Programming
    Replies: 6
    Last Post: 8th January 2006, 19:52

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.