【发布时间】:2020-12-30 11:21:55
【问题描述】:
我有以下代码在 QTableWidget 的项目列表的第一列中放置一个 CheckBox。
checkboxWidget = QWidget()
checkBox = QCheckBox(checkboxWidget)
checkBox.clicked.connect(self._check_changed)
#
# If the variable is in the monitored list
# check the checkbox
#
isMonitored = False
if (self._monitored_variables != None):
if (self._monitored_variables[name]):
isMonitored = True
if (isMonitored):
checkBox.setCheckState(Qt.CheckState.Checked)
else:
checkBox.setCheckState(Qt.CheckState.Unchecked)
layoutCheckbox = QHBoxLayout(checkboxWidget)
layoutCheckbox.addWidget(checkBox)
layoutCheckbox.setAlignment(Qt.AlignCenter)
layoutCheckbox.setContentsMargins(0, 0, 0, 0)
self._variables_view.setCellWidget(row,0, checkboxWidget)
我从这个问题的答案开始: How should I connect CheckBox clicked signals in Table Widgets in PyQt5?
我的不同之处在于我希望 CheckBox 在表格单元格中居中,因此需要额外的控件。
点击处理程序如下所示:
def _check_changed(self):
cb = self.sender()
print(cb.parent())
ix = self._variables_view.indexAt(cb.pos())
print(ix.row(), ix.column(), cb.isChecked())
我面临的问题是行/列不正确。
如何恢复被点击的复选框的行/列?
【问题讨论】: