1 回答
TA贡献1820条经验 获得超10个赞
一个简单的解决方案是建立一个委托,其中QTableWidget使用以下setItemDelegateForColumn()方法调整图标大小和设置:
from PySide import QtCore, QtGui
class IconDelegate(QtGui.QStyledItemDelegate):
def initStyleOption(self, option, index):
super(IconDelegate, self).initStyleOption(option, index)
option.decorationSize = option.rect.size()
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
table_widget = QtGui.QTableWidget()
self.setCentralWidget(table_widget)
table_widget.setColumnCount(2)
table_widget.verticalHeader().setDefaultSectionSize(80)
for index, file in enumerate(("clear.png", "butterfly.png")):
table_widget.insertRow(table_widget.rowCount())
item1 = QtGui.QTableWidgetItem(QtGui.QIcon(file), "")
item2 = QtGui.QTableWidgetItem(file)
table_widget.setItem(index, 0, item1)
table_widget.setItem(index, 1, item2)
delegate = IconDelegate(table_widget)
table_widget.setItemDelegateForColumn(0, delegate)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
添加回答
举报