要获取QTableWidget的内容,您可以使用以下方法之一:
1. 使用`item()`方法:可以使用`item(row, column)`方法获取特定位置的单元格项。例如,要获取第1行第2列的单元格项,可以使用以下代码:
item = tableWidget.item(0, 1)???if?item?is?not?None:
???????print(item.text())
2. 使用selectedItems()
方法:可以使用selectedItems()
方法获取选中的所有单元格项。然后,您可以遍历每个单元格项并获取其内容。例如,要获取所有选中的单元格项的内容,可以使用以下代码:
??selected_items?=?tableWidget.selectedItems()???for?item?in?selected_items:
???????print(item.text())
3. 使用rowCount()
和columnCount()
方法:可以使用rowCount()
和columnCount()
方法获取表格的行数和列数。然后,您可以使用嵌套循环遍历每个单元格并获取其内容。例如,要获取所有单元格的内容,可以使用以下代码:
??for?row?in?range(tableWidget.rowCount()):???????for?column?in?range(tableWidget.columnCount()):
???????????item?=?tableWidget.item(row,?column)
???????????if?item?is?not?None:
???????????????print(item.text())
请注意,上述示例代码中的tableWidget
是指您的QTableWidget对象的名称,请根据您自己的实际情况进行更改。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>