我制作了一个带有数据和导出到 pdf 按钮的 QTableWidget,我制作了一个功能,它进行下载并将 pdf 文件放入选定的文件夹中,但该文件无法打开。
这是我的代码
import test
class recordsWindow(QMainWindow):
def __init__(self, record_list=None):
super().__init__()
....
# present records chart
record_list = test.show_records(self)
self.tableWidget.setColumnCount(5)
self.tableWidget.setHorizontalHeaderLabels(["Blod Type", "Donation Date", "First Name", "Last Name", "ID"])
self.tableWidget.setColumnWidth(0, 125)
self.tableWidget.setColumnWidth(1, 125)
self.tableWidget.setColumnWidth(2, 125)
self.tableWidget.setColumnWidth(3, 125)
self.tableWidget.setColumnWidth(4,125)
self.tableWidget.setRowCount(len(record_list))
i=0
for record in record_list:
self.tableWidget.setItem(i, 0, QTableWidgetItem((record[0])))
self.tableWidget.setItem(i, 1, QTableWidgetItem((record[1])))
self.tableWidget.setItem(i, 2, QTableWidgetItem((record[2])))
self.tableWidget.setItem(i, 3, QTableWidgetItem((record[3])))
self.tableWidget.setItem(i, 4, QTableWidgetItem((record[4])))
i+=1
# activate export
self.pushButton.clicked.connect(self.exportPDF)
def exportPDF(self):
filename, _ = QFileDialog.getSaveFileName(self, "Export PDF", "", "PDF files (*.pdf)")
if filename:
printer = QPrinter(QPrinter.PrinterMode.HighResolution)
printer.setOutputFormat(QPrinter.OutputFormat.PdfFormat)
printer.setOutputFileName(filename)
printer.setPageSize(QPageSize(QPageSize.A4))
try:
painter = QPainter()
painter.begin(printer)
# render the table into the painter object
self.tableWidget.render(painter)
painter.end()
print("PDF generated successfully")
except Exception as e:
print("PDF generation failed: ", e)
实际上并没有生成 vlid 文件。也没有收到任何异常或错误。