How to change the background colour of specific QTableWidget cell when clicked



  • Hi guys, I am very new towards using PYQT5. So apperantly I have a table with just a single row and multiple column. I insert the images using tableWidget.setCellWidget() and not setItem because i wanted to insert an image into different cell and setItem doesnt allow me to do so. I wanted to change the background colour of the cell and by doing so I only able to use tableWidget.cellWidget(0, 1).setStyleSheet('background-color: red;') instead of item.setColor right now I just wanted to change the colour of that one cell when I clicked on the particular cell. However, I cant change that colour of the particular cell instead, it changes the entire table colour how do I fix this?
    Thanks.

    from PyQt5 import QtCore, QtGui
    import sys
    
    from PyQt5.QtWidgets import QApplication, QLabel, QTableWidget, QTableWidgetItem, QWidget
    
    imagePath = "tubeBLUE2.png"
    imagePath2 = "arrowRED.png"
    
    class ImgWidget1(QLabel):
    
        def __init__(self, parent=None):
            super(ImgWidget1, self).__init__(parent)
            pic = QtGui.QPixmap(imagePath)
            self.setPixmap(pic)
    
    class ImgWidget2(QLabel):
    
        def __init__(self, parent=None):
            super(ImgWidget2, self).__init__(parent)
            pic = QtGui.QPixmap(imagePath2)
            self.setPixmap(pic)
    
    # class ImgWidget3(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(QWidget):
    
        def __init__(self, array1):
            super(Widget, self).__init__()
            tableWidget = QTableWidget(1, len(array1), self)
            tableWidget.setMinimumWidth(800)
            tableWidget.setMinimumHeight(800)
            tableWidget.setShowGrid(False)
            # tableWidget.setItem(3, 5, QtGui.QTableWidgetItem())
            self.array1 = array1
            # header = tableWidget.horizontalHeader()
            # header. hide()
            for i in range (0, len(array1)-1):
                if(self.array1[i] == "s"):
                    tableWidget.setCellWidget(0, i, ImgWidget1(self))
                else:
                    tableWidget.setCellWidget(0, i, ImgWidget2(self))
                    
            # tableWidget.cellWidget(0, 0).palette().setColor(Qt.red)
            # tableWidget.cellWidget(0, 0).setStyleSheet('background-color: yellow;')
            # tableWidget.cellWidget(0, 1).setStyleSheet('background-color: red;')
            tableWidget.cellClicked.connect(self.cellClicked)
            # tableWidget.cellWidget(0, 1).setStyleSheet('background-color: red;')
            tableWidget.resizeRowsToContents()
            tableWidget.resizeColumnsToContents()
        
        def cellClicked(self, row, column): 
            print("column", column)
            self.setStyleSheet('background-color: red;')
    
            return column
                
                    
    if __name__ == "__main__":
        app = QApplication([])
        array1 = ["a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s"]
        wnd = Widget(array1)
        wnd.show()
        sys.exit(app.exec_())
    
    
    

    Capture.PNG


  • Lifetime Qt Champion

    Since you use a QTableWidget, create a QTableWidgetItem with an icon from your image.


  • Lifetime Qt Champion

    Hi,

    I would start by nuking the use of cell widgets. Use the decoration role of your model to return the appropriate image. Then you'll be able to customize your view more easily.



  • @SGaist Hi is there any example of using decoration role for my example?


  • Lifetime Qt Champion

    Since you use a QTableWidget, create a QTableWidgetItem with an icon from your image.


Log in to reply