tableWidget 获取行、列、单元格中QCheckBox的状态
时间: 2023-11-13 15:02:28 浏览: 57
可以通过以下代码获取QTableWidget中QCheckBox的状态:
1. 获取单元格中QCheckBox的状态:
```python
# tableWidget为QTableWidget对象,row和column为单元格的行列号
cell = tableWidget.cellWidget(row, column)
if cell is not None and isinstance(cell, QCheckBox):
checked = cell.isChecked()
```
2. 获取一行中所有QCheckBox的状态:
```python
# tableWidget为QTableWidget对象,row为行号
checked_list = []
for column in range(tableWidget.columnCount()):
cell = tableWidget.cellWidget(row, column)
if cell is not None and isinstance(cell, QCheckBox):
checked = cell.isChecked()
checked_list.append(checked)
```
3. 获取一列中所有QCheckBox的状态:
```python
# tableWidget为QTableWidget对象,column为列号
checked_list = []
for row in range(tableWidget.rowCount()):
cell = tableWidget.cellWidget(row, column)
if cell is not None and isinstance(cell, QCheckBox):
checked = cell.isChecked()
checked_list.append(checked)
```
注意:以上代码中,需要先判断单元格中是否有QCheckBox控件,否则可能会出现NoneType错误。