0

I am attempting to use QTableWidget in conjunction with PyQt5 and QtDesigner to display images in a grid in the GUI. (I'm simulating an LED panel display.) I want each image to be changeable independently of the other images and to be spatially tangent upon the neighboring images in the grid. The problem I am having is that I cannot find a way to remove the cell padding so that I can make the images appear to form a continuous mosaic. Does anyone know if there is a way to do this?

Edit: Here is some more info:

class Home_Win(QMainWindow):
    def __init__(self):
        # Show GUI------------------------
        QMainWindow.__init__(self)
        self.ui = loadUi("bugpanel.ui", self)
        blueLED = QTableWidgetItem(QIcon("img/blueLED.jpg"),'',0)
        rows, cols = 16,32
        for row in range(rows):
            for col in range(cols):
                self.tableWidget_2.setItem(0 , 0, QTableWidgetItem(QIcon("img/greenLED.jpg"),'',0))
        self.tableWidget_2.setItem(0 , 1, blueLED)

What I want:

simulated LED panel picture

What I get:

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Charles R
  • 49
  • 7

1 Answers1

2

You have to implement a custom painting through a delegate, also it is better to set a default size override the sizeHint() method of the delegate and calling the resizeRowsToContents() and resizeColumnsToContents() methods:

import random

from PyQt5 import QtCore, QtGui, QtWidgets


class IconDelegate(QtWidgets.QStyledItemDelegate):
    def paint(self, painter, option, index):
        icon = index.data(QtCore.Qt.DecorationRole)
        mode = QtGui.QIcon.Normal
        if not (option.state & QtWidgets.QStyle.State_Enabled):
            mode = QtGui.QIcon.Disabled
        elif option.state & QtWidgets.QStyle.State_Selected:
            mode = QtGui.QIcon.Selected
        state = (
            QtGui.QIcon.On
            if option.state & QtWidgets.QStyle.State_Open
            else QtGui.QIcon.Off
        )
        pixmap = icon.pixmap(option.rect.size(), mode, state)
        painter.drawPixmap(option.rect, pixmap)

    def sizeHint(self, option, index):
        return QtCore.QSize(60, 60)


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        table = QtWidgets.QTableWidget(10, 10)
        delegate = IconDelegate(table)
        table.setItemDelegate(delegate)

        self.setCentralWidget(table)

        for i in range(table.rowCount()):
            for j in range(table.columnCount()):

                # create icon
                pixmap = QtGui.QPixmap(100, 100)
                color = QtGui.QColor(*random.sample(range(255), 3))
                pixmap.fill(color)
                icon = QtGui.QIcon(pixmap)

                it = QtWidgets.QTableWidgetItem()
                it.setIcon(icon)
                table.setItem(i, j, it)

        table.resizeRowsToContents()
        table.resizeColumnsToContents()


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • By using the deligate function, Is it still possible to change the background colour of the cell item when selected? I have tried it but it wasn't working because the delegate style have overwritten the background color of the table cell with this example with a custom icon with an imported image – TheCodeLearner Dec 31 '21 at 18:12