pyqt在tablewidget前面循环添加复选框
时间: 2023-08-29 11:06:04 浏览: 209
可以使用setItem()方法在TableWidget中添加复选框。具体步骤如下:
1. 创建复选框并设置其属性。
```python
checkbox = QtWidgets.QCheckBox()
checkbox.setTristate(False)
checkbox.setCheckState(QtCore.Qt.Unchecked)
```
2. 将复选框添加到TableWidget的单元格中。
```python
cell_widget = QtWidgets.QWidget()
cell_layout = QtWidgets.QHBoxLayout(cell_widget)
cell_layout.addWidget(checkbox)
cell_layout.setAlignment(QtCore.Qt.AlignCenter)
cell_layout.setContentsMargins(0, 0, 0, 0)
cell_widget.setLayout(cell_layout)
table_widget.setCellWidget(row_index, column_index, cell_widget)
```
3. 循环添加多个复选框。
```python
for i in range(row_count):
checkbox = QtWidgets.QCheckBox()
checkbox.setTristate(False)
checkbox.setCheckState(QtCore.Qt.Unchecked)
cell_widget = QtWidgets.QWidget()
cell_layout = QtWidgets.QHBoxLayout(cell_widget)
cell_layout.addWidget(checkbox)
cell_layout.setAlignment(QtCore.Qt.AlignCenter)
cell_layout.setContentsMargins(0, 0, 0, 0)
cell_widget.setLayout(cell_layout)
table_widget.setCellWidget(i, 0, cell_widget)
```
阅读全文