Python Forum
[PyQt] QTableWidget print problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] QTableWidget print problem
#1
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
Reply
#2
There is an example on github

PyQt5 - Print paginated QTableWidget
JokerSob likes this post
Reply
#3
(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
Reply
#4
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)
JokerSob likes this post
Reply
#5
(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
Reply
#6
Video 
(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.
Reply
#7
How do you save it? Print to PDF?
Reply
#8
(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.

Attached Files

Thumbnail(s)
   
Reply
#9
Only someone who uses Windows can probably help there.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] How do I display the DB table I will choose from the QComboBox in QTableWidget JokerSob 2 2,324 Aug-05-2021, 03:00 PM
Last Post: JokerSob
  Displaying database info in QTableWidget thewolf 6 5,294 Apr-03-2021, 02:49 PM
Last Post: thewolf
  [PyQt] Help: check content of combobox in horizontal header of QTableWidget mart79 1 3,370 Jul-26-2020, 06:18 PM
Last Post: deanhystad
  [PyQt] Add validation (regex) to QTableWidget cells mart79 0 2,754 May-05-2020, 12:51 PM
Last Post: mart79
  [PyQt] QTableWidget stylesheet error mart79 3 6,455 Feb-26-2020, 04:54 PM
Last Post: Denni
  [PyQt] Pyqt5: How do you make a button that adds new row with data to a Qtablewidget YoshikageKira 6 7,028 Jan-02-2020, 04:32 PM
Last Post: Denni
  [PyQt] QTableWidget cell validation littleGreenDude 1 7,692 Jan-18-2019, 07:44 PM
Last Post: littleGreenDude
  Printing QTableWidget to an DIN A4 page Mady 2 7,235 Dec-18-2018, 06:56 PM
Last Post: Axel_Erfurt
  [PyQt]Find item or text in Qtablewidget maziio 0 11,284 Aug-06-2018, 01:37 PM
Last Post: maziio

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020