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. Selection-background-color not working for cell widget in QTableWidget
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved Qt for Python
4 Posts 2 Posters 7.0k 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.
  • S Offline
    S Offline
    sherief
    wrote on 26 Jun 2019, 19:05 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Denni
      wrote on 27 Jun 2019, 12:57 last edited by
      #2

      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.

      madness... is like gravity, all takes is a little... push -- like from an unsolvable bug

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sherief
        wrote on 28 Jun 2019, 20:57 last edited by
        #3

        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_())
        
        1 Reply Last reply
        0
        • D Offline
          D Offline
          Denni
          wrote on 1 Jul 2019, 16:03 last edited by Denni 7 Jan 2019, 16:06
          #4

          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_())
          

          madness... is like gravity, all takes is a little... push -- like from an unsolvable bug

          1 Reply Last reply
          1

          1/4

          26 Jun 2019, 19:05

          • Login

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