如果qtablewidget设置了cell widget是一个qcombobox,怎么在获取表格数据时获取到combobox选择的值
时间: 2024-05-14 10:18:58 浏览: 105
如果你想获取表格中每个单元格的 QComboBox 的当前选定值,可以使用 QTableWidget 的 itemWidget() 方法来获取单元格的 widget,然后使用 QComboBox 的 currentText() 方法来获取选定项的文本。以下是一个示例代码:
```python
for row in range(table.rowCount()):
for column in range(table.columnCount()):
item = table.item(row, column)
if item is not None and isinstance(item, QtWidgets.QTableWidgetItem):
widget = table.cellWidget(row, column)
if widget is not None and isinstance(widget, QtWidgets.QComboBox):
current_value = widget.currentText()
print(f"Row {row}, Column {column}: {current_value}")
```
在上面的代码中,我们首先遍历表格中的所有行和列。然后,我们使用 itemWidget() 方法获取每个单元格的 widget,如果它是 QComboBox,则使用 currentText() 方法获取当前选定项的文本,并将其打印出来。注意,我们使用 isinstance() 检查 item 和 widget 是否是 QTableWidgetItem 和 QComboBox 的实例,以确保我们只处理正确的单元格类型。
阅读全文
相关推荐
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)