实现此目的的一种方法是继承 QTableWidgetItem 并重新实现setData方法。这样,您可以控制项目是否接受某些角色的值。
要控制所有项目的“可检查性”,您可以向子类添加一个类属性,只要将检查状态角色的值传递给 setData,就可以对其进行测试。
下面是子类的样子:
class TableWidgetItem(QtGui.QTableWidgetItem):
_blocked = True
@classmethod
def blocked(cls):
return cls._checkable
@classmethod
def setBlocked(cls, checkable):
cls._checkable = bool(checkable)
def setData(self, role, value):
if role != QtCore.Qt.CheckStateRole or self.checkable():
QtGui.QTableWidgetItem.setData(self, role, value)
并且所有项目的“可检查性”都将被禁用,如下所示:
TableWidgetItem.setCheckable(False)
更新:
可以通过为单元小部件添加通用包装类来扩展上述想法。
下面的类将阻止对表格小部件项目的文本和检查状态的更改,以及通过事件过滤器对单元格小部件的一系列键盘和鼠标事件(可以根据需要阻止其他事件)。
需要像这样创建单元格小部件:
widget = CellWidget(self.table, QtGui.QLineEdit())
self.table.setCellWidget(row, column, widget)
然后像这样访问:
widget = self.table.cellWidget().widget()
整个表的阻塞将像这样打开:
TableWidgetItem.setBlocked(True)
CellWidget.setBlocked(True)
# or Blockable.setBlocked(True)
以下是课程:
class Blockable(object):
_blocked = False
@classmethod
def blocked(cls):
return cls._blocked
@classmethod
def setBlocked(cls, blocked):
cls._blocked = bool(blocked)
class TableWidgetItem(Blockable, QtGui.QTableWidgetItem):
def setData(self, role, value):
if (not self.blocked() or (
role != QtCore.Qt.EditRole and
role != QtCore.Qt.CheckStateRole)):
QtGui.QTableWidgetItem.setData(self, role, value)
class CellWidget(Blockable, QtGui.QWidget):
def __init__(self, parent, widget):
QtGui.QWidget.__init__(self, parent)
self._widget = widget
layout = QtGui.QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(widget)
widget.setParent(self)
widget.installEventFilter(self)
if hasattr(widget, 'viewport'):
widget.viewport().installEventFilter(self)
widget.show()
def widget(self):
return self._widget
def eventFilter(self, widget, event):
if self.blocked():
etype = event.type()
if (etype == QtCore.QEvent.KeyPress or
etype == QtCore.QEvent.KeyRelease or
etype == QtCore.QEvent.MouseButtonPress or
etype == QtCore.QEvent.MouseButtonRelease or
etype == QtCore.QEvent.MouseButtonDblClick or
etype == QtCore.QEvent.ContextMenu or
etype == QtCore.QEvent.Wheel):
return True
return QtGui.QWidget.eventFilter(self, widget, event)