Python Forum

Full Version: QTableWidget print problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I created a print button for the QTableWidget where I display the db data, but after the dialog window that appears, I get an error. my code is as below :

def PrintPrinter(self):
    printer = QPrinter(QPrinter.HighResolution)
    dialog = QPrintDialog(printer, self)
    if dialog.exec_() == QPrintDialog.Accepted:
        self.ui.table_PrinterData.print_(printer)
if the result:
AttributeError: 'PySide2.QtWidgets.QTableWidget' object has no attribute 'print_'

It takes a screenshot of the tablewidget when I use render() instead of "print_"

I tried to look for examples from external sources but only found published examples on QTableView. I need information on how to proceed
There is an example on github

PyQt5 - Print paginated QTableWidget
(Aug-05-2021, 08:57 PM)Axel_Erfurt Wrote: [ -> ]There is an example on github

PyQt5 - Print paginated QTableWidget

i hadn't seen this... i'll try it now i hope it works...thank you
to style the borders:

    def handlePaintRequest(self, printer):
        tableFormat = QtGui.QTextTableFormat()
        tableFormat.setBorder(0.5)
        tableFormat.setBorderStyle(3)
        tableFormat.setCellSpacing(0);
        tableFormat.setTopMargin(0);
        tableFormat.setCellPadding(4)
        document = QtGui.QTextDocument()
        cursor = QtGui.QTextCursor(document)
        table = cursor.insertTable(
            self.table.rowCount(), self.table.columnCount(), tableFormat)
        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)
(Aug-05-2021, 09:15 PM)Axel_Erfurt Wrote: [ -> ]to style the borders:

    def handlePaintRequest(self, printer):
        tableFormat = QtGui.QTextTableFormat()
        tableFormat.setBorder(0.5)
        tableFormat.setBorderStyle(3)
        tableFormat.setCellSpacing(0);
        tableFormat.setTopMargin(0);
        tableFormat.setCellPadding(4)
        document = QtGui.QTextDocument()
        cursor = QtGui.QTextCursor(document)
        table = cursor.insertTable(
            self.table.rowCount(), self.table.columnCount(), tableFormat)
        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)

i managed to adapt it now it's working...now i can work on the excel export method
(Aug-05-2021, 08:57 PM)Axel_Erfurt Wrote: [ -> ]There is an example on github

PyQt5 - Print paginated QTableWidget

Hi! I checked the code, but when I save the PDF file and open it, it doesn't load.
How do you save it? Print to PDF?
(Jan-28-2022, 12:28 PM)Axel_Erfurt Wrote: [ -> ]How do you save it? Print to PDF?

When we get to the print form we have the option to save it in PDF, then when I run the PDF it doesn't open.
Only someone who uses Windows can probably help there.