Results 1 to 5 of 5

Thread: Background of a selected QItemDelegate

  1. #1
    Join Date
    Jan 2008
    Location
    Germany
    Posts
    80
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Background of a selected QItemDelegate

    Hi,

    Is there a simple way to use a gradient for the box displayed as a background of a selected Item in a class that derives from QItemDelegate ?
    I was first thinking to reimplement paint by doing something like:

    Qt Code:
    1. void
    2. MyDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& i ) const
    3.  
    4. {
    5.  
    6. // Special treatment for the selected state
    7. if (option.state & QStyle::State_Selected) {
    8.  
    9. QItemDelegate::paint( painter, option, i );
    10. QRect selectedRect = option.rect;
    11. selectedRect.setLeft(0);
    12. QLinearGradient linearGradient(0, selectedRect.top() , 0, selectedRect.top() + 100);
    13. linearGradient.setColorAt(0.0, QColor(93,148,214));
    14. linearGradient.setColorAt(0.2, QColor(25,86,173));
    15. painter->setBrush(linearGradient);
    16. painter->fillRect(selectedRect, linearGradient);
    17.  
    18. // But this is not convenient because it makes me need to reimplement also
    19. // the display of the check box, decoration, text,...
    20. .....
    21. .....
    22.  
    23. } else {
    24.  
    25. QItemDelegate::drawBackground( painter, option, i );
    26.  
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Germany
    Posts
    80
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Background of a selected QItemDelegate

    I just saw that QT4.4 comes with a new class called QStyledItemDelegate.
    I am wondering if using QStyledItemDelegate together with a style sheet would be simpler then reimplementing paint ?
    Does anyone has any project sample to look at ?

  3. #3
    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: Background of a selected QItemDelegate

    Quote Originally Posted by schall_l View Post
    I just saw that QT4.4 comes with a new class called QStyledItemDelegate.
    I am wondering if using QStyledItemDelegate together with a style sheet would be simpler then reimplementing paint ?
    Does anyone has any project sample to look at ?
    Yes, it is a lot easier. There are examples in docs:
    J-P Nurmi

  4. #4
    Join Date
    Jan 2008
    Location
    Germany
    Posts
    80
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Background of a selected QItemDelegate

    Is it possible to define style sheet for a custom class that derives directly from QStyledItemDelegate ?

    Something like this don't seems to work.

    Qt Code:
    1. class SideBarDelegate : public QStyledItemDelegate
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. void paint( QPainter *painter , const QStyleOptionViewItem &option, const QModelIndex &index ) const;
    7. QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const;
    8. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. SideBarDelegate::item:selected:active{
    2. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6ea1f1, stop: 1 #567dbc);
    3. }
    4.  
    5. SideBarDelegate::item:selected:!active {
    6. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6b9be8, stop: 1 #577fbf);
    7. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    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: Background of a selected QItemDelegate

    You must call the base class implementation; QStyledItemDelegate::paint(). What do you need a reimplementation for?
    J-P Nurmi

Similar Threads

  1. background colour
    By kw in forum Qt Programming
    Replies: 6
    Last Post: 11th April 2006, 00:44
  2. Replies: 1
    Last Post: 5th April 2006, 16:44

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.