Results 1 to 5 of 5

Thread: Delegate doesn't paint properly

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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.