我在qtablewidget的第一个单元格中添加了qcheckbox,现在我想要的是如何访问复选框以及如何获取其单击事件。基本上我想要的是复选框应该被选中,而未选中的行应该被取消选择。 这是代码:
def add_items(self):
rows = 5
cols = 5
self.tableWidget.setRowCount(5)
self.tableWidget.setColumnCount(5)
for r in range(0, rows):
for c in range(1, cols):
self.qwidget = QtGui.QWidget()
self.checkbox = QtGui.QCheckBox()
self.checkbox.setCheckState(QtCore.Qt.Checked)
qhboxlayout = QtGui.QHBoxLayout(self.qwidget)
qhboxlayout.addWidget(self.checkbox)
qhboxlayout.setAlignment(QtCore.Qt.AlignCenter)
qhboxlayout.setContentsMargins(0, 0, 0, 0)
self.tableWidget.setCellWidget(r, 0, self.qwidget)
item = QTableWidgetItem('note'+str(r))
self.tableWidget.setItem(r, c, item)
print 'done'
您可以使用itemChanged
信号。
from PySide.QtCore import Slot, Qt
from PySide.QtGui import QTableWidgetItem
@Slot(QTableWidgetItem)
def on_tableWidget_itemChanged(self, item):
""" Handles the row's state
:type item: QTableWidgetItem
:parameter item: The changed item
"""
checked = item.checkState() == Qt.Checked
if checked: # the item gets checked
# do stuff here..
else: # the item gets unchecked
# do stuff here