【发布时间】:2019-04-02 07:26:11
【问题描述】:
我正在尝试从 QTableWidget 打印数据,它将跨度应用于几列。 Span 合并行以处理一对多关系。
问题:打印时忽略跨度格式。
class TableViewPage(QtWidgets.QMainWindow, Ui_table_MainWindow):
def __init__(self, parent=None):
super(TableViewPage, self).__init__(parent)
self.setupUi(self)
self.btn_load.clicked.connect(self.loadTable)
self.btn_preview.clicked.connect(self.handlePreview)
self.btn_print.clicked.connect(self.handlePrint)
for row in range(self.tableWidget.rowCount()):
for col in range(self.tableWidget.columnCount()):
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(row, col, item)
self.tableWidget.setHorizontalHeaderLabels(
'Party|Date|Product|Qty|Rate|Amt|Loading|Freight|Net|CD|Comm'.split('|'))
def loadTable(self):
connection = sqlite3.connect('main_databaseSample.db')
connection.execute("DROP VIEW IF EXISTS myview;")
query = "CREATE VIEW myview as SELECT dispatch_id, d_buyer," \
"d_date, product, quantity, rate, d_amount, d_addFreight, d_subFreight, d_netAmount " \
" from " \
"dispatch, dispatch_products WHERE dispatch.id_dispatch = dispatch_products.dispatch_id " \
"ORDER BY dispatch_id ASC;"
connection.execute(query)
query = "SELECT * FROM myview"
result = connection.execute(query)
last_id = -1
start_row = 0
for row, row_data in enumerate(result):
self.tableWidget.insertRow(row)
current_id, *other_values = row_data
for col, data in enumerate(other_values):
it = QtWidgets.QTableWidgetItem(str(data))
self.tableWidget.setItem(row, col, it)
if last_id != current_id and last_id != -1:
self.apply_span(start_row, row - start_row)
start_row = row
last_id = current_id
if start_row != row:
self.apply_span(start_row, self.tableWidget.rowCount() - start_row)
def apply_span(self, row, nrow):
if nrow <= 1:
return
for c in (0, 1, 5, 6, 7, 8):
self.tableWidget.setSpan(row, c, nrow, 1)
def handlePrint(self):
dialog = QtPrintSupport.QPrintDialog()
if dialog.exec_() == QtWidgets.QDialog.Accepted:
self.handlePaintRequest(dialog.printer())
def handlePreview(self):
dialog = QtPrintSupport.QPrintPreviewDialog()
dialog.paintRequested.connect(self.handlePaintRequest)
dialog.exec_()
def handlePaintRequest(self, printer):
document = QtGui.QTextDocument()
cursor = QtGui.QTextCursor(document)
table = cursor.insertTable(
self.tableWidget.rowCount(), self.tableWidget.columnCount())
for row in range(table.rows()):
for col in range(table.columns()):
cursor.insertText(self.tableWidget.item(row, col).text())
cursor.movePosition(QtGui.QTextCursor.NextCell)
document.print_(printer)
样本数据:
dispatch table:
ID date mill buyer addF subF amount cd comm netAmount
1 15-10 abc A 0 0 100 0 0 100
2 16-10 xyz B 0 0 200 0 0 200
dispatch_products table:
Dispatch_ID product qty rate
1 M 40 1
1 A 60 1
2 S 50 4
Code Output:
buyer date product quantity rate amount addFreight subFreight NetAmount
A 15-10 M 40 1 100 0 0 100
A 15-10 A 60 1 100 0 0 100
B 16-10 S 50 4 200 0 0 200
Expected Output:
buyer date product quantity rate amount addFreight subFreight NetAmount
A 15-10 M 40 1 100 0 0 100
A 60 1
B 16-10 S 50 4 200 0 0 200
使用 apply_span 函数,我可以将表格显示为“预期输出”,但在打印数据时打印为“代码输出”格式。
更新:在打印列名时也会被忽略。
【问题讨论】:
-
没有灵丹妙药的解决方案可以猜出你想要什么输出格式。如果您想以不同的方式格式化 span,则必须自己编写所有必要的代码来执行此操作(即检查 rowSpan 和/或 columnSpan,然后根据需要更改格式)。
-
@ekhumoro 我知道我必须自己编写代码,但是学习需要一些时间,而且我是 PYQt 的新手。另外,这里的问题是,在 QTableWidget 中已经实现了“预期输出”格式,但是在打印时格式丢失了。那么,打印前需要重新格式化吗?
-
你真的需要将表格打印为
QTextDocument吗?如果您只想直接打印表格,请查看this question 的答案。它显示了打印小部件的不同方式。所有小部件都有一个render 方法,允许您打印小部件在屏幕上显示的图像。
标签: python sqlite pyqt pyqt5 qtablewidget