tmdag
tmdag

Reputation: 529

scale QPixmap to match QTreeView cell column width

I have QPixmaps inside cells of QTreeView using QStandardItemModel. I would like to be able to scale QPixmap when column is scaled (matching colum/cell width while keeping aspect ratio).

I can use scaled() method on QPixmap, but then I would have to figure out a trigger signal on column width change. Is there any simple and straightforward method ?

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

enter image description here

enter image description here

Upvotes: 1

Views: 328

Answers (1)

eyllanesc
eyllanesc

Reputation: 244003

One possible solution is to use a delegate:

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)

Upvotes: 1

Related Questions