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
QtWS25 Last Chance

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 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
                              • J JonB
                                25 Oct 2022, 11:39

                                @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 Offline
                                L Offline
                                LT-K101
                                wrote on 25 Oct 2022, 12:05 last edited by
                                #21

                                @JonB Well the Valentina report is written with Qt I have been able to integrate with PyQt5. The developers have not written a Dialog for the print method yet, when I asked I was told I can use pyQt5 to create a custom Dialog so I was hoping QPrintPreviewDialog will do the magic for me. Below is the image they suggested it could be done using pyqt.
                                printDialog.jpg

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

                                  @JonB Well the Valentina report is written with Qt I have been able to integrate with PyQt5. The developers have not written a Dialog for the print method yet, when I asked I was told I can use pyQt5 to create a custom Dialog so I was hoping QPrintPreviewDialog will do the magic for me. Below is the image they suggested it could be done using pyqt.
                                  printDialog.jpg

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

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

                                  Well the Valentina report is written with Qt I have been able to integrate with PyQt5.

                                  Is it really that hard to provide a link to this thing if you want help with it, after being asked to do so several times? I just don't get why you don't....

                                  A custom dialog to invite the user to print is not the same thing as a QPrintPreviewDialog to allow previewing of the output.

                                  L 1 Reply Last reply 25 Oct 2022, 12:22
                                  0
                                  • J JonB
                                    25 Oct 2022, 12:09

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

                                    Well the Valentina report is written with Qt I have been able to integrate with PyQt5.

                                    Is it really that hard to provide a link to this thing if you want help with it, after being asked to do so several times? I just don't get why you don't....

                                    A custom dialog to invite the user to print is not the same thing as a QPrintPreviewDialog to allow previewing of the output.

                                    L Offline
                                    L Offline
                                    LT-K101
                                    wrote on 25 Oct 2022, 12:22 last edited by
                                    #23

                                    @JonB Well even creating a custom Dialog is my headache now.Any assistance please?

                                    J 1 Reply Last reply 25 Oct 2022, 12:27
                                    0
                                    • L LT-K101
                                      25 Oct 2022, 12:22

                                      @JonB Well even creating a custom Dialog is my headache now.Any assistance please?

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 25 Oct 2022, 12:27 last edited by JonB
                                      #24

                                      @LT-K101
                                      Seriously, in what way? Create a QDialog to ask the user if they want to print, whether to file or physical printer? And/or maybe a QFileDialog to let the user pick a file/location to save to?

                                      Or maybe that inbuilt Windows Print dialog is all user needs anyway, seems to offer print to printer or to file?

                                      L 1 Reply Last reply 25 Oct 2022, 12:48
                                      0
                                      • J JonB
                                        25 Oct 2022, 12:27

                                        @LT-K101
                                        Seriously, in what way? Create a QDialog to ask the user if they want to print, whether to file or physical printer? And/or maybe a QFileDialog to let the user pick a file/location to save to?

                                        Or maybe that inbuilt Windows Print dialog is all user needs anyway, seems to offer print to printer or to file?

                                        L Offline
                                        L Offline
                                        LT-K101
                                        wrote on 25 Oct 2022, 12:48 last edited by
                                        #25

                                        @JonB said in How to create a Print Preview Dialog for a method:

                                        maybe that inbuilt Windows Print dialog is all user needs anyway, seems to offer print to printer or to file?

                                        Creating the a Dialog to offer print to printer or to file which will connect to my print method is what I need please.

                                        J 1 Reply Last reply 25 Oct 2022, 12:55
                                        0
                                        • L LT-K101
                                          25 Oct 2022, 12:48

                                          @JonB said in How to create a Print Preview Dialog for a method:

                                          maybe that inbuilt Windows Print dialog is all user needs anyway, seems to offer print to printer or to file?

                                          Creating the a Dialog to offer print to printer or to file which will connect to my print method is what I need please.

                                          J Offline
                                          J Offline
                                          JonB
                                          wrote on 25 Oct 2022, 12:55 last edited by JonB
                                          #26

                                          @LT-K101
                                          You write a dialog to ask the user whether they want to print and if they say yes you call printToLocalPrinter() or printToDisk() or similar. I'm not writing that for you, it's a dialog, I have no idea what your issue is. You also already have a printReport() shown earlier which does a getSaveFileName() and calls printToDisk(filename), so I don't know what your problem is.

                                          1 Reply Last reply
                                          0

                                          16/26

                                          25 Oct 2022, 11:09

                                          • Login

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