Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding images to a QTableWidget in PyQt

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

like image 447
Stephen Avatar asked Apr 05 '11 14:04

Stephen


People also ask

How to add images to PyQT?

From the property editor dropdown select "Choose File…" and select an image file to insert. As you can see, the image is inserted, but the image is kept at its original size, cropped to the boundaries of the QLabel box. You need to resize the QLabel to be able to see the entire image.

How do I display an image in Python pyqt5?

Displaying an Image To display an image we can actually use a label! We will start by dragging in a label and resizing it to our desired dimensions. After that we will scroll all the way down in the Property Editor tab until we see a property that says Pixmap. Here we can select an image file to display.


1 Answers

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_())
like image 64
Plutonergy Avatar answered Oct 02 '22 15:10

Plutonergy