Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to change the background colour of specific QTableWidget cell when clicked
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved Qt for Python
qt for python
4 Posts 2 Posters 5.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JohnLim02
    wrote on last edited by JohnLim02
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #4

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        J 1 Reply Last reply
        1
        • SGaistS SGaist

          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.

          J Offline
          J Offline
          JohnLim02
          wrote on last edited by
          #3

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

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

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

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved