我试图用python读取PyQt QTableWidget中复选框的checkstate,但当我点击复选框时,我看到它正在改变,但我的代码没有。这是我正在使用的代码。有什么想法吗?
class MyWindowClass(QtGui.QMainWindow, window):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.load_table()
def load_table(self):
"""Fill table with data from results"""
self.todoTable.clear()
result = [(0, "name", "Desc", "2014-12-07", "1"), (1, "name", "desc", "2014-12-08", "0")]
self.todoTable.setRowCount(result.__len__())
self.todoTable.setColumnCount(4)
self.todoTable.setHorizontalHeaderLabels(["State", "Date", "Name", "Description"])
for i in range(result.__len__()):
self.todoTable.setItem(i, 2, QtGui.QTableWidgetItem(result[i][1]))
self.todoTable.setItem(i, 3, QtGui.QTableWidgetItem(result[i][2]))
self.todoTable.setItem(i, 1, QtGui.QTableWidgetItem(result[i][3].__str__()))
checkItem = QtGui.QTableWidgetItem()
if int(result[i][4]) == 2:
checkItem.setCheckState(QtCore.Qt.Checked)
checkItem.setText("")
else:
checkItem.setCheckState(QtCore.Qt.Unchecked)
checkItem.setText("")
self.todoTable.setItem(i, 0, checkItem)
self.todoTable.resizeColumnsToContents()
self.todoTable.itemChanged.connect(self.item_changed)
def item_changed(self, item):
row = item.row()
state = item.checkState()
print("Current state is: {}".format(state))
print("we're now at item in row: {}".format(row))
if state == 2:
print("set checked")
elif state == 0:
print("set unchecked")
else:
print("Something is not working fine here")
更新:修复了代码,更新后反映了更改