如何在 QTableWidget 中打印数据库中的数据? [英] How can I print data from my database while in QTableWidget?

查看:153
本文介绍了如何在 QTableWidget 中打印数据库中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将我的数据从我的数据库 (sqlite) 打印到我的 GUI 表中的记事本/word 文档中(使用相同的格式).这是我的 gui 上表示的表格的代码.

How can I print my data from my database (sqlite) into a notepad /word document from my table in my GUI (using the same formatting). Here is my code for the table which is represented on my gui.

class Table(QtGui.QDialog):
    def __init__(self):
        super(Table, self).__init__()
        with sqlite3.connect('database.db') as db:
            cursor=db.cursor()
            cursor.execute('select* from Receipt Order BY ReceiptID ASC')
            title = [cn[0] for cn in cursor.description]
            rows = [cn[0] for cn in cursor.description]
            cur=cursor.fetchall()
            layout = QtGui.QGridLayout() 
            self.table = QtGui.QTableWidget()
            self.setGeometry(500,500,500,400)
            qr = self.frameGeometry()
            cp = QtGui.QDesktopWidget().availableGeometry().center()
            qr.moveCenter(cp)
            self.move(qr.topLeft())
            self.label2=QtGui.QLabel(self)
            self.label2.setPixmap(QtGui.QPixmap('receipt_pic.jpg'))
            self.label2.setGeometry(0,0,500,400)
            self.table.setColumnCount(2)
            self.table.setHorizontalHeaderLabels(title)
            for i,row in enumerate(cur):
                self.table.insertRow(self.table.rowCount())
                for j,val in enumerate(row):
                    self.table.setItem(i, j, QtGui.QTableWidgetItem(str(val)))
            layout.addWidget(self.table, 0, 0)
            self.setLayout(layout)
            self.setWindowTitle('Receipt Table')

我希望能够单击一个按钮,将这些信息(将显示为带有填充列和行的表格)复制到一个单独的记事本文件/或任何文本文档(我可以将表格发送到打印机以打印).

I want to be able to click a button which copies this information (which would appear as a table with filled columns and rows) into a seperate notepad file / or any text document (where I can send the table to the printer to be printed).

推荐答案

直接打印表格可能比使用中间文件更容易.

It would probably be easier to just print the table directly, rather than use an intermediary file.

为此,您可以使用 QTextDocument 创建一个表格的可打印表示,然后使用内置的打印对话框来完成剩下的工作.

To do this, you can use a QTextDocument to create a printable representation of the table, and then use the built-in print dialogs to do the rest.

所以,首先添加一些按钮:

So, first add some buttons:

    ...
    layout.addWidget(self.table, 0, 0, 1, 2)
    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.addWidget(self.buttonPrint, 1, 0)
    layout.addWidget(self.buttonPreview, 1, 1)
    self.setLayout(layout)
    ...

然后是打印和打印预览对话框的一些处理程序:

then some handlers for the print and print-preview dialogs:

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_()

最后是一些创建和打印文档的方法:

and finally some methods for creating the document and printing it:

def handlePaintRequest(self, printer):
    document = self.makeTableDocument()
    document.print_(printer)

def makeTableDocument(self):
    document = QtGui.QTextDocument()
    cursor = QtGui.QTextCursor(document)
    rows = self.table.rowCount()
    columns = self.table.columnCount()
    table = cursor.insertTable(rows + 1, columns)
    format = table.format()
    format.setHeaderRowCount(1)
    table.setFormat(format)
    format = cursor.blockCharFormat()
    format.setFontWeight(QtGui.QFont.Bold)
    for column in range(columns):
        cursor.setCharFormat(format)
        cursor.insertText(
            self.table.horizontalHeaderItem(column).text())
        cursor.movePosition(QtGui.QTextCursor.NextCell)
    for row in range(rows):
        for column in range(columns):
            cursor.insertText(
                self.table.item(row, column).text())
            cursor.movePosition(QtGui.QTextCursor.NextCell)
    return document

如果您更喜欢保存到文件,可以使用 QTextDocument.toHtml 将表格转储为 html.

If you prefer to save to a file, you can use QTextDocument.toHtml to dump the table as html.

打印出来的结果很基本,但如果你想要更奇特的东西,你总是可以使用 html/css 来构建文档.

The printed results are quite basic, but if you want something more fancy, you can always build the document using html/css.

这篇关于如何在 QTableWidget 中打印数据库中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆