Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Printing QWidget window using python

Printing QWidget window using python

Scheduled Pinned Locked Moved Solved Language Bindings
python
22 Posts 2 Posters 14.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • khakhilK Offline
    khakhilK Offline
    khakhil
    wrote on last edited by
    #5

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

    http://theweblogics.com/screen.png

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

    mrjjM 1 Reply Last reply
    0
    • khakhilK khakhil

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

      http://theweblogics.com/screen.png

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

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @khakhil
      im confused.
      so what is wrong with
      http://stackoverflow.com/questions/10705712/screenshot-of-a-window-using-python
      take image of whole thing and print that image??

      1 Reply Last reply
      1
      • khakhilK Offline
        khakhilK Offline
        khakhil
        wrote on last edited by khakhil
        #7

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

        (http://stackoverflow.com/questions/10705712/screenshot-of-a-window-using-python)

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

        mrjjM 1 Reply Last reply
        0
        • khakhilK khakhil

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

          (http://stackoverflow.com/questions/10705712/screenshot-of-a-window-using-python)

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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #8

          @khakhil
          HI
          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
          http://stackoverflow.com/questions/8193920/print-a-text-through-a-printer-using-pyqt4
          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);
            printer.setOrientation(QPrinter::Landscape);
            QPainter painter;
            painter.begin(&printer);
            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);
            painter.end();
          }
          

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

          khakhilK 1 Reply Last reply
          1
          • khakhilK Offline
            khakhilK Offline
            khakhil
            wrote on last edited by
            #9

            @mrjj

            The link http://stackoverflow.com/questions/8193920/print-a-text-through-a-printer-using-pyqt4
            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?

            mrjjM 1 Reply Last reply
            0
            • khakhilK khakhil

              @mrjj

              The link http://stackoverflow.com/questions/8193920/print-a-text-through-a-printer-using-pyqt4
              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?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #10

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

              khakhilK 1 Reply Last reply
              1
              • mrjjM mrjj

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

                khakhilK Offline
                khakhilK Offline
                khakhil
                wrote on last edited by
                #11

                @mrjj
                not working :( :(

                mrjjM 1 Reply Last reply
                0
                • khakhilK khakhil

                  @mrjj
                  not working :( :(

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #12

                  @khakhil said:
                  still getting?

                  "ÿØÿà

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

                  1 Reply Last reply
                  0
                  • khakhilK Offline
                    khakhilK Offline
                    khakhil
                    wrote on last edited by
                    #13

                    @mrjj

                          if info.completeSuffix() == 'html':
                               self.editor.setHtml(text)
                           else:
                               self.editor.setPlainText(text)
                    

                    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())
                        printer.setOrientation(printer.Landscape)
                        Painter= QtGui.QPainter() 
                        Painter.begin(printer)
                        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)
                        Painter.end()
                    

                    but not succeed.

                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #14

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

                      khakhilK 1 Reply Last reply
                      0
                      • mrjjM mrjj

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

                        khakhilK Offline
                        khakhilK Offline
                        khakhil
                        wrote on last edited by
                        #15

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

                        mrjjM 1 Reply Last reply
                        0
                        • khakhilK khakhil

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

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #16

                          @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.?

                          khakhilK 1 Reply Last reply
                          0
                          • mrjjM mrjj

                            @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.?

                            khakhilK Offline
                            khakhilK Offline
                            khakhil
                            wrote on last edited by
                            #17

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

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

                            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????

                            1 Reply Last reply
                            0
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #18

                              hi
                              it should print the image.
                              code looks ok.

                              could u try display the image in a label?
                              self.label.setPixmap(pix)

                              to see what u grab

                              1 Reply Last reply
                              0
                              • khakhilK Offline
                                khakhilK Offline
                                khakhil
                                wrote on last edited by
                                #19

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

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

                                same code works for grab screenshot and print it.

                                thanks @mrjj

                                mrjjM 1 Reply Last reply
                                1
                                • khakhilK khakhil

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

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

                                  same code works for grab screenshot and print it.

                                  thanks @mrjj

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #20

                                  @khakhil
                                  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 :)

                                  1 Reply Last reply
                                  0
                                  • mrjjM mrjj

                                    @khakhil
                                    HI
                                    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
                                    http://stackoverflow.com/questions/8193920/print-a-text-through-a-printer-using-pyqt4
                                    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);
                                      printer.setOrientation(QPrinter::Landscape);
                                      QPainter painter;
                                      painter.begin(&printer);
                                      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);
                                      painter.end();
                                    }
                                    

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

                                    khakhilK Offline
                                    khakhilK Offline
                                    khakhil
                                    wrote on last edited by
                                    #21

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

                                    mrjjM 1 Reply Last reply
                                    0
                                    • khakhilK khakhil

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

                                      mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #22

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

                                      1 Reply Last reply
                                      0

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved