10

I'm very new to Python and even newer to PyQt. I've managed to create a table, but want to add images in certain cells. I've read that I need to subclass the QTableWidget class, or possibly the QTableWidgetItem class and re-implement the QPaintEvent. If anyone has an example of what goes into re-implementing the QPaintEvent I would really appreciate it.

Thanks, Stephen

Stephen
  • 663
  • 5
  • 13
  • 24

3 Answers3

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

Two ways of doing it, since you asked for the painEvent.

Credits: http://www.mail-archive.com/pyqt@riverbankcomputing.com/msg01259.html

Hope this helps.

Edit: Added requested solution using a QTableWidget subclass.

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_())
aukaost
  • 3,778
  • 1
  • 24
  • 26
  • 1
    Thanks, that helped a lot. Would it also be possible to subclass the QTableWidget class to create something like: tableWidget.setImage(imagePath)? or would that take so much effort that it wouldn't be worth while? – Stephen Apr 05 '11 at 20:47
  • It seems so obvious when I see it in black and white. Thanks – Stephen Apr 05 '11 at 21:47
  • Glad to help. Feel free to accept this answer if you get the chance :) – aukaost Apr 06 '11 at 15:49
  • I hope this is a question with a quick and easy answer. IIf you are going to change the look and feel (what happens when cells are clicked, color and formatting of the headers, etc..) is it easier to modify QTableWidget, or create your own table with the QTableView and QModel? – Stephen Apr 06 '11 at 17:17
  • I'm afraid I don't have much experience with Qt's Model/View classes. Personally I would see if I can do what I need with the widgets provided. And so far I could. – aukaost Apr 06 '11 at 19:20
  • Is it possible to set the background color of specific cell when using cellWidget instead of table item? – TheCodeLearner Dec 31 '21 at 11:15
2

I find this solution to be kind to my beginners mind:

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_())
Plutonergy
  • 39
  • 1
  • 3
0

At the first you should convert your opencv image to qpixlemap format with this code :

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)

and then you can insert your image to qtable like this :

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))
Erfan
  • 31
  • 3