2

How does one set the icon size for items within a Qtreewidget? So far I have tried

QTreeWidget.setIconSize(QSize(32,32))

But all that does is increase the width, not the height.

However, a

print QTreeWidget.iconSize()

shows the correct result : PyQt4.QtCore.QSize(32, 32)

Does anyone have a better understanding of how QTreewidget works?

p.i.g.
  • 2,815
  • 2
  • 24
  • 41
regomodo
  • 644
  • 3
  • 9
  • 18

2 Answers2

7

Jason had some of the solution. The height of the treeview's rows needs to be resized as well:

class MyDelegate(QItemDelegate):      
    def __init__(self):    
        QItemDelegate.__init__(self)  

    def sizeHint(self, option, index):  
        return QSize(32,32)

Then, elsewhere:

delegate = MyDelegate()   
tree = QTreeWidget()
tree.setItemDelegate(delegate)

Not ideal as it resizes every row.

[edit] If you want to vary the size of the rows ensure the QTreeWidget/View.uniformRowHeights == False

Then mess around using the index. For me I wanted the 2nd row to bigger than the rest. I'm sure there's a better way but my sizeHint became:

def sizeHint(self,option,index):
    parent = index.parent()
    if parent.isValid() and  not parent.parent().isValid():            
        return QSize(32,32)
    return QSize(24,24)

However, there's another issue with this. Icons are not resizeable. Ah! That has to be done with

QTreeWidget.setIconSize(QSize(width,height))

regomodo
  • 644
  • 3
  • 9
  • 18
  • This is very helpfull! Thanks. Curiously how can I signal that delegate sizeHint? If I have a button inside my QTreeWidgetItem that I'd like to click to refresh the widget height/size. How can I link it? – Dariusz Mar 23 '17 at 01:13
1

Looks to me like you need to adjust the row height of the QTreeView to accommodate the larger icons. From what I can see in the pictures, it looks like the icons are being resized properly, but because the rows are not tall enough and the icons are being cropped. See this for how to change the row heights.

Jason B
  • 12,835
  • 2
  • 41
  • 43
  • Cheers,that really helped. Although not entirely perfect; I only need the 2nd level (album-level) to be bigger. So far I get every row to be bigger although I reckon with some extra research/experimentation I might be able to sort that out. – regomodo Jan 19 '11 at 19:17
  • You will have to override the sizeHint() function of your item delegate. You can tell where you are in the tree from the `QModelIndex` that is passed into the `sizeHint()` function and return the bigger size if at the 2nd level. You can test if your at the 2nd level by testing whether or not `index.parent()` returns an empty `QModelIndex()` or not. – Jason B Jan 19 '11 at 19:29
  • I tried using the QModelIndex being returned but the index is always the same. print index.parent().data().toString() always show's the same thing; nothing. Could be going about this all wrong though; just found this: QTreeWidgetItem.setSizeHint (self, int column, QSize size) – regomodo Jan 19 '11 at 19:48
  • Nope, QTreeWidgetItem.setSizeHint (self, int column, QSize size) does nothing. Continuing from my previous comment, index.internalId() always shows the same value. – regomodo Jan 19 '11 at 19:52
  • I'm out of ideas. I tried to use a qtreeview and populate it using QStandardItemModel/Items and they function as normal. However, the QItemDelegate.sizeHint is only ever called once. Expanding trees don't call the hint so the row heights are still fixed. – regomodo Jan 20 '11 at 18:07
  • Do you by any chance have the `UniformRowHeights()` property set to true in the `QTreeView`? This would cause only one call to the sizeHint(). – Jason B Jan 20 '11 at 18:52
  • Jason, i found that out just after I sent my last reply. I updated my answer with that very crucial requirement. – regomodo Jan 20 '11 at 21:36