如何创建正确跨越多页的可打印列表 [英] How to create printable list that correctly spans multiple pages

查看:56
本文介绍了如何创建正确跨越多页的可打印列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只是我的第二个 PyQt4 项目.开发将发票管理作为其组件之一的 Windows 应用.

This is only my second PyQt4 project. Developing a Windows app that has INVOICE management as one of its components.

我需要有关如何成功创建即使跨越多个页面也可以清晰打印的列表的建议.
类似于 QTableView 或 QTableWidget.

I need suggestions on how to successfully create a list that can be cleanly printed even if it spans across multiple pages.
Something like QTableView or QTableWidget.

很少有标题行(客户信息、发票编号等)后跟以下行:|SKU # |姓名 |描述 |数量 |价格
底部有一些小计和总计.

Few Header Lines (Client info, invoice number etc) followed by rows of: | SKU # | NAME | DESCRIPTION | QUANTITY | PRICE
With some subtotals and grand totals on the bottom.

当按下 [Ctrl]+P 或选择文件-->打印时,应弹出系统打印机对话框,允许您打印可以跨越多个 Letter 大小页面的发票.
或者,是否有一种快速简便的方法来实现打印预览?

When [Ctrl]+P is pressed or File-->Print is selected, system printer dialog should pop-up allowing you to print the invoice that can span multiple Letter sized pages.
Optionally, is there a quick and easy way to implement a print preview?

我不想花 3 天时间使用 QTableWidget + QPrinter 只是为了发现它不能做我需要它做的事情.从您过去的经验中寻求帮助/提示/见解,这将节省我的时间和精力.

I don't want to spend 3 days using QTableWidget + QPrinter only to find out that it cannot do what I need it to do. Looking for help/tips/insight from your past experience that will save me time and effort.

谢谢

推荐答案

我很确定你想要实现的一切都可以使用 PyQt4.

I pretty sure everything you want to achieve is possible using PyQt4.

我在下面添加了一个基本脚本,用于演示您所追求的功能.打印表格的格式非常粗糙,但您可以通过使用 html 而不是我使用的简单文本表格轻松实现更漂亮的东西.

I have added a basic script below which demonstrates the functionality you're after. The format of the printed table is very crude, but you could easily achieve something fancier by using html instead of the simple text-table I've used.

from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setWindowTitle(self.tr('Document Printer'))
        self.table = QtGui.QTableWidget(200, 5, self)
        for row in range(self.table.rowCount()):
            for col in range(self.table.columnCount()):
                item = QtGui.QTableWidgetItem('(%d, %d)' % (row, col))
                item.setTextAlignment(QtCore.Qt.AlignCenter)
                self.table.setItem(row, col, item)
        self.table.setHorizontalHeaderLabels(
            'SKU #|NAME|DESCRIPTION|QUANTITY|PRICE'.split('|'))
        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)
        table = cursor.insertTable(
            self.table.rowCount(), self.table.columnCount())
        for row in range(table.rows()):
            for col in range(table.columns()):
                cursor.insertText(self.table.item(row, col).text())
                cursor.movePosition(QtGui.QTextCursor.NextCell)
        document.print_(printer)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.resize(640, 480)
    window.show()
    sys.exit(app.exec_())

这篇关于如何创建正确跨越多页的可打印列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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