动态调整QListView项目的图标大小

时间:2019-09-04 15:20:04

标签: python pyside

当更换滑块时,如何使QListView的项目图标调整大小?它似乎是在调整项目的大小,但不是图标。

我尝试同时调用这两种方法,但两者均无效。理想情况下,我不希望调用setGridSize,因为这会导致小部件忽略我打算使用的setSpacing(5)

self.uiListView.setGridSize(iconSize)
self.uiListView.setIconSize(iconSize)

Current

Current

import os, sys, re
from Qt import QtWidgets, QtGui, QtCore
from PictureShop.Resources import StyleUtils


values = ['MomBod','Colonel','Tater','Tot','Ginger','Donut','Sport','LaLa','Itchy','Bruiser','Cotton','Cumulus','Toodles','Salt','Ghoulie','Cat','Dirty','Harry','Buckeye','Flyby','Swiss','Miss','Buddy','Pecan','Sunny','Jet','Thor','Gingersnap','Cuddle','Pig','Turkey','Foxy','Mini','Me','Dolly','Stud','Music','Man','Barbie','Munchkin','Bubba','Hammer','Twizzler','Bebe']


class ViewerWidget(QtWidgets.QWidget):

    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        self.resize(500,500)

        self.uiIconSize = QtWidgets.QSlider(QtCore.Qt.Horizontal)
        self.uiIconSize.setRange(32,256)
        self.uiIconSize.setValue(128)
        self.uiIconSize.setMinimumWidth(100)

        self.viewerModel = QtGui.QStandardItemModel()
        self.viewerProxyModel = QtCore.QSortFilterProxyModel()
        self.viewerProxyModel.setSourceModel(self.viewerModel)

        self.uiListView = QtWidgets.QListView()
        self.uiListView.setSpacing(5)
        self.uiListView.setMovement(QtWidgets.QListView.Static)
        self.uiListView.setViewMode(QtWidgets.QListView.IconMode)
        self.uiListView.setLayoutMode(QtWidgets.QListView.Batched)
        self.uiListView.setBatchSize(100)
        self.uiListView.setFlow(QtWidgets.QListView.LeftToRight)
        self.uiListView.setWrapping(True)
        self.uiListView.setResizeMode(QtWidgets.QListView.Adjust)
        self.uiListView.setDragEnabled(False)
        self.uiListView.setUniformItemSizes(True)
        self.uiListView.setIconSize(self.getIconSize())
        self.uiListView.setSelectionMode(QtWidgets.QListView.ExtendedSelection)
        self.uiListView.setModel(self.viewerProxyModel)

        # main layout
        self.layout = QtWidgets.QVBoxLayout()
        self.layout.setContentsMargins(0,0,0,0)
        self.layout.setSpacing(0)
        self.layout.addWidget(self.uiListView)
        self.layout.addWidget(self.uiIconSize)
        self.setLayout(self.layout)

        # Signals
        self.uiIconSize.valueChanged.connect(self.setItemSize)

        # Init
        self.populateModel()

    def getIconSize(self):
        return QtCore.QSize(self.uiIconSize.value(), self.uiIconSize.value())

    def setItemSize(self):
        iconSize = self.getIconSize()
        # self.uiListView.setGridSize(iconSize)
        self.uiListView.setIconSize(iconSize)


    def populateModel(self):
        model = self.viewerModel
        model.clear()

        icon = QtGui.QIcon('C:/Users/jmartini/Desktop/image.png')
        for x in values:
            newItem = QtGui.QStandardItem(x)
            newItem.setData(icon, role=QtCore.Qt.DecorationRole)
            model.appendRow(newItem)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ex = ViewerWidget()
    ex.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

我发现这是我图标的原始大小为64x64的结果。 Qt不会将图标的大小调整为大于其原始大小。只会将图标的大小调整为较小的大小。

self.uiListView.setIconSize(QtCore.QSize(64,64))