Planned maintenance: From Sunday 8th December 10:00 CET there will be changes to try and solve the caching issues that have been experienced. If anyone has a problem connecting after this period then please PM @AndyS or any of the moderators.

Selection-background-color not working for cell widget in QTableWidget



  • Hi, I'm working in pyqt5 for python 3.6 and am having trouble setting the selection background of a checkbox I have as a cell widget in a QTableWidget. The other entries in my table properly get highlighted when selected on, but for some reason, my checkbox never changes background color when selected. This is the stylesheet I have for my checkbox :

    self.checkBox.setStyleSheet("background-color: rgb(0, 0, 0); selection-background-color: #353535; padding-left: 10px")

    The color stays as black whether selected or not. My other cells are just strings of text and they have no issues in changing background color when selected, but for some reason my checkbox cell widget never changes color when selected. I've messed around with signals/slots but ran into a lot of issues with it, so if there's a simple fix I'd really appreciate it.

    Any ideas?



  • Okay as always when asking such questions a mini fully functional bit of code that reproduces your issue is extremely helpful for someone to help you with your issue. The key here is something that demonstrates you problem because you never know exactly what the issue might be. I personally have solved an issue by creating this mini-program as is it worked just fine while the full program did not and I was able to pin-point the problem. Further if I create a mini program it just might work because I did not do the things the way you did them and thus I will have wasted my time since I would not be able to reproduce your issue.



  • Hi sorry for not responding sooner, I created an example:

    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtWidgets import (QApplication,
              QTableView, QAbstractItemView)
    
    
    
    class Ui_MainWindow(object):
       def setupUi(self, MainWindow):
           MainWindow.setObjectName("MainWindow")
           MainWindow.resize(400, 300)
           self.centralwidget = QtWidgets.QWidget(MainWindow)
           self.centralwidget.setObjectName("centralwidget")
           MainWindow.setCentralWidget(self.centralwidget)
    
           self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
           self.tableWidget.setGeometry(QtCore.QRect(50, 40, 310, 50))
           self.tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)
           self.tableWidget.setSelectionMode(QAbstractItemView. 
              SingleSelection)
           self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
           self.tableWidget.verticalHeader().setVisible(False)
           self.tableWidget.horizontalHeader().setVisible(False)
           self.tableWidget.setShowGrid(False)
           self.tableWidget.setStyleSheet("background-color: white; selection-background-color: #353535;")
    
           self.tableWidget.insertRow(0)
    
           self.tableWidget.insertColumn(0)
           self.tableWidget.insertColumn(1)
           self.tableWidget.insertColumn(2)
    
    
           self.tableWidget.checkBox = QtWidgets.QCheckBox(self.tableWidget)
    
           self.tableWidget.checkBox.setAttribute(QtCore.Qt.WA_TransparentForMouseEvents)
           self.tableWidget.checkBox.setFocusPolicy(QtCore.Qt.NoFocus)
           self.tableWidget.checkBox.setMaximumSize(30, 30)
           self.tableWidget.checkBox.setStyleSheet("background-color: white; selection-background-color: #353535; padding-left: 10px")
           self.tableWidget.checkBox.setChecked(True)
    
    
           self.tableWidget.setCellWidget(0, 1, self.tableWidget.checkBox)
    
    
    
    
    if __name__ == "__main__":
       import sys
       app = QtWidgets.QApplication(sys.argv)
       MainWindow = QtWidgets.QMainWindow()
       ui = Ui_MainWindow()
       ui.setupUi(MainWindow)
       MainWindow.show()
       sys.exit(app.exec_())
    


  • Okay one of the biggest lessons I learned with working with pyqt5 is defining your own class is almost essential when trying to implement things other than just the generic operation of a particular object. Which of course is what you are attempting to do. That being said here is a more pythonic version of what you presented with the bits necessary to implement row widget contained object highlighting now you would also have to (unless you let the checkbox fill its container) set the background for the container housing the checkbox since you have limited it size to less than its container. However, if you just remove that limit it will appear as you would like it to without that extra effort but then again not sure why you limited it size so I left it as is.

    Secondary note the QTableWidget is its own object and a container for your other objects (QLineEdit, QTextEdit, QComboBox) as such setting values to the QTableWidget does not affect the objects it contains to change those you have to address them directly and/or build some other feature to handle it more generically -- a QTreeView * QItemStandardModel implementation comes to mind for that -- however again did not know why you chose the QTableWidget so just made it work with that.

    Oh final note -- This is Python 3.7 with pyqt5 not sure if that will be an issue for you or not

    from sys import exit as sysExit
    
    from PyQt5.QtCore import *
    from PyQt5.QtGui  import *
    from PyQt5.QtWidgets import *
    
    class TableLine(QLineEdit):
        def __init__(self, parent):
            QLineEdit.__init__(self)
            self.setStyleSheet("padding-left: 10px")
    
        def IsSelected(self):
            self.On_Selected(True)
    
        def NotSelected(self):
            self.On_Selected(False)
    
        def On_Selected(self, Slctd):
            if Slctd:
                self.setStyleSheet("background-color: #353535")
            else:
                self.setStyleSheet("background-color: white")
    
    class TableText(QTextEdit):
        def __init__(self, parent):
            QTextEdit.__init__(self)
            self.setStyleSheet("padding-left: 10px")
    
        def IsSelected(self):
            self.On_Selected(True)
    
        def NotSelected(self):
            self.On_Selected(False)
    
        def On_Selected(self, Slctd):
            if Slctd:
                self.setStyleSheet("background-color: #353535")
            else:
                self.setStyleSheet("background-color: white")
    
    class TableCheck(QCheckBox):
        def __init__(self, parent):
            QCheckBox.__init__(self)
            self.setAttribute(Qt.WA_TransparentForMouseEvents)
            self.setFocusPolicy(Qt.NoFocus)
            self.setMaximumSize(30, 30)
            self.setChecked(True)
            self.setStyleSheet("padding-left: 10px")
    
        def IsSelected(self):
            self.On_Selected(True)
    
        def NotSelected(self):
            self.On_Selected(False)
    
        def On_Selected(self, Slctd):
            if Slctd:
                self.setStyleSheet("background-color: #353535")
            else:
                self.setStyleSheet("background-color: white")
    
    class TableTable(QTableWidget):
        def __init__(self, parent):
            QTableWidget.__init__(self)
    
            self.setGeometry(QRect(50, 40, 310, 50))
            self.setSelectionBehavior(QAbstractItemView.SelectRows)
            self.setSelectionMode(QAbstractItemView.SingleSelection)
            self.setEditTriggers(QAbstractItemView.NoEditTriggers)
            self.verticalHeader().setVisible(False)
            self.horizontalHeader().setVisible(False)
            self.setShowGrid(False)
            self.PrvSlctd = -1
            self.Selected = -1
    
            self.insertRow(0)
            self.insertRow(1)
    
            self.insertColumn(0)
            self.insertColumn(1)
            self.insertColumn(2)
    
            self.setCellWidget(0, 0, TableLine(self))
            self.setCellWidget(0, 1, TableCheck(self))
            self.setCellWidget(0, 2, TableText(self))
    
            self.setCellWidget(1, 0, TableLine(self))
            self.setCellWidget(1, 1, TableCheck(self))
            self.setCellWidget(1, 2, TableText(self))
    
            self.itemSelectionChanged.connect(self.On_Selected)
    
        def On_Selected(self):
            if self.PrvSlctd > -1:
                self.cellWidget(self.PrvSlctd, 0).NotSelected()
                self.cellWidget(self.PrvSlctd, 1).NotSelected()
                self.cellWidget(self.PrvSlctd, 2).NotSelected()
            
            if self.Selected == 0:
                self.Selected = 1
                self.PrvSlctd = 1
            else:
                self.Selected = 0
                self.PrvSlctd = 0
    
            self.cellWidget(self.Selected, 0).IsSelected()
            self.cellWidget(self.Selected, 1).IsSelected()
            self.cellWidget(self.Selected, 2).IsSelected()
    
    class UI_MainWindow(QMainWindow):
        def __init__(self):
            super(UI_MainWindow, self).__init__()
            self.setWindowTitle('Main Window')
            self.setObjectName("MainWindow")
            self.resize(400, 300)
    
            self.MyTable = TableTable(self)
            self.setCentralWidget(self.MyTable)
    
    if __name__ == "__main__":
        MainApp = QApplication([])
    
        MainGui = UI_MainWindow()
        MainGui.show()
    
        sysExit(MainApp.exec_())
    

Log in to reply