Skip to content Skip to sidebar Skip to footer

PySide: How To Scale Up Images In QTableWidget?

I have problem with PySide QTableWidget. I need to add in first column of all rows image preview. I'm trying to add this using QIcon: library_table.insertRow(index) library_table.s

Solution 1:

A simple solution is to establish a delegate where the icon is resized and set in the QTableWidget using the setItemDelegateForColumn() method:

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

enter image description here


Post a Comment for "PySide: How To Scale Up Images In QTableWidget?"