0

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.

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by britanicus
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.