本文介绍了PySide:如何在 QTableWidget 中放大图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 PySide QTableWidget 有问题.我需要添加所有行图像预览的第一列.我正在尝试使用 QIcon 添加此内容:

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.setItem(index, 1, QTableWidgetItem(file))
image = QIcon(self.sub_ad + file)
library_table.setItem(index, 0, QTableWidgetItem(image, ""))

但是图片很小.

我尝试使用 QSize、QPixmap 等,但没有任何成功,大小仍然相同.我怎样才能把这个上一张图片放大?

I was trying to use QSize, QPixmap etc. without any succes, size is still the same. How can I make this prev images bigger?

推荐答案

一个简单的解决方案是建立一个委托,使用 setItemDelegateForColumn() 在 QTableWidget 中调整图标大小和设置 方法:

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

这篇关于PySide:如何在 QTableWidget 中放大图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:42