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
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.
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.
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_())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With