def download_data(self): rows = [] # 获取表头数据 headers = [] for column in range(self.table_widget.columnCount()): headers.append(self.table_widget.horizontalHeaderItem(column).text()) rows.append(headers) # 获取表格数据 for row in range(self.table_widget.rowCount()): row_data = [] for column in range(self.table_widget.columnCount()): item = self.table_widget.item(row, column) if item is not None: row_data.append(item.text()) else: row_data.append('') rows.append(row_data) # 将数据写入 CSV 文件 file_path, _ = QFileDialog.getSaveFileName(self, '保存文件', '', 'CSV Files (*.csv)') if file_path: with open(file_path, 'w', newline='') as file: writer = csv.writer(file) writer.writerows(rows)添加下载成功后弹窗提示
时间: 2023-11-27 09:49:06 浏览: 158
Introduction to data.table.pdf
你可以在 `download_data` 函数的最后添加以下代码来实现下载成功后弹窗提示:
```python
QMessageBox.information(self, '提示', '文件下载成功!')
```
这样,当用户成功下载 CSV 文件时,就会出现一个提示框,告知用户下载成功。完整的代码如下:
```python
def download_data(self):
rows = []
# 获取表头数据
headers = []
for column in range(self.table_widget.columnCount()):
headers.append(self.table_widget.horizontalHeaderItem(column).text())
rows.append(headers)
# 获取表格数据
for row in range(self.table_widget.rowCount()):
row_data = []
for column in range(self.table_widget.columnCount()):
item = self.table_widget.item(row, column)
if item is not None:
row_data.append(item.text())
else:
row_data.append('')
rows.append(row_data)
# 将数据写入 CSV 文件
file_path, _ = QFileDialog.getSaveFileName(self, '保存文件', '', 'CSV Files (*.csv)')
if file_path:
with open(file_path, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(rows)
QMessageBox.information(self, '提示', '文件下载成功!')
```
阅读全文