pyqt qtablewidget获得数据
时间: 2023-07-23 09:05:17 浏览: 117
要向PyQt QTableWidget添加一行数据,可以先创建一个新行,然后在该行上设置各个单元格的数据。最后,将新行添加到QTableWidget中即可。代码示例:
```python
# 创建新行
new_row = QtWidgets.QTableWidgetItem()
self.tableWidget.insertRow(self.tableWidget.rowCount())
# 设置单元格数据
new_row.setText("数据1")
self.tableWidget.setItem(self.tableWidget.rowCount()-1, 0, new_row)
new_row.setText("数据2")
self.tableWidget.setItem(self.tableWidget.rowCount()-1, 1, new_row)
# 将新行添加到QTableWidget中
self.tableWidget.setItem(self.tableWidget.rowCount()-1)
```
相关问题
pyqt QTableWidget
### PyQt QTableWidget 使用教程及常见问题解决方案
#### 创建并显示QTableWidget实例
为了创建和展示`QTableWidget`,可以先初始化一个窗口,并在其中嵌入该部件。下面是一个简单的例子来说明如何实现这一点:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem
app = QApplication(sys.argv)
window = QMainWindow()
table_widget = QTableWidget(4, 3) # 行数为4,列数为3的表格
window.setCentralWidget(table_widget)
# 设置表头标签
table_widget.setHorizontalHeaderLabels(['姓名', '年龄', '性别'])
for row in range(4):
for column in range(3):
item = QTableWidgetItem(f'Item {row},{column}')
table_widget.setItem(row, column, item)
window.show()
app.exec_()
```
此代码片段展示了如何构建基本的`QTableWidget`对象,并填充一些数据项[^1]。
#### 调整QTableWidget尺寸适应内容
当希望使`QTableWidget`自动调整其大小以适合内部项目时,可以通过设置合适的大小策略和配置列宽比例来进行优化。具体做法如下所示:
```python
table_widget.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
header = table_widget.horizontalHeader()
header.setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
```
上述操作会使得整个表格尽可能扩展至父容器宽度,并让每一列表格均匀分布可用空间[^2]。
#### 响应单元格点击事件
对于监听特定单元格被选中的情况,可连接信号槽机制处理交互逻辑。这里给出一段示范性的代码用于捕捉用户的鼠标单击动作:
```python
def on_cell_clicked(row: int, col: int):
print(f"Cell clicked at ({row}, {col})")
table_widget.cellClicked.connect(on_cell_clicked)
```
每当有任意位置上的单元格受到用户触发后,都会调用指定的方法打印出对应行列坐标信息。
pyqt qtablewidget添加一行数据
要向PyQt QTableWidget添加一行数据,可以先创建一个新行,然后在该行上设置各个单元格的数据。最后,将新行添加到QTableWidget中即可。代码示例:
```python
# 创建新行
new_row = QtWidgets.QTableWidgetItem()
self.tableWidget.insertRow(self.tableWidget.rowCount())
# 设置单元格数据
new_row.setText("数据1")
self.tableWidget.setItem(self.tableWidget.rowCount()-1, 0, new_row)
new_row.setText("数据2")
self.tableWidget.setItem(self.tableWidget.rowCount()-1, 1, new_row)
# 将新行添加到QTableWidget中
self.tableWidget.setItem(self.tableWidget.rowCount()-1)
```
阅读全文
相关推荐
![](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)