在pyqt5中设置禁用复选框的选择背景

时间:2019-06-28 20:48:54

标签: python pyqt pyqt5 qtablewidget

我有一个被禁用的复选框(用户应该不能切换它),但是在选择它时很难更改其背景。我确定它与禁用此框有关,但是我不确定如何修复它。如果有一个简单的样式表修复程序将是理想的选择。

我尝试弄乱插槽/信号,但这引起了奇怪的问题,我宁愿不以这种方式解决此问题

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 个答案:

答案 0 :(得分:3)

您所遇到的问题是,要选择的QCheckBox必须具有焦点,但是您已将其禁用,并且根据您在评论中的论述,使我认为您拥有XY problem:问题是每行都有选择,并根据QTableWidget的数据启用复选框状态更改。

考虑到上述情况,不必使用QCheckBox,仅使用QCheckBox即可启用Qt :: ItemIsUserCheckable标志,而无需使用委托即可启用或禁用复选框的状态更改。

考虑到上述情况,解决方案是:

import random
from enum import Flag
from PyQt5 import QtCore, QtGui, QtWidgets


PermissionsRole = QtCore.Qt.UserRole + 1000


class Permissions(Flag):
    User = 0
    Admin = 1 << 0


class CheckBoxDelegate(QtWidgets.QStyledItemDelegate):
    def editorEvent(self, event, model, option, index):
        if index.data(PermissionsRole) & Permissions.Admin:
            super(CheckBoxDelegate, self).editorEvent(
                event, model, option, index
            )
        return False


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.m_tablewidget = QtWidgets.QTableWidget(
            selectionBehavior=QtWidgets.QAbstractItemView.SelectRows,
            selectionMode=QtWidgets.QAbstractItemView.SingleSelection,
            editTriggers=QtWidgets.QAbstractItemView.NoEditTriggers,
            showGrid=False,
            columnCount=3,
        )
        delegate = CheckBoxDelegate(self.m_tablewidget)
        self.m_tablewidget.setItemDelegateForColumn(1, delegate)
        self.m_tablewidget.setStyleSheet(
            """
            QTableView
            {
                background-color: white; 
                selection-background-color: #353535;
            }
            """
        )
        for header in (
            self.m_tablewidget.horizontalHeader(),
            self.m_tablewidget.verticalHeader(),
        ):
            header.hide()
            if header.orientation() == QtCore.Qt.Horizontal:
                header.setSectionResizeMode(QtWidgets.QHeaderView.Stretch)

        for i in range(10):
            isAdmin = random.choice([Permissions.User, Permissions.Admin])
            self.m_tablewidget.insertRow(self.m_tablewidget.rowCount())
            self.m_tablewidget.setItem(
                i,
                0,
                QtWidgets.QTableWidgetItem(
                    "Is Admin?: {}".format(isAdmin == Permissions.Admin)
                ),
            )

            it = QtWidgets.QTableWidgetItem()
            it.setData(PermissionsRole, isAdmin)
            it.setFlags(it.flags() | QtCore.Qt.ItemIsUserCheckable)
            it.setCheckState(QtCore.Qt.Checked)
            self.m_tablewidget.setItem(i, 1, it)

        self.setCentralWidget(self.m_tablewidget)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())