Results 1 to 8 of 8

Thread: [SOLVED] QTreeView drawing selection with icons

  1. #1
    Join Date
    Jan 2006
    Location
    Germany - Mannheim
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [SOLVED] QTreeView drawing selection with icons

    Hi all,

    I'm trying to display 3 icons in the first 3 columns of a QTreeView. I did this by creating a model and returning a QIcon in the DecorationRole of the data function (the icon depends on the data of the item). This works quite ok but when selecting a row the selection looks quite ugly imo (as you can see in the first pic I attached). Because of the icons are placed very close to each other the selection looks more like a separator than a selection and I'm looking for a solution like in the second pic attached (taken from eMule) where the selection surrounds the icon.

    The problem I'm facing now is that I don't really know where to start.

    I think either my approach to use the decoration is not that good and drawing the icon in another way (but which one ?) would solve ths problem automatically ...

    or

    I have to reimplement the QTreeView class and handle drawing the selection by myself. But also here I'm not really sure where to start.

    any ideas about this ?
    Attached Images Attached Images
    Last edited by Timewarp; 22nd January 2006 at 14:27.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeView drawing selection with icons

    Leave the treeview alone for now. I think reimplementing the delegate (its paint method) will suffice. If not, reimplement QTreeView::drawRow().

  3. #3
    Join Date
    Jan 2006
    Location
    Germany - Mannheim
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView drawing selection with icons

    Ok got it working now. Reimplementing the delegate worked.
    drawDecoration() only colors the pixmap itself (not the remaining space)
    when a row is selected in qtemdelegate.
    Attached Images Attached Images

  4. #4
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeView drawing selection with icons

    Hi Timewarp. I see that you had a problem similar that mine. I have inserted an example of my problem. Is like your a difference that in my case the icons are in different columns. But how you can see the icon of the book when is selected looks bad. I don't know why because the background of the icon is transparent. Do you or anybody else know how to solve this? Any example of code will be apreciated. Thanks.
    Attached Images Attached Images
    Last edited by SkripT; 23rd January 2006 at 13:24.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany - Mannheim
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView drawing selection with icons

    my icons are in different columns too so I would say this is exactly the same problem I had.
    I solved it by deriving my own delegate class from QItemDelegate and reimplemented the paint() function. If you have a look at the paint function (qitemdelegate.cpp) you can see that the drawing of the text (drawDisplay) and icons (drawDecoration) is done in the last lines.
    I replaced this code by mine.

    [Edit] But when looking at it closer this works only for columns with just an icon.
    When it worked I hadn't looked at it closer. The text will be printed below the icon in this case.[/Edit]

    [Edit2] I removed the not complete working code but this should work better now:

    Qt Code:
    1. //paint()
    2. // draw the item
    3. drawCheck(painter, opt, checkRect, checkState);
    4.  
    5. QRect newRect;
    6.  
    7. newRect.setTop(textRect.top());
    8. newRect.setBottom(textRect.bottom());
    9. newRect.setLeft(pixmapRect.left() );
    10. newRect.setRight(textRect.left());
    11.  
    12. drawDecoration(painter, opt, pixmapRect, pixmap, newRect);
    13. drawDisplay(painter, opt, textRect, text);
    14. drawFocus(painter, opt, textRect);
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. //new drawDecoration
    2. void ServerDelegate::drawDecoration(QPainter *painter, const QStyleOptionViewItem &option,
    3. const QRect &rect, const QPixmap &pixmap, const QRect &newRect) const
    4. {
    5. if(!pixmap.isNull() && !rect.isEmpty())
    6. {
    7. if(option.state & QStyle::State_Selected)
    8. {
    9. bool enabled = option.state & QStyle::State_Enabled;
    10. QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
    11.  
    12. QPixmap *pm = selected(pixmap, option.palette, enabled);
    13. painter->fillRect(newRect, option.palette.brush(cg, QPalette::Highlight));
    14. painter->drawPixmap(rect.topLeft(), *pm);
    15. }
    16. else
    17. {
    18. painter->drawPixmap(rect.topLeft(), pixmap);
    19. }
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Timewarp; 23rd January 2006 at 20:39.

  6. #6
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeView drawing selection with icons

    Many thanks Timewrap. I will try it.

  7. #7
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeView drawing selection with icons

    Hi, only comment that this problem is automaticly solved in the new version of Qt (4.1.0)

  8. #8
    Join Date
    Oct 2012
    Location
    Gurgaon, Haryana, India
    Posts
    15
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeView drawing selection with icons

    Hi, Can u provide me code to display 3 icon in a row?

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.