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.

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.