Results 1 to 4 of 4

Thread: QItemDelegate and QStyle::State_Selected

  1. #1
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default QItemDelegate and QStyle::State_Selected

    Hi to all,
    I'm looking for a solution to my QItemDelegate applied to a QTreeView:

    ColumnIconDelegate.h
    Qt Code:
    1. #ifndef COLUMNICONDELEGATE_H
    2. #define COLUMNICONDELEGATE_H
    3.  
    4. #include <QtGui>
    5.  
    6. class ColumnIconDelegate: public QItemDelegate
    7. {
    8. public:
    9. ColumnIconDelegate(QObject *parent = 0);
    10.  
    11. void paint(QPainter *painter, const QStyleOptionViewItem &option,
    12. const QModelIndex &index) const;
    13. };
    14.  
    15. #endif // COLUMNICONDELEGATE_H
    To copy to clipboard, switch view to plain text mode 

    ColumnIconDelegate.cpp
    Qt Code:
    1. #include "ColumnIconDelegate.h"
    2.  
    3. /*----------------------------------------------------------------------------*/
    4.  
    5. ColumnIconDelegate::ColumnIconDelegate(QObject *parent)
    6. : QItemDelegate(parent)
    7. {
    8. //qDebug()() << "ColumnDateDelegate Costruttore";
    9. }
    10.  
    11. /*----------------------------------------------------------------------------*/
    12.  
    13. void ColumnIconDelegate::paint(QPainter *painter,
    14. const QStyleOptionViewItem &option, const QModelIndex &index) const
    15. {
    16. QPixmap IconCalendar(48, 48);
    17. IconCalendar.load(":sottomenu/images/calendar-empty.png", "PNG", Qt::AutoColor);
    18.  
    19. QStyleOptionViewItem myOption = option;
    20. myOption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
    21.  
    22. int pixIcona;
    23.  
    24. if (myOption.rect.height() > 48) pixIcona = 48;
    25. else pixIcona = (myOption.rect.height());
    26.  
    27. painter->drawPixmap(myOption.rect.x(),myOption.rect.y(),pixIcona,pixIcona,IconCalendar);
    28.  
    29. drawDisplay(painter, myOption, myOption.rect, "");
    30. drawFocus(painter, myOption, myOption.rect);
    31. }
    To copy to clipboard, switch view to plain text mode 

    it shows the icon when the item is NOT selected (see picture Before_Click.png) but it disappears after the click (see picture After_Click.png),so what are I missing?
    thanks
    Attached Images Attached Images
    Last edited by cydside; 25th July 2010 at 17:13.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QItemDelegate and QStyle::State_Selected

    You have to call drawDisplay and drawFocus fist, then paint your icon. Right now you icon is covered by the later drawn blue background!

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

    cydside (25th July 2010)

  4. #3
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QItemDelegate and QStyle::State_Selected

    Quote Originally Posted by Lykurg View Post
    You have to call drawDisplay and drawFocus fist, then paint your icon. Right now you icon is covered by the later drawn blue background!
    Ops, thanks!

  5. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QItemDelegate and QStyle::State_Selected

    You can also simply override QItemDelegate::drawDecoration if you want to change only the icon.

Similar Threads

  1. Replies: 2
    Last Post: 14th July 2011, 12:28
  2. ComboBox QStyle
    By deMarco in forum Qt Programming
    Replies: 6
    Last Post: 11th January 2010, 17:18
  3. QStyle's question
    By aoo7wangyan in forum Qt Programming
    Replies: 1
    Last Post: 15th July 2009, 09:42
  4. how to rewrite QStyle
    By trytoremeber963 in forum Newbie
    Replies: 3
    Last Post: 28th April 2009, 08:18
  5. Using QStyle and StyleSheet
    By informatics in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2007, 16:46

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.