0 Replies - 575 Views - Last Post: 29 August 2019 - 01:03 PM Rate Topic: -----

#1 summerbye   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-August 19

PyQt5 - print a QTable(with QPrinter)

Posted 29 August 2019 - 01:03 PM

Hi! I'm trying to print a QTableWidget but I canít really succeed. At first, i tried with the QPrinter class, but online Ive found tutorials and answers only for TextEdit. Anyway, i've tried to apply them for the QTableWidget and i get errors as such:

AttributeError: 'QTableWidget' object has no attribute 'print_'

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtPrintSupport import QPrintDialog, QPrinter


class Ui_SecondWindow(object):
    def setupUi(self, SecondWindow):
        Secondwindow.setObjectName("SecondWindow")
        Secondwindow.resize(689, 518)
        self.centralwidget = QtWidgets.QWidget(SecondWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(20, 10, 661, 441))
        self.tableWidget.setMaximumSize(QtCore.QSize(661, 16777215))
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(6)
        self.tableWidget.setRowCount(0)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(0, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(1, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(2, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(3, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(4, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(5, item)
        Secondwindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(SecondWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 689, 22))
        self.menubar.setObjectName("menubar")
        self.menuPrint = QtWidgets.QMenu(self.menubar)
        self.menuPrint.setObjectName("menuPrint")
        Secondwindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(SecondWindow)
        self.statusbar.setObjectName("statusbar")
        Secondwindow.setStatusBar(self.statusbar)
        self.menubar.addAction(self.menuPrint.menuAction())

        self.retranslateUi(SecondWindow)
        QtCore.QMetaObject.connectSlotsByName(SecondWindow)

    def retranslateUi(self, SecondWindow):
        _translate = QtCore.QCoreApplication.translate
        Secondwindow.setWindowTitle(_translate("SecondWindow", "SecondWindow"))
        item = self.tableWidget.horizontalHeaderItem(0)
        item.setText(_translate("SecondWindow", "Nume"))
        item = self.tableWidget.horizontalHeaderItem(1)
        item.setText(_translate("SecondWindow", "Cantitate"))
        item = self.tableWidget.horizontalHeaderItem(2)
        item.setText(_translate("SecondWindow", "Diametru"))
        item = self.tableWidget.horizontalHeaderItem(3)
        item.setText(_translate("SecondWindow", "Lungime"))
        item = self.tableWidget.horizontalHeaderItem(4)
        item.setText(_translate("SecondWindow", "Greutate"))
        item = self.tableWidget.horizontalHeaderItem(5)
        item.setText(_translate("SecondWindow", "Figura"))
        self.menuPrint.setTitle(_translate("SecondWindow", "Print"))

    def create_print(self):
        printer = QPrinter(QPrinter.HighResolution)
        dialog = QPrintDialog(printer, self)
        if dialog.exec_() == QPrintDialog.Accepted:
            self.tableWidget.print_(printer)




if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    SecondWindow = QtWidgets.QMainWindow()
    ui = Ui_SecondWindow()
    ui.setupUi(SecondWindow)
    Secondwindow.show()
    sys.exit(app.exec_())




Now I'm just trying to think of an alternative of printing the table widget(exporting it in some way, with a website or even giving up on the qtablewidget and trying putting the data into a excel file).

What should I do ?

Thank you!

Is This A Good Question/Topic? 0
  • +

Page 1 of 1