Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to create a Print Preview Dialog for a method
Forum Updated to NodeBB v4.3 + New Features

How to create a Print Preview Dialog for a method

Scheduled Pinned Locked Moved Unsolved Qt for Python
26 Posts 4 Posters 3.4k Views
  • 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.
  • L Offline
    L Offline
    LT-K101
    wrote on 24 Oct 2022, 10:39 last edited by
    #1

    Hi,
    Is it possible to create a print preview dialog for a method that prints to a local printer. The print method does not display any GUI dialog for user to choose from available printers listed to be used. Below is the example I tried but it does not work. Please any assistance would be much appreciated. Thanks in advance.

    def printReport(self,printer):
        report details
    
    
    def printpreviewDialog(self):
        printer = QPrinter(QPrinter.HighResolution)
        previewDialog = QPrintPreviewDialog(printer, self)
        previewDialog.paintRequested.connect(self.printReport)
        previewDialog.exec_()
    
    
    J 1 Reply Last reply 24 Oct 2022, 10:51
    0
    • L LT-K101
      24 Oct 2022, 10:39

      Hi,
      Is it possible to create a print preview dialog for a method that prints to a local printer. The print method does not display any GUI dialog for user to choose from available printers listed to be used. Below is the example I tried but it does not work. Please any assistance would be much appreciated. Thanks in advance.

      def printReport(self,printer):
          report details
      
      
      def printpreviewDialog(self):
          printer = QPrinter(QPrinter.HighResolution)
          previewDialog = QPrintPreviewDialog(printer, self)
          previewDialog.paintRequested.connect(self.printReport)
          previewDialog.exec_()
      
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 24 Oct 2022, 10:51 last edited by
      #2

      @LT-K101 said in How to create a Print Preview Dialog for a method:

      but it does not work

      In what way it does not work?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply 24 Oct 2022, 11:09
      0
      • J jsulm
        24 Oct 2022, 10:51

        @LT-K101 said in How to create a Print Preview Dialog for a method:

        but it does not work

        In what way it does not work?

        L Offline
        L Offline
        LT-K101
        wrote on 24 Oct 2022, 11:09 last edited by
        #3

        @jsulm The printpreview Dialog pops up empty without the report deails

        J 1 Reply Last reply 24 Oct 2022, 11:22
        0
        • I Offline
          I Offline
          imene sa
          Banned
          wrote on 24 Oct 2022, 11:15 last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • L LT-K101
            24 Oct 2022, 11:09

            @jsulm The printpreview Dialog pops up empty without the report deails

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 24 Oct 2022, 11:22 last edited by
            #5

            @LT-K101 Are you doing anything in printReport slot?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            L 1 Reply Last reply 25 Oct 2022, 08:59
            2
            • J jsulm
              24 Oct 2022, 11:22

              @LT-K101 Are you doing anything in printReport slot?

              L Offline
              L Offline
              LT-K101
              wrote on 25 Oct 2022, 08:59 last edited by
              #6

              @jsulm Below is the printReport method details.

              def printReport(self,printer):
                  c = canvas.Canvas("hello.pdf")
                  c.drawString(100,700, "First time using reportlab")
                  c.save()
              
              J J 2 Replies Last reply 25 Oct 2022, 09:09
              0
              • L LT-K101
                25 Oct 2022, 08:59

                @jsulm Below is the printReport method details.

                def printReport(self,printer):
                    c = canvas.Canvas("hello.pdf")
                    c.drawString(100,700, "First time using reportlab")
                    c.save()
                
                J Offline
                J Offline
                JonB
                wrote on 25 Oct 2022, 09:09 last edited by
                #7

                @LT-K101
                And what is canvas, and what do you expect this to do? The docs for QPrintPreviewDialog::paintRequested(QPrinter *printer) tell you

                The printer instance supplied is the paint device onto which you should paint the contents of each page, using the QPrinter instance in the same way as you would when printing directly.

                L 1 Reply Last reply 25 Oct 2022, 09:21
                1
                • J JonB
                  25 Oct 2022, 09:09

                  @LT-K101
                  And what is canvas, and what do you expect this to do? The docs for QPrintPreviewDialog::paintRequested(QPrinter *printer) tell you

                  The printer instance supplied is the paint device onto which you should paint the contents of each page, using the QPrinter instance in the same way as you would when printing directly.

                  L Offline
                  L Offline
                  LT-K101
                  wrote on 25 Oct 2022, 09:21 last edited by
                  #8

                  @JonB I want to call the printReport() in the printpreviewDialog to initiate a QDialog popup for the user to view the pdf created in printReport method and then print.

                  J 1 Reply Last reply 25 Oct 2022, 09:25
                  0
                  • L LT-K101
                    25 Oct 2022, 09:21

                    @JonB I want to call the printReport() in the printpreviewDialog to initiate a QDialog popup for the user to view the pdf created in printReport method and then print.

                    J Offline
                    J Offline
                    JonB
                    wrote on 25 Oct 2022, 09:25 last edited by
                    #9

                    @LT-K101
                    Well, your current code does not presently do anything like that....

                    You are inside a method of an already-created QPrintPreviewDialog, at the paintRequested stage. This is not where or for popping up dialogs, and you don't want some dialog there to view the PDF since you are already inside a previewer.

                    1 Reply Last reply
                    0
                    • L LT-K101
                      25 Oct 2022, 08:59

                      @jsulm Below is the printReport method details.

                      def printReport(self,printer):
                          c = canvas.Canvas("hello.pdf")
                          c.drawString(100,700, "First time using reportlab")
                          c.save()
                      
                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 25 Oct 2022, 09:38 last edited by
                      #10

                      @LT-K101 said in How to create a Print Preview Dialog for a method:

                      Below is the printReport method details.

                      No wonder there is nothing in preview dialog.
                      As documentation explains (see @JonB post) you need to use the provided printer to generate the preview...

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      L 1 Reply Last reply 25 Oct 2022, 10:07
                      1
                      • J jsulm
                        25 Oct 2022, 09:38

                        @LT-K101 said in How to create a Print Preview Dialog for a method:

                        Below is the printReport method details.

                        No wonder there is nothing in preview dialog.
                        As documentation explains (see @JonB post) you need to use the provided printer to generate the preview...

                        L Offline
                        L Offline
                        LT-K101
                        wrote on 25 Oct 2022, 10:07 last edited by
                        #11

                        @jsulm Honestly, I'm lost since this is the first time trying to implement this, I was following some examples online which is getting me confused. Please how do I use the provided printer to generate the preview in my case. I know it easy for you and @JonB to do this.Please I need assistance with this. Thanks

                        J J 2 Replies Last reply 25 Oct 2022, 10:09
                        0
                        • L LT-K101
                          25 Oct 2022, 10:07

                          @jsulm Honestly, I'm lost since this is the first time trying to implement this, I was following some examples online which is getting me confused. Please how do I use the provided printer to generate the preview in my case. I know it easy for you and @JonB to do this.Please I need assistance with this. Thanks

                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 25 Oct 2022, 10:09 last edited by
                          #12

                          @LT-K101 Did you check https://doc.qt.io/qt-5/qtprintsupport-index.html ?

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          L 1 Reply Last reply 25 Oct 2022, 10:35
                          1
                          • L LT-K101
                            25 Oct 2022, 10:07

                            @jsulm Honestly, I'm lost since this is the first time trying to implement this, I was following some examples online which is getting me confused. Please how do I use the provided printer to generate the preview in my case. I know it easy for you and @JonB to do this.Please I need assistance with this. Thanks

                            J Offline
                            J Offline
                            JonB
                            wrote on 25 Oct 2022, 10:14 last edited by
                            #13

                            @LT-K101
                            Please start by following @jsulm's reference. You can also get help by Googling, say, qprintpreviewdialog paintrequested example. We do not even know what it is that you want to print/print preview....

                            1 Reply Last reply
                            0
                            • J jsulm
                              25 Oct 2022, 10:09

                              @LT-K101 Did you check https://doc.qt.io/qt-5/qtprintsupport-index.html ?

                              L Offline
                              L Offline
                              LT-K101
                              wrote on 25 Oct 2022, 10:35 last edited by
                              #14

                              @jsulm Please I did check it but I did not understand it

                              J 1 Reply Last reply 25 Oct 2022, 10:47
                              0
                              • L LT-K101
                                25 Oct 2022, 10:35

                                @jsulm Please I did check it but I did not understand it

                                J Offline
                                J Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on 25 Oct 2022, 10:47 last edited by
                                #15

                                @LT-K101 said in How to create a Print Preview Dialog for a method:

                                but I did not understand it

                                What exactly?
                                There is an example:

                                printer.setOutputFileName("print.ps");
                                QPainter painter;
                                painter.begin(&printer);
                                for (int page = 0; page < numberOfPages; ++page) {
                                    // Use the painter to draw on the page.
                                    if (page != lastPage)
                                        printer.newPage();
                                 }
                                painter.end();
                                

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                L 1 Reply Last reply 25 Oct 2022, 11:09
                                0
                                • J jsulm
                                  25 Oct 2022, 10:47

                                  @LT-K101 said in How to create a Print Preview Dialog for a method:

                                  but I did not understand it

                                  What exactly?
                                  There is an example:

                                  printer.setOutputFileName("print.ps");
                                  QPainter painter;
                                  painter.begin(&printer);
                                  for (int page = 0; page < numberOfPages; ++page) {
                                      // Use the painter to draw on the page.
                                      if (page != lastPage)
                                          printer.newPage();
                                   }
                                  painter.end();
                                  
                                  L Offline
                                  L Offline
                                  LT-K101
                                  wrote on 25 Oct 2022, 11:09 last edited by
                                  #16

                                  @JonB It's the C++ syntax that confuses me. Sorry for bothering you.Please below is a report method I want to preview and print. It works perfectly without a Dialog by saving to disk so I wanted to use QPrintPreviewDialog to let user preview the report before printing.

                                  def printReport(self):
                                      filename, ok = QtWidgets.QFileDialog.getSaveFileName(self, 'Save file', '', 'Pdf files (*.pdf)')  
                                      if ok:
                                           # Open local project
                                           project = valentina.project.connect('./temp.vsp')
                                  
                                           # Make report instance
                                           report = project.report(name='tempofficials_summary',
                                                                          dsn='sqlite://C:/Users/PC/Desktop/PROJECT/temp.db',
                                                                          query="SELECT firstname,surname,email,phone,address,dob, '{}' || image as full_path FROM temp_table WHERE id_no={}".format(
                                                                              'C:/Users/LTK101/Desktop/PROJECT/img/', id))
                                  
                                          # Print result as PDF
                                          report.printToDisk(filename)
                                  
                                  
                                  J J 2 Replies Last reply 25 Oct 2022, 11:22
                                  0
                                  • L LT-K101
                                    25 Oct 2022, 11:09

                                    @JonB It's the C++ syntax that confuses me. Sorry for bothering you.Please below is a report method I want to preview and print. It works perfectly without a Dialog by saving to disk so I wanted to use QPrintPreviewDialog to let user preview the report before printing.

                                    def printReport(self):
                                        filename, ok = QtWidgets.QFileDialog.getSaveFileName(self, 'Save file', '', 'Pdf files (*.pdf)')  
                                        if ok:
                                             # Open local project
                                             project = valentina.project.connect('./temp.vsp')
                                    
                                             # Make report instance
                                             report = project.report(name='tempofficials_summary',
                                                                            dsn='sqlite://C:/Users/PC/Desktop/PROJECT/temp.db',
                                                                            query="SELECT firstname,surname,email,phone,address,dob, '{}' || image as full_path FROM temp_table WHERE id_no={}".format(
                                                                                'C:/Users/LTK101/Desktop/PROJECT/img/', id))
                                    
                                            # Print result as PDF
                                            report.printToDisk(filename)
                                    
                                    
                                    J Offline
                                    J Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on 25 Oct 2022, 11:22 last edited by
                                    #17

                                    @LT-K101 said in How to create a Print Preview Dialog for a method:

                                    I wanted to use QPrintPreviewDialog to let user preview the report before printing.

                                    Then please do what was suggested here. Something like (should not be difficult to translate it to Python):

                                    def reportPreview(self, printer):
                                        QPainter painter;
                                        painter.begin(printer);
                                        for (int page = 0; page < numberOfPages; ++page) {
                                            // Use the painter to draw on the page.
                                            // Put here code to draw preview using painter
                                            if (page != lastPage)
                                                printer.newPage();
                                         }
                                        painter.end();
                                    
                                    def printpreviewDialog(self):
                                        printer = QPrinter(QPrinter.HighResolution)
                                        previewDialog = QPrintPreviewDialog(printer, self)
                                        previewDialog.paintRequested.connect(self.reportPreview)
                                        previewDialog.exec_()
                                    

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0
                                    • L LT-K101
                                      25 Oct 2022, 11:09

                                      @JonB It's the C++ syntax that confuses me. Sorry for bothering you.Please below is a report method I want to preview and print. It works perfectly without a Dialog by saving to disk so I wanted to use QPrintPreviewDialog to let user preview the report before printing.

                                      def printReport(self):
                                          filename, ok = QtWidgets.QFileDialog.getSaveFileName(self, 'Save file', '', 'Pdf files (*.pdf)')  
                                          if ok:
                                               # Open local project
                                               project = valentina.project.connect('./temp.vsp')
                                      
                                               # Make report instance
                                               report = project.report(name='tempofficials_summary',
                                                                              dsn='sqlite://C:/Users/PC/Desktop/PROJECT/temp.db',
                                                                              query="SELECT firstname,surname,email,phone,address,dob, '{}' || image as full_path FROM temp_table WHERE id_no={}".format(
                                                                                  'C:/Users/LTK101/Desktop/PROJECT/img/', id))
                                      
                                              # Print result as PDF
                                              report.printToDisk(filename)
                                      
                                      
                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 25 Oct 2022, 11:24 last edited by
                                      #18

                                      @LT-K101
                                      Noone can answer this without knowing exactly what a valentina.project is. I see it has some printToDisk() method. QPrintPreviewDialog requires a QPrinter. So does a valentina.project offer to print to a QPrinter?

                                      L 1 Reply Last reply 25 Oct 2022, 11:36
                                      0
                                      • J JonB
                                        25 Oct 2022, 11:24

                                        @LT-K101
                                        Noone can answer this without knowing exactly what a valentina.project is. I see it has some printToDisk() method. QPrintPreviewDialog requires a QPrinter. So does a valentina.project offer to print to a QPrinter?

                                        L Offline
                                        L Offline
                                        LT-K101
                                        wrote on 25 Oct 2022, 11:36 last edited by
                                        #19

                                        @JonB It has printToDisk() which save the report as pdf and printToLocalPrinter() which print to any default printer set as default without a dialog so I was trying to see if I could use QPrintPreviewDialog.

                                        J 1 Reply Last reply 25 Oct 2022, 11:39
                                        0
                                        • L LT-K101
                                          25 Oct 2022, 11:36

                                          @JonB It has printToDisk() which save the report as pdf and printToLocalPrinter() which print to any default printer set as default without a dialog so I was trying to see if I could use QPrintPreviewDialog.

                                          J Offline
                                          J Offline
                                          JonB
                                          wrote on 25 Oct 2022, 11:39 last edited by JonB
                                          #20

                                          @LT-K101 said in How to create a Print Preview Dialog for a method:

                                          printToLocalPrinter() which print to any default printer

                                          If that accepts a QPrinter it can be sent to a QPrintPreviewDialog. But it does not look like it can? Looks like it does something about the printer internally instead?

                                          If I had been asking for help on whatever a valentina.project is or whatever your "report"/"report writer" is, which provides these print...() calls, I would have supplied a link reference to it, plus however it interacts with Qt. But you would like us to answer without having that. I'm not even sure it integrates with Qt, let alone QPrinter, if it is just some Python thing what Qt integration does it have?

                                          L 1 Reply Last reply 25 Oct 2022, 12:05
                                          0

                                          1/26

                                          24 Oct 2022, 10:39

                                          • Login

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