在PyQt的QTableWidget中添加图片

10 投票
3 回答
19375 浏览
提问于 2025-04-16 15:07

我刚开始学习Python,对PyQt也不太熟悉。我已经成功创建了一个表格,但想在某些单元格里添加图片。我听说我需要对QTableWidget类进行子类化,或者可能是QTableWidgetItem类,并重新实现QPaintEvent。如果有人能给我一个重新实现QPaintEvent的例子,我会非常感激。

谢谢,
Stephen

3 个回答

0

首先,你需要把你的opencv图像转换成qpixmap格式,可以用下面的代码:

def convert_cv_qt(self, cv_img):
    rgb_image = cv.cvtColor(cv_img, cv.COLOR_BGR2RGB)
    h, w, ch = rgb_image.shape
    bytes_per_line = ch * w
    convert_to_Qt_format = QtGui.QImage(rgb_image.data, w, h, bytes_per_line, QtGui.QImage.Format.Format_RGB888)
    p = convert_to_Qt_format.scaled(cv_img.shape[0], cv_img.shape[1], Qt.AspectRatioMode.KeepAspectRatio)
    return QPixmap.fromImage(p)

然后,你就可以像这样把你的图像插入到qtable里:

qt_img = convert_cv_qt(cv_img)

rowPosition = table.rowCount()
table.insertRow(rowPosition)
table.setItem(rowPosition, 0, QTableWidgetItem('(My Text)'))
table.setItem(rowPosition, 1, QTableWidgetItem('datetime(2019, 5, 4)'))
table.setCellWidget(rowPosition, 2,self.getImageLabel(qt_img))
2

我觉得这个解决方案对我这个初学者来说很友好:

from PyQt5 import QtCore, QtGui, QtWidgets
import sys

class KindForMind(object):
    def THINK(self, PLEASE):
        self.table = QtWidgets.QTableWidget()
        pic = QtGui.QPixmap("your_image.jpg")
        self.label = QtWidgets.QLabel(PLEASE)
        self.label.setPixmap(pic)
        self.table.setCellWidget(0,0, self.label)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    PLEASE = QtWidgets.QWidget()
    ui = KindForMind()
    ui.THINK(PLEASE)
    PLEASE.show()
    sys.exit(app.exec_())
16
from PyQt4 import QtGui
import sys

imagePath = "enter the path to your image here"

class ImgWidget1(QtGui.QLabel):

    def __init__(self, parent=None):
        super(ImgWidget1, self).__init__(parent)
        pic = QtGui.QPixmap(imagePath)
        self.setPixmap(pic)

class ImgWidget2(QtGui.QWidget):

    def __init__(self, parent=None):
        super(ImgWidget2, self).__init__(parent)
        self.pic = QtGui.QPixmap(imagePath)

    def paintEvent(self, event):
        painter = QtGui.QPainter(self)
        painter.drawPixmap(0, 0, self.pic)


class Widget(QtGui.QWidget):

    def __init__(self):
        super(Widget, self).__init__()
        tableWidget = QtGui.QTableWidget(10, 2, self)
        tableWidget.setCellWidget(0, 1, ImgWidget1(self))
        tableWidget.setCellWidget(1, 1, ImgWidget2(self))

if __name__ == "__main__":
    app = QtGui.QApplication([])
    wnd = Widget()
    wnd.show()
    sys.exit(app.exec_())

有两种方法可以做到这一点,因为你提到了painEvent。

感谢来源: http://www.mail-archive.com/pyqt@riverbankcomputing.com/msg01259.html

希望这对你有帮助。

编辑: 添加了使用QTableWidget子类的解决方案。

from PyQt4 import QtGui
import sys

class ImageWidget(QtGui.QWidget):

    def __init__(self, imagePath, parent):
        super(ImageWidget, self).__init__(parent)
        self.picture = QtGui.QPixmap(imagePath)

    def paintEvent(self, event):
        painter = QtGui.QPainter(self)
        painter.drawPixmap(0, 0, self.picture)


class TableWidget(QtGui.QTableWidget):

    def setImage(self, row, col, imagePath):
        image = ImageWidget(imagePath, self)
        self.setCellWidget(row, col, image)

if __name__ == "__main__":
    app = QtGui.QApplication([])
    tableWidget = TableWidget(10, 2)
    tableWidget.setImage(0, 1, "<your image path here>")
    tableWidget.show()
    sys.exit(app.exec_())

撰写回答