如何打印 QTableView [英] How to print a QTableView
本文介绍了如何打印 QTableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 Python 和 PySide 应用程序,它连接到 mysql 数据库并在 QTableView 中显示查询结果.我需要打印表格视图的内容.这是一些代码:
I have a Python and PySide app that connects to a mysql database and displays the results of a query in a QTableView. I need to print the contents of the table view. Here's some code:
self.db_table = QtGui.QTableView(self)
self.model = QtSql.QSqlQueryModel()
self.model.setQuery("SELECT * FROM simpsons")
self.model.setHeaderData(1, QtCore.Qt.Horizontal, self.tr("First Name"))
self.model.setHeaderData(2, QtCore.Qt.Horizontal, self.tr("Last Name"))
self.db_table.setModel(self.model)
self.print_btn = QtGui.QPushButton("Print")
self.print_btn.clicked.connect(self.print_btn_clicked)
def print_btn_clicked(self):
printDialog = QtGui.QPrintDialog(self.printer, self)
if printDialog.exec_() == QtGui.QDialog.Accepted:
#printing code
我找不到这方面的示例,而且我对文档的了解也不多,所以我希望得到一些帮助
I can't find an example for this and I don't understand much from the documentation so I'd appreciate some help
推荐答案
一种方法是将表格内容转储到 QTextDocument
中,然后打印出来.
One way to do it is to dump the table contents into a QTextDocument
, and then print that.
以下演示使用一个简单的文本表,但可以使用 html 来获得更复杂的格式:
The following demo uses a simple text-table, but html could be used to get more sophisticated formatting:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self, rows, columns):
QtGui.QWidget.__init__(self)
self.table = QtGui.QTableView(self)
model = QtGui.QStandardItemModel(rows, columns, self.table)
for row in range(rows):
for column in range(columns):
item = QtGui.QStandardItem('(%d, %d)' % (row, column))
item.setTextAlignment(QtCore.Qt.AlignCenter)
model.setItem(row, column, item)
self.table.setModel(model)
self.buttonPrint = QtGui.QPushButton('Print', self)
self.buttonPrint.clicked.connect(self.handlePrint)
self.buttonPreview = QtGui.QPushButton('Preview', self)
self.buttonPreview.clicked.connect(self.handlePreview)
layout = QtGui.QGridLayout(self)
layout.addWidget(self.table, 0, 0, 1, 2)
layout.addWidget(self.buttonPrint, 1, 0)
layout.addWidget(self.buttonPreview, 1, 1)
def handlePrint(self):
dialog = QtGui.QPrintDialog()
if dialog.exec_() == QtGui.QDialog.Accepted:
self.handlePaintRequest(dialog.printer())
def handlePreview(self):
dialog = QtGui.QPrintPreviewDialog()
dialog.paintRequested.connect(self.handlePaintRequest)
dialog.exec_()
def handlePaintRequest(self, printer):
document = QtGui.QTextDocument()
cursor = QtGui.QTextCursor(document)
model = self.table.model()
table = cursor.insertTable(
model.rowCount(), model.columnCount())
for row in range(table.rows()):
for column in range(table.columns()):
cursor.insertText(model.item(row, column).text())
cursor.movePosition(QtGui.QTextCursor.NextCell)
document.print_(printer)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window(25, 2)
window.resize(300, 400)
window.show()
sys.exit(app.exec_())
这篇关于如何打印 QTableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文