Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to print/screenshot a tabwidget to an image file (.jpg/.png) in high resolution?
Forum Updated to NodeBB v4.3 + New Features

How to print/screenshot a tabwidget to an image file (.jpg/.png) in high resolution?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.4k 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.
  • P Offline
    P Offline
    Piyush
    wrote on last edited by Piyush
    #1

    Hi,

    I am working on a piece of code wherein I have to print the tabwidget to an image file (.png/.jpg) file. The file is being created by the following code but i cannot view it.

    Code:

        ```
        printer.PrinterMode(QtPrintSupport.QPrinter.HighResolution)
        printer.setOrientation(QtPrintSupport.QPrinter.Landscape)
        printer.setPaperSize(QtCore.QSizeF(297, 420), QtPrintSupport.QPrinter.Millimeter)
    
        temp_pdf = "abc.png"
        printer.setOutputFileName(temp_pdf)
        painter = QtGui.QPainter(printer)
        painter.drawPixmap(0, 10, self.ui.tabWidget.grab())
        painter.end()
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Do you want to send the picture to a printer or why do you use QPrinter instead QPixmap::save()?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • P Offline
        P Offline
        Piyush
        wrote on last edited by
        #3

        I was actually working with PDF before this after which the requirement changed to image file. I am open to using QPixmap but I don't know how? Could you please help me with an example?

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Piyush
          wrote on last edited by
          #4

          tried Qpixmap.grabwidget but it seems to be deprecated in PyQt5.

          mrjjM 1 Reply Last reply
          0
          • P Piyush

            tried Qpixmap.grabwidget but it seems to be deprecated in PyQt5.

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

            @Piyush
            hi
            There is
            http://doc.qt.io/qt-5/qwidget.html#render
            which you can use to directly make tabwidget draw to pixmap.

            P 1 Reply Last reply
            4
            • mrjjM mrjj

              @Piyush
              hi
              There is
              http://doc.qt.io/qt-5/qwidget.html#render
              which you can use to directly make tabwidget draw to pixmap.

              P Offline
              P Offline
              Piyush
              wrote on last edited by
              #6

              @mrjj Can you share an example?

              mrjjM 1 Reply Last reply
              0
              • P Piyush

                @mrjj Can you share an example?

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

                @Piyush
                Hi
                There is not so much to say about it.

                // make bitmap that is size of widget
                QPixmap pixmap(widget->size());
                // ask widget to paint it self to the pixmap
                widget->render(&pixmap);
                
                P 1 Reply Last reply
                3
                • mrjjM mrjj

                  @Piyush
                  Hi
                  There is not so much to say about it.

                  // make bitmap that is size of widget
                  QPixmap pixmap(widget->size());
                  // ask widget to paint it self to the pixmap
                  widget->render(&pixmap);
                  
                  P Offline
                  P Offline
                  Piyush
                  wrote on last edited by
                  #8

                  @mrjj Thanks. It worked. Was a very simple and quick solution.

                  pixmap = QtGui.QPixmap(self.ui.tab_2.size())
                  self.ui.tab_2.render(pixmap)
                  pixmap.save('abc.jpg','jpg')
                  
                  1 Reply Last reply
                  1

                  • Login

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