Printing QWidget window using python

  • hey....
    I want to print window. print command works well but return a blank page.
    i have tried following -

    def printer(self):
    def handlePrint(self):
        dialog = QtGui.QPrintDialog()
        if dialog.exec_() == QtGui.QDialog.Accepted:
    def handlePaintRequest(self, printer):        
        db = MySQLdb.connect("localhost","root","","kailash")
        db_cursor = db.cursor()     
            a="SELECT * FROM customer_list WHERE id = '%s'" % ( self.receipt_id)
            result = db_cursor.fetchall()
            for row in result:
                date = row[1]
                name = row[2]
                envoice_no =row [3]
                weight = row[4]
                sample = row[5]
                purity = row[6]
        except Exception, e:
            print e
        document = QtGui.QTextDocument()
        cursor = QtGui.QTextCursor(document)       

    if I using cursor.insertText('hi') then printer returns a page with hi . but not return data fetching from database.

  • Qt Champions 2017

    @khakhil said:
    have you tested with a fixed select so u know it 100% works

    "SELECT * FROM customer_list WHERE id =666"

    to see if it does enter the
    " for row in result:" part.

    if yes, then next question is
    label_23 is a Label?

    Im asking as later you print the document
    Those Labels in not part of that and hence dont print.

    instead of labels, do you mean
    self.label_23.setText(date) -> cursor.insertText(date)

    so data from DB is printed as a document ?

    Or do you mean to take a screenshots of the labels and sort of print that?

    ps. im python noob

  • No, I do not want screen shot of window.

    I need to send it to printer. I want to print the current widget as it is.

    It is sending to printer, but the output is blank. How can I get widget/window to print ?

  • Qt Champions 2017

    but if u dont want screen shot of the labels (and print that),
    how do you mean to print them?
    a Label is just some text, so you might as well just print it yourself?

    Currently you are only printing the document. so unless you insert into that, its
    not printed.

    in c++ you can draw text to printer like this
    QPrinter printer(QPrinter::HighResolution);
    QPainter painter;

    is it what you want?
    a Label cannot just print it self
    but you can ask it to draw to the painter
    with the render function.

  • No, I do not want to print only just labels. I need to print whole window. Please see following image.

    On click of print button, I need to get this window by printer.

  • Qt Champions 2017

    im confused.
    so what is wrong with
    take image of whole thing and print that image??

  • thanks @mrjj
    i follow the link you have sent to me


    It works, now i want to get that image/screenshot from directory and send it to printer.
    please help.

  • Qt Champions 2017

    you will need a way to select real printer
    then load the image from file
    and draw onto a painter connected to the selected printer.

    try to play around with this sample
    its with preview and all so maybe over the top

    here is c++ function that grabs screenshot and print it.
    (thx to @Ni-Sumi)

    void MainWindow::printPage() {
      QPixmap pix = QPixmap::grabWindow(QApplication::desktop()->winId());
      QPrinter printer(QPrinter::HighResolution);
      QPainter painter;
      double xscale = printer.pageRect().width() / double(pix.width());
      double yscale = printer.pageRect().height() / double(pix.height());
      double scale = qMin(xscale, yscale);
      painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2,
                        printer.paperRect().y() + printer.pageRect().height() / 2);
      painter.scale(scale, scale);
      painter.translate(-width() / 2, -height() / 2);
      painter.drawPixmap(0, 0, pix);

    I assume u can read enough to translate to py. :)

  • @mrjj

    The link
    is very helpful. I solved 90% of my problem.
    now, the problem is that as i select the image to print, the printer not getting the image.
    instead of image it takes "ÿØÿà" and prints it.

    what i do?

  • Qt Champions 2017

    it looks like it prints the pointer?
    did you use painter.drawPixmap ?

  • @mrjj
    not working :( :(

  • Qt Champions 2017

    @khakhil said:
    still getting?


    can you post the code. maybe someone can spot the issue :)

  • @mrjj

          if info.completeSuffix() == 'html':

    is there any way like this for .jpg or .tif format ???

    for the same issue i am also trying the following as @mrjj suggest

    class Window(QtGui.QWidget):
      def __init__(self):
        super(Window, self).__init__()
        self._new_window = None
        printer = QtGui.QPrinter()
        Pixmap = QtGui.QPixmap()
        pix = QPixmap.grabWindow(QApplication.desktop().winId())
        Painter= QtGui.QPainter() 
        xscale = printer.pageRect().width() / (pix.width())
        yscale = printer.pageRect().height() / (pix.height())
        scale = qmin(xscale, yscale)
        Painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2,
                          printer.paperRect().y() + printer.pageRect().height() / 2);
        Painter.scale(scale, scale)
        Painter.translate(-width() / 2, -height() / 2)
        Painter.drawPixmap(0, 0, pix)

    but not succeed.

  • Qt Champions 2017

    so it just print "ÿØÿà on piece of paper?

  • @mrjj
    yes.. it is printing sometimes II* and sometimes "ÿØÿà" .

  • Qt Champions 2017

    @khakhil said:

    def init(self):

    can you try to move the code to a button? ( to a clicked slot)

    I assume
    def init(self):
    is the constructor?
    Might not be the best spot as object might not be fully shown or
    the (QApplication.desktop().winId()) dont return what we think.

    Could u also add a Painter.drawText(200,200, "hello") and see if that comes.?

  • @mrjj
    hey thanks..
    this code is working properly.

     printer = QtGui.QPrinter()
        Pixmap = QtGui.QPixmap() 
        pix= QPixmap.grabWindow(loginwindow.winId())
        Painter= QtGui.QPainter() 
        Painter.drawPixmap(50, 490, pix)

    It saves images every time i ran the code .
    what should i do if i don't want to save it but directly print it????

  • Qt Champions 2017

    it should print the image.
    code looks ok.

    could u try display the image in a label?

    to see what u grab

  • @mrjj
    thank you so much for help !!
    problem solved :) :)

    printer = QtGui.QPrinter()
      Pixmap = QtGui.QPixmap() 
      pix= QPixmap.grabWindow(loginwindow.winId())
      Painter= QtGui.QPainter() 
      Painter.drawPixmap(50, 490, pix)

    same code works for grab screenshot and print it.

    thanks @mrjj

  • Qt Champions 2017

    super! :)
    as a little note.
    If printer has high DPI, image might become really small.
    if that happens u can use the scale function of the pixmap to make it bigger or
    use the Painter.translate() as c++ sample showed :)

  • thanks @mrjj
    I wll try it later
    you saved my life :)

  • Qt Champions 2017

    hehe super :)
    Good work.
    Im complete python noob but syntax is somewhat the same :)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.