How to print with Pyqt5?

Asked

Viewed 135 times

0

I made a little program in Pyqt5 where I type the Name, Number, Phone,Address, description of services and values. Now I want to print it out as a budget, but using that function:

    def imprimir(self):
    imprimir = QPrinter(QPrinter.HighResolution)
    dialog = QPrintDialog(imprimir)

    if dialog.exec_()==QPrintDialog.Accepted:
        self.nome.print_(imprimir)
        self.cpf.print_(imprimir)

The program creates a page per label. 1 - How can I make it to print everything on one page? 2 - And if possible how to format this on the page?

Thanks in advance.

  • Use a QPainter, then you draw on it and then print

1 answer

0

See if that helps anything.

import sys
from PyQt5.QtPrintSupport import QPrintDialog, QPrinter, QPrintPreviewDialog
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QTextEdit, QToolTip, QVBoxLayout, QHBoxLayout

class Window(QWidget):
    def __init__(self, **ac):
        super( ).__init__(**ac)
        self.vbox = QVBoxLayout( )
        self.title = "Cadastro"
        self.top = 100
        self.left = 100
        self.width = 640
        self.height = 480
        self.setWindowIcon(QtGui.QIcon("img1.png"))
        self.setLayout(self.vbox)

        self.InitWindow( )

    def InitWindow(self):
        self.hbox = QHBoxLayout( )

        self.print_btn = QPushButton("IMPRIMIR", self)
        self.print_btn.clicked.connect(self.print)

        self.view_btn = QPushButton("VISUALIZAR", self)
        self.view_btn.clicked.connect(self.view)

        self.edt = QTextEdit(self)
        self.vbox.addWidget(self.edt)

        self.hbox.addWidget(self.print_btn)
        self.hbox.addWidget(self.view_btn)
        self.vbox.addLayout(self.hbox)

        self.setWindowTitle(self.title)
        self.setGeometry(self.top, self.left, self.width, self.height)
        self.show( )

    def print(self):
        prt = QPrinter(QPrinter.HighResolution)
        dialog = QPrintDialog(prt)

        if dialog.exec_( ) == QPrintDialog.Accepted:
            self.edt.print_(prt)

    def view(self):
        pt = QPrinter(QPrinter.HighResolution)
        prev = QPrintPreviewDialog(pt, self)
        prev.paintRequested.connect(self.preview)
        prev.exec_( )

    def preview(self, pt):
        self.edt.print_(pt)

App = QApplication(sys.argv)
window = Window( )
sys.exit(App.exec_( ))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.