Python 3.x 缩放QPixmap以匹配QTreeView单元格列宽
我使用QStandardItemModel在QTreeView的单元格中绘制了qpixmap。我希望能够在列被缩放时缩放QPixmap(匹配列/单元宽度,同时保持纵横比) 我可以在QPixmap上使用scaled()方法,但接下来我必须找出列宽变化的触发信号。有什么简单直接的方法吗Python 3.x 缩放QPixmap以匹配QTreeView单元格列宽,python-3.x,pyqt,pyqt5,qtableview,qpixmap,Python 3.x,Pyqt,Pyqt5,Qtableview,Qpixmap,我使用QStandardItemModel在QTreeView的单元格中绘制了qpixmap。我希望能够在列被缩放时缩放QPixmap(匹配列/单元宽度,同时保持纵横比) 我可以在QPixmap上使用scaled()方法,但接下来我必须找出列宽变化的触发信号。有什么简单直接的方法吗 from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow):
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.resize(400, 300)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.treeView = QtWidgets.QTreeView(self.centralwidget)
self.verticalLayout.addWidget(self.treeView)
MainWindow.setCentralWidget(self.centralwidget)
self.my_tree_model = MYTreeModel()
self.treeView.setModel(self.my_tree_model)
MainWindow.setCentralWidget(self.centralwidget)
class MYTreeModel(QtGui.QStandardItemModel):
def __init__(self):
super().__init__()
columns = ['data', 'thumbnail', 'data', 'data']
for row_id in range(3):
qrow_data = []
for col_id, column in enumerate(columns):
content = {}
if column == 'thumbnail':
image = QtGui.QImage('monkey.png')
item = QtGui.QStandardItem()
pxmap = QtGui.QPixmap.fromImage(image).scaled(50,50, QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation)
item.setData(QtCore.QVariant(pxmap), QtCore.Qt.DecorationRole)
qrow_data.append(item)
else:
key = "data{0:s}{1:s}".format(str(row_id), str(col_id))
content[key] = QtGui.QStandardItem('my_data')
qrow_data.append(content[key])
self.appendRow(qrow_data)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication([])
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
一种可能的解决方案是使用代理:
class ThumbnailDelegate(QtWidgets.QStyledItemDelegate):
def initStyleOption(self, option, index):
super().initStyleOption(option, index)
option.icon = QtGui.QIcon()
option.features &= ~QtWidgets.QStyleOptionViewItem.HasDecoration
def paint(self, painter, option, index):
super().paint(painter, option, index)
mode = QtGui.QIcon.Normal
if not (option.state & QtWidgets.QStyle.State_Enabled):
mode = QtGui.QIcon.Disabled
elif option.state & QtWidgets.QStyle.State_Selected:
mode = QtGui.QIcon.Selected
state = (
QtGui.QIcon.On
if option.state & QtWidgets.QStyle.State_Open
else QtGui.QIcon.Off
)
pixmap = index.data(QtCore.Qt.DecorationRole).scaled(
option.rect.size(),
QtCore.Qt.KeepAspectRatio,
QtCore.Qt.SmoothTransformation,
)
icon = QtGui.QIcon(pixmap)
pixmap = icon.pixmap(pixmap.size(), mode, state)
painter.drawPixmap(option.rect, pixmap)
def sizeHint(self, option, index):
return index.data(QtCore.Qt.DecorationRole).size()
请提供一份完整的表格。谢谢!我看到您添加了'QtCore.Qt.KeepAspectRatio',但静态图像会被拉伸,并且它们不会保持其纵横比。你知道怎么处理吗?
delegate = ThumbnailDelegate(self.treeView)
self.treeView.setItemDelegateForColumn(1, delegate)