Results 1 to 5 of 5

Thread: Delegate doesn't paint properly

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

    Default Delegate doesn't paint properly

    Hi everybody

    I try to create my own delegate for a list view.
    I'd like to draw my list items similar to the default behaviour. That is a picture on the left side and than a text. But the text is in html format to support some formation as different text size and text colour.

    So I implemented my ouwn QStyledItemDelegate::paint method. But my items are not painted properly.
    The first item is painted as supposed. But for the other items only the image is painted.
    When I scroll the list view then sometimes other items are painted. But the text is drawn to far left and under the images.

    Qt Code:
    1. void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    2. if(!index.isValid())
    3. return;
    4. painter->save();
    5. // handle selection
    6. if(option.state & QStyle::State_Selected){
    7. painter->fillRect(option.rect, option.palette.color(QPalette::Highlight));
    8. }
    9. // get the oicture for the decoration role
    10. QIcon ic = QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)));
    11.  
    12. QString txt = index.data(Qt::DisplayRole).toString();
    13. doc.setHtml(txt);
    14.  
    15. // rectangle for the icon
    16. QRect r = option.rect.adjusted(2, 2, -2, -2);
    17. ic.paint(painter, r, Qt::AlignVCenter|Qt::AlignLeft);
    18.  
    19. // rectangle for the text
    20. r = r.adjusted(60, 0, 0, 0);
    21. doc.drawContents(painter, r);
    22. painter->restore();
    23. }
    To copy to clipboard, switch view to plain text mode 

    Any suggestions?

    Thx Luke
    Attached Images Attached Images
    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
    Jul 2008
    Location
    Germany
    Posts
    507
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Delegate doesn't paint properly

    Hi, check the values of r if they are what you expected.

    Ginsengelf
    Last edited by Ginsengelf; 10th August 2010 at 15:38. Reason: updated contents

  3. #3
    Join Date
    Sep 2009
    Location
    Aachen, Germany
    Posts
    60
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Delegate doesn't paint properly

    option.rect doesn't necessarily return the visible rect of the current index. You should try this:

    Qt Code:
    1. QStyleOptionViewItemV4 opt = option;
    2. QStyledItemDelegate::initStyleOption(&opt, index);
    3. QRect rect = opt.rect;
    To copy to clipboard, switch view to plain text mode 

    From the Qt doc:
    When reimplementing paint in a subclass. Use the initStyleOption() to set up the option in the same way as the QStyledItemDelegate; the option will always be an instance of QStyleOptionViewItemV4. Please see its class description for information on its contents.

    Whenever possible, use the option while painting. Especially its rect variable to decide where to draw and its state to determine if it is enabled or selected.
    Last edited by ChiliPalmer; 10th August 2010 at 15:43.

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

    Default Re: Delegate doesn't paint properly

    Thanks for the hint.

    It's still not working. But I'll try to toy around with the given code and read the docs.
    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

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

    Default Re: Delegate doesn't paint properly

    Ok it's working now (more or less):

    Qt Code:
    1. void MyDelegate::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. QPen selectionPen(option.palette.color(QPalette::Highlight));
    16. selectionPen.setWidth(2);
    17. painter->setPen(selectionPen);
    18. painter->drawRoundedRect(rect.adjusted(1,1,-1,-1), 5, 5);
    19. painter->restore();
    20. }
    21.  
    22. QIcon ic = QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)));
    23. rect = rect.adjusted(4, 4, -4, -4);
    24. /// @todo if ic.isNull() ...
    25. ic.paint(painter, rect, Qt::AlignVCenter|Qt::AlignLeft);
    26.  
    27. QAbstractTextDocumentLayout::PaintContext context;
    28.  
    29. QString txt = index.data(Qt::DisplayRole).toString();
    30. doc.setHtml(txt);
    31.  
    32. // icon needs at most 60 pixels in y-axis
    33. rect = rect.adjusted(62, 0, 0, 0);
    34. doc.setPageSize(QSize(rect.width(), rect.height()));
    35. painter->translate(rect.x(),rect.y());
    36. doc.documentLayout()->draw(painter, context);
    37.  
    38. painter->restore();
    39. }
    To copy to clipboard, switch view to plain text mode 

    Thaks for your help!
    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

Similar Threads

  1. QListWidget delegate paint problem.
    By rule in forum Qt Programming
    Replies: 0
    Last Post: 5th July 2010, 13:31
  2. Replies: 1
    Last Post: 22nd March 2010, 14:38
  3. QTableView doesn't use delegate
    By treaves in forum Qt Programming
    Replies: 3
    Last Post: 1st February 2010, 09:45
  4. Delegate paint optimisation
    By bunjee in forum Qt Programming
    Replies: 11
    Last Post: 21st April 2008, 19:48
  5. QGLWidget doesn't paint when I add another data member
    By Thoughtjacked in forum Qt Programming
    Replies: 6
    Last Post: 17th October 2007, 15:45

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.