Results 1 to 9 of 9

Thread: QListView + QStyledItemDelegate + text besides icon

  1. #1
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default QListView + QStyledItemDelegate + text besides icon

    Hi,

    could anybody give me a hint on how to show a listview (ViewMode=QListView::IconMode) where the text is at the left/right side of the image?
    Because per default the text is shown under the icon which isn't what i want.

    Best Regards
    NoRulez

  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: QListView + QStyledItemDelegate + text besides icon

    I can't verify it right now, but should that be possible by ViewMode=QListView::ListMode with Flow=QListView::LeftToRight?

  3. #3
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: QListView + QStyledItemDelegate + text besides icon

    But it should be in IconMode, i thought that it could be done easily with a styled item delegate

  4. #4
    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: QListView + QStyledItemDelegate + text besides icon

    Yeah, of course, you can subclass QItemDelegate and do the painting yourself. What is exactly the problem?

  5. #5
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: QListView + QStyledItemDelegate + text besides icon

    Currently the problem is that i don't know on which member function/variable I must set the alignment.
    I already tried viewItemPosition from QStyleOptionViewItemV4, but the result isn't what i want.

  6. #6
    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: QListView + QStyledItemDelegate + text besides icon

    Well you need to calculate the new size yourself, since you need more space if you draw the text beside the icon. And then in the paint method you simply call drawImage() and drawText().

  7. #7
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: QListView + QStyledItemDelegate + text besides icon

    I mean something like the following in the image, which i had attached.
    Could you please help me.

    Thanks in advance

    Best Regards
    NoRulez
    Attached Images Attached Images

  8. #8
    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: QListView + QStyledItemDelegate + text besides icon

    1. Subclass QItemDelegate.
    2. reimp QItemDelegate::sizeHint() where you return the minimum needed space for the item
    3. reimp QItemDelegate::paint() where you paint the pixmap, and the two text lines using the normal QPainter structure.


    Try it and if you stuck, ask again, but please show what you have done so far. (Of course you also need a proper model...)

    EDIT: There are also examples in the documentation on how to create custom delegates.

  9. #9
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: QListView + QStyledItemDelegate + text besides icon

    So, here is what i have so far:
    Qt Code:
    1. void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    2. QStyleOptionViewItemV4 options = option;
    3. initStyleOption(&options, index);
    4.  
    5. painter->save();
    6. doc.setHtml(options.text);
    7. // Set the maximum text width
    8. //doc.setTextWidth(75);
    9.  
    10. // Calculate the icon size
    11. QIcon icon = options.icon;
    12. QSize iconSize = options.icon.actualSize(options.rect.size());
    13. iconSize.scale(iconSize.width() - qRound(doc.idealWidth()), iconSize.height(), Qt::KeepAspectRatio);
    14.  
    15. options.text = "";
    16. options.icon = icon.pixmap(iconSize);
    17. options.decorationAlignment = Qt::AlignLeft |Qt::AlignVCenter;
    18. options.decorationPosition = QStyleOptionViewItem::Left;
    19.  
    20. QStyle *style = options.widget ? options.widget->style() : QApplication::style();
    21. style->drawControl(QStyle::CE_ItemViewItem, &options, painter, options.widget);
    22.  
    23. // To support html tags in the text
    24. painter->translate(options.rect.left() + iconSize.width(), options.rect.top());
    25. QRect clip(0, 0, options.rect.width() + iconSize.width(), options.rect.height());
    26. doc.drawContents(painter, clip);
    27.  
    28. painter->restore();
    29. }
    30.  
    31. QSize ItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const {
    32. QStyleOptionViewItemV4 options = option;
    33. initStyleOption(&options, index);
    34.  
    35. doc.setHtml(options.text);
    36. doc.setTextWidth(options.rect.width());
    37.  
    38. QSize size = QStyledItemDelegate::sizeHint(option, index);
    39. return QSize(size.width() > doc.idealWidth() ? size.width() : doc.idealWidth(),
    40. size.height() > doc.size().height() ? size.height() : doc.size().height());
    41. }
    To copy to clipboard, switch view to plain text mode 

    I think there is only a small problem in the implementation, but i couldn't find it.
    The problem is that the selection border is to big (height) and cutted and the hover border is also cutted. I think this has something to do with:
    Qt Code:
    1. options.decorationAlignment = Qt::AlignLeft |Qt::AlignVCenter;
    To copy to clipboard, switch view to plain text mode 

    But i don't know how to solve this issue

    I've attached a screenshot of the misbehaviour

    Best Regards
    NoRulez
    Attached Images Attached Images

Similar Threads

  1. Icon margin in QListView
    By mpi in forum Qt Programming
    Replies: 4
    Last Post: 1st July 2010, 13:56
  2. Replies: 2
    Last Post: 19th February 2010, 12:58
  3. icon glitch in QListView
    By trhaynes in forum Qt Programming
    Replies: 1
    Last Post: 31st August 2009, 19:48
  4. QListView with image and text!
    By Faster in forum Newbie
    Replies: 10
    Last Post: 28th June 2009, 14:53
  5. QListView icon double clicking
    By been_1990 in forum Qt Programming
    Replies: 8
    Last Post: 28th April 2009, 20:05

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.