Hi,

What's the best and easiest way to make a table and print it on a printer?

1. I tried QTextEdit + QTextTable but it's a lot of work to move/set a cursor for each cell first and then add text to each cell
2. I also tried a QTableWidget but I don't understand how I can print it (couldn't find any examples)
3. I also tried rendering a QDialog itself but it looks aweful plus it shows the whole window, I just need the table

Does anyone know any examples? Thank you.

Dani AI

Generated

Short summary and practical options (building on 's tests and 's suggestion): there are two clean, commonly used approaches—(A) generate printable HTML and let a QTextDocument handle pagination and printing, or (B) render the actual widget (or its viewport) to a QPrinter via QPainter for a WYSIWYG result. Use QPrintDialog/QPrintPreviewDialog for user selection and preview. (doc.qt.io)

HTML -> QTextDocument (best for invoices/reports, automatic pagination). Build an HTML table from your model, put it into a QTextDocument, set the document page size to the printer page, then call the document print helper. This keeps layout and styling simple and the document API will paginate for you. Example (PyQt5/PySide style):

printer = QPrinter(QPrinter.HighResolution)
if QPrintDialog(printer).exec_() == QPrintDialog.Accepted:
    doc = QTextDocument()
    doc.setHtml(html_string)            # build html_string from your model
    doc.setPageSize(printer.pageRect().size())
    doc.print_(printer)

See the QTextDocument printing notes and an export-to-PDF example for the page-size trick. (doc.qt.io)

Widget render -> QPainter (best when you need exact on-screen look). Create a QPrinter, begin a QPainter on it, scale/translate to fit the page, then call myTable.render(painter) or myTable.viewport().render(painter) (viewport for just the cells). Use QPrintPreviewDialog to test the result and iterate on scaling/pagination. For very large tables prefer the HTML route or implement manual row-based pagination; rendering a huge widget can produce clipped or low-quality output if not scaled correctly. (stackoverflow.com)

Troubleshooting tips: ensure every cell has an item before reading it (avoid None), set printer.setOutputFormat(QPrinter.PdfFormat) when debugging to generate a PDF, and use preview to tune margins and scaling before printing.

Printing a table should not be any problem at all...

Say, table is the name of your QTableWidget, then to print, just iterate over the rows and columns, pick each QTableWidgetItem to get the text.

table = QTableWidget( NO_OF_ROWS, NO_OF_COLS )

# Add/Edit the data in the table
# Now to print it to the terminal

for row in range( table.rowCount() ) :
    for col in range( table.columnCount() ) :
        sys.stdout.write( table.itemAt( row, col ).text().leftJustified( MAX_SIZE ) )
        sys.stdout.flush()

    else :
        print

....
....

Here, MAX_SIZE is the length of the largest entry in the table

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.