开发者

How to set the icon-size in a QTreeWidget?

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 wi开发者_如何学运维dth, 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?


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))


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜