【问题标题】:scale QPixmap to match QTreeView cell column width缩放 QPixmap 以匹配 QTreeView 单元格列宽
【发布时间】:2020-11-19 18:49:07
【问题描述】:

我在 QTreeView 的单元格中使用 QStandardItemModel 有 QPixmap。我希望能够在缩放列时缩放 QPixmap(在保持纵横比的同时匹配列/单元格宽度)。

我可以在 QPixmap 上使用 scaled() 方法,但是我必须找出列宽变化的触发信号。有没有简单直接的方法?

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

【问题讨论】:

标签: python-3.x pyqt pyqt5 qtableview qpixmap


【解决方案1】:

一种可能的解决方案是使用委托:

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()
delegate = ThumbnailDelegate(self.treeView)
self.treeView.setItemDelegateForColumn(1, delegate)

【讨论】:

  • 谢谢!我看到您添加了“QtCore.Qt.KeepAspectRatio”,但静止图像被拉伸并且它们不保持其纵横比。任何想法如何处理它?
猜你喜欢
  • 1970-01-01
  • 2016-12-17
  • 2012-03-24
  • 2012-05-31
  • 1970-01-01
  • 2012-02-14
  • 1970-01-01
  • 1970-01-01
  • 2017-05-07
相关资源
最近更新 更多