Important: Please read the Qt Code of Conduct -

How to create printable list that correctly spans multiple pages

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

    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.

    UI Details
    Few Header Lines (Client info, invoice number etc) followed by rows of:


    With some subtotals and grand totals on the bottom

    Print Functionality
    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?

    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.


  • Unfortunately there is no quick and easy way.

    You will have to do the calculations yourself [[Doc:QFontInfo]] and some other metrics helpers will be of use here. I once did it myself, but I cannot reveal the code here, it's a closed source app and I would have to make it "understandable" for outsiders too :)

    Anyways - some random hints you should pay attention for:

    • use [[Doc:QFontInfo]] and its metrics helpers do determine the hight of a line - it can vary depending on the contents of some descriptive text and its line count
    • use the printer device for setting up metric calculations (respect the DPI settings!)
    • add the lines separating items (rows) and columns
    • if needed, handle alternating background colors, reset to the correct starting color on each page
    • calculate the height of your subtotals and grand totals parts at first and check if they will still fit onto the page before you print another row. You may have space for the row, but not the sub/grand total. It looks better if you move a regular item row to the next page, instead of having the grand total on a page alone
    • take into account that you need some padding between subsequent columns and rows
    • for the columns containing prices, amounts or units calculate the maximum width before you start to print and make the column wide enough to keep all values and the caption(!) (and add the padding!)
    • keep in mind that the size of your paper sheet can differ (US sizes, DIN sizes)

Log in to reply