Results 1 to 4 of 4

Thread: QTreeView with varying icon size

  1. #1
    Join Date
    Sep 2011
    Posts
    16
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default QTreeView with varying icon size

    Hi everyone,

    I have a question regarding the default behaviour of QTreeView. I have a tree view with various different items like folders and file and each have a different icon of different size. The folder's icon is 28x22 and the file's icon is 18x21. When displaying the view, Qt seems to have attempt to keep the width fixed to 18 and a value close to that and the icon of the folder is shrunk to fit. So far, the only way I found to make QTreeView accommodate for the largest icon is to can setIconSize with the size of the largest icon as the parameter.

    Wondering what's the default behaviour of QTreeView when it comes to icon size? Is calling setIconSize my best bet?

    Thuan

  2. #2
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeView with varying icon size

    I had almost the same problem but in my case was with menuItem icons...your solution is possible. But I was recommended to make something like this:

    //header
    Qt Code:
    1. #ifndef ICONSIZE_H
    2. #define ICONSIZE_H
    3.  
    4. #include <QObject>
    5. #include <QStyle>
    6. #include <QWindowsStyle>
    7.  
    8. class IconSize : public QWindowsStyle
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit IconSize(QWindowsStyle *parent = 0);
    13. int pixelMetric(PixelMetric metric, const QStyleOption * option = 0, const QWidget * widget = 0 ) const;
    14.  
    15. signals:
    16.  
    17. public slots:
    18.  
    19. };
    To copy to clipboard, switch view to plain text mode 

    //CPP
    Qt Code:
    1. #include "iconsize.h"
    2.  
    3. IconSize::IconSize(QWindowsStyle *parent) :
    4. {
    5. Q_UNUSED (parent);
    6. }
    7.  
    8. int IconSize::pixelMetric(PixelMetric metric, const QStyleOption * option, const QWidget * widget)const
    9. {
    10. int s = QWindowsStyle::pixelMetric(metric, option, widget);
    11. if (metric == QStyle::PM_SmallIconSize) {
    12. s = 23;
    13. }
    14. return s;
    15. }
    To copy to clipboard, switch view to plain text mode 

    I reimplemented the necessary methods (in this case only one) of the Windows Style that is the one I use for my application.

  3. #3
    Join Date
    Sep 2011
    Posts
    16
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTreeView with varying icon size

    Thanks for the response.

    I looked into Qt source code and it seems for some reason if you have a QIcon, it will try to find the best icon that is no larger than the size specified by the decorationSize of the option. If you have a QPixmap, it will actually use the size of the image. Which in my situation, the decorationSize defaults to 16x16 (i.e. small icon) and during the paint, it draws the icon for file which is a QPixmap correctly, but shrinks the QIcon for the folder.

    For my case, I ended up calling setIconSize() which seems simpler to me.

  4. #4
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeView with varying icon size

    IMHO you should correct data model.
    Note that there is a Qt::SizeHintRole.
    So subclass data model you are using and override QAbstractItemModel::data (and/or: QAbstractItemModel::itemData)to provide Qt::SizeHintRole value.

    It can be also done on view level by overriding QAbstractItemView::sizeHintForRow.

    Playing with delegate can also do the trick.
    Last edited by MarekR22; 2nd February 2012 at 10:08.

Similar Threads

  1. Replies: 6
    Last Post: 10th February 2011, 12:10
  2. Add icon to the right in a QTreeView
    By hubbobubbo in forum Qt Programming
    Replies: 3
    Last Post: 21st April 2010, 20:01
  3. qtoolbox and icon size
    By Talei in forum Newbie
    Replies: 1
    Last Post: 2nd February 2010, 09:39
  4. QTreeView ICON
    By Raymond in forum Qt Programming
    Replies: 2
    Last Post: 28th October 2009, 03:43
  5. Size of an Icon
    By LordQt in forum Qt Programming
    Replies: 7
    Last Post: 26th August 2007, 22:36

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.