Highlight cellWidget in QTableWidget in pyqt5

Viewed 326

I have a QTableWidget setup that basically displays the content of a database I have connected. Each column is basic text info but the last column is a checkbox that is checked based on the entry's privileges and the user is not able to toggle it. I also have my table setup so that the entire row is highlighted when a single column is selected. For some reason though, the column with the checkbox doesn't get highlighted. I messed around with stylesheets for the check box (with selection-background-color) but it still doesn't do anything. I've also tried messing around with isSelected() and changing the background-color of all the items it returns, but still no luck.

Any ideas on how to set the selection-color of my checkbox?

from PyQt5 import QtCore, QtGui, QtWidgets
from buttons import *
import sqlite3
from _sqlite3 import connect
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QDialog, QDialogButtonBox,
        QHBoxLayout, QMessageBox, QPushButton, QHeaderView, QTableView, QAbstractItemView)

def loadData(self, QTableWidget, insert):
        self.tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)

        connection = sqlite3.connect('employee.db')
        c = connection.cursor()
        data = fetch_all()
        self.tableWidget.setRowCount(0)

        self.tableWidget.horizontalHeader().resizeSection(1, 50); 
        i = 0
        self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
        self.tableWidget.verticalHeader().setVisible(False)
        self.tableWidget.horizontalHeader().setVisible(False)
        self.tableWidget.setShowGrid(False)

      #  self.tableWidget.setStyleSheet("selection-background-color: red")

  #      self.tableWidget.setStyleSheet("background-color: black")



        header = self.tableWidget.horizontalHeader()

        for row in data:
            self.tableWidget.insertRow(i)
            i += 1
            j = 0
            for x in row:
                if (j == 4):
                    break
                if(i == 1 and insert == True):
                    self.tableWidget.insertColumn(j)
                if (j == 0):
                    self.tableWidget.setItem(i-1, j, QtWidgets.QTableWidgetItem("  " + str(x)))
                elif (j == 3 and str(x) == "admin"):
                    self.tableWidget.checkBox = 
                   QtWidgets.QCheckBox(self.tableWidget)
                    self.tableWidget.checkBox.setChecked(True)
                    self.tableWidget.checkBox.setAttribute(QtCore.Qt.WA_TransparentForMouseEvents)
                    self.tableWidget.checkBox.setFocusPolicy(QtCore.Qt.NoFocus)


                    self.tableWidget.setCellWidget(i-1, j, self.tableWidget.checkBox)

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


                else:
                    if (str(x) != "user"):
                        self.tableWidget.setItem(i-1, j, QtWidgets.QTableWidgetItem(str(x)))


                j += 1

        self.tableWidget.horizontalHeader().resizeSection(0, 188)
        self.tableWidget.horizontalHeader().resizeSection(1, 155)
        self.tableWidget.horizontalHeader().resizeSection(2, 255)

        self.tableWidget.horizontalHeader().resizeSection(3, 30)

        self.tableWidget.setStyleSheet("background-color: rgb(0, 0, 0); selection-background-color: #353535;")


        header = self.tableWidget.horizontalHeader()

        #header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)

        connection.close()
0 Answers
Related