Results 1 to 7 of 7

Thread: QItemDelegate + QListView : paint

  1. #1
    Join Date
    Mar 2014
    Posts
    23
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default QItemDelegate + QListView : paint

    Hi guys,

    I'm trying to polish a small tool I wrote and therefore want to implement an item delegate for my list view.

    Scenario:
    There's a QStandardItemModel which is assigned to the list view. The view also has a delegate.
    I just added to items to the model which are shown without any problem.

    Issue:
    There's something wrong with the hovering background color. Please see my code below.

    Qt Code:
    1. painter->setPen(Qt::NoPen);
    2. painter->setBrush(QColor(0, 0, 255));
    3.  
    4. if(option.state & QStyle::State_MouseOver)
    5. {
    6. painter->setBrush(QColor(255, 0, 0));
    7. }
    8.  
    9. painter->drawRect(option.rect);
    10.  
    11. painter->setPen(fontColor);
    12. painter->drawText(option.rect.left() + 5, option.rect.top() + option.rect.height() / 2 + 4, qvariant_cast<QString>(index.data()));
    To copy to clipboard, switch view to plain text mode 

    The item's text is drawn correctly at the desired position. But the background is drawn over both items.

    The sizeHint() return this:
    Qt Code:
    1. return QSize(option.rect.width(), 20);
    To copy to clipboard, switch view to plain text mode 

    I'm sure I'm missing something or am doing something wrong.
    Could anybody please shed some light? :-)

    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QItemDelegate + QListView : paint

    Have you tried saving and restoring the old values for pen and brush, i.e. not permanently change painter state?

    Something like
    Qt Code:
    1. const QPen oldPen = painter->pen(),
    2. const QBrush oldBrush = painter->brush();
    3.  
    4. // your code
    5.  
    6. painter->setPen(oldPen);
    7. painter->setBrush(oldBrush);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. #3
    Join Date
    Mar 2014
    Posts
    23
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: QItemDelegate + QListView : paint

    Well, the issue is not related to wrong painter states but rather to the area where the painter draws the rectangle for the item background.
    I assumed that option.rect would deliver the exact boundaries of the hovered item, but if I hover over the first item in the view the second is also highlighted.

    Don't know if my explanation makes it clear, please let me know if more details/code is required to help.

    Thanks.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QItemDelegate + QListView : paint

    Hmm, weird, so option.rect is larger than the cell of that list view entry?

    But the text positioning on the same values is correct at the same time?

    Cheers,
    _

  5. #5
    Join Date
    Mar 2014
    Posts
    23
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: QItemDelegate + QListView : paint

    Exactly! That's what makes me think I'm doing something wrong with the background. Just can't see what that would be.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QItemDelegate + QListView : paint

    Have you tried not changing the brush but using QPainter::fillRect() instead?

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    Killian (11th October 2016)

  8. #7
    Join Date
    Mar 2014
    Posts
    23
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: QItemDelegate + QListView : paint

    I also tried that but still to no avail. So I guess there must be something wrong somewhere else in my delegate class. I've done delegates so many times but maybe I just dont see the wood for the trees.
    Anyway, it's just an esthetic thingy so I won't spend any more time on it right now.

    Thanks for your ideas!

Similar Threads

  1. Best practice for QItemDelegate (paint() needs underlying data)
    By kiss-o-matic in forum Qt Programming
    Replies: 1
    Last Post: 24th January 2013, 05:37
  2. Reimplement paint() in QItemDelegate
    By john_erlandsson in forum Newbie
    Replies: 4
    Last Post: 19th January 2012, 20:37
  3. High cpu usage in custom QItemDelegate::paint
    By rsilva in forum Qt Programming
    Replies: 1
    Last Post: 17th May 2011, 22:52
  4. QItemDelegate paint() is not called
    By Cortex in forum Qt Programming
    Replies: 2
    Last Post: 10th January 2010, 20:03
  5. Drawing a widget in QItemDelegate's paint method
    By darkadept in forum Qt Programming
    Replies: 17
    Last Post: 11th August 2009, 05:15

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.