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. Creating print option for simple calculation tool

Creating print option for simple calculation tool

Scheduled Pinned Locked Moved Unsolved Qt for Python
12 Posts 5 Posters 1.1k 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.
  • C Offline
    C Offline
    CivilEngineer
    wrote on 12 Oct 2019, 19:34 last edited by CivilEngineer
    #1

    Hello,

    I made a small app for some calculations as you can see from the screenshot and I would like to add a printing functionality to this.

    But as I'm a python as well as PyQt beginner, I'm struggling from the beginning.

    My aim is to get a document with small header followed by the content consisting of the input and output data from the calculation from the actual tab.

    So my question is: What is the usual way to achieve this?
    So far I think printDialog (from Qt Print Support) could do the job. But all the examples I found only show how to print the content of a textedit-widget.

    So, besides the question how to deal with the header, I'm stuck at this point:

    def printDialog():
        printer = QPrinter(QPrinter.HighResolution)
        dialog = QPrintDialog(printer, self)
    

    Application Screenshot

    Any ideas how to start?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 13 Oct 2019, 19:17 last edited by
      #2

      Hi and welcome to devnet,

      Do you want to print the content of the widget or rather some small report based on the numbers input and the result ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • C Offline
        C Offline
        CivilEngineer
        wrote on 13 Oct 2019, 20:08 last edited by
        #3

        Hi SGaist,

        thank you. Actually I would prefer a small report as you mentioned. I would like to create a template with some general information (address of the office, software version etc.) in the header and then a content area where the input and output of the actual tab is described and printed.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 13 Oct 2019, 20:50 last edited by
          #4

          Then you should check the Printing chapter and examples from the "C++ GUI Programming with Qt 4 (2nd Edition) ". They show various ways for printing generated document.

          In short, one simple way is to generate a basic html document with tables containing your values and then print that document.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • C Offline
            C Offline
            CivilEngineer
            wrote on 17 Oct 2019, 20:22 last edited by
            #5

            Hello SGaist,

            thanks a lot for your answer. This seems to be a very interesting way to do it. Unfortunately it's in C++ an Qt4 (I forgot to mention that I'm using Qt5). But now I know what I need to search for.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 17 Oct 2019, 21:30 last edited by
              #6

              Despite the Qt version, most of the book is still relevant for Qt 5.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • C Offline
                C Offline
                CivilEngineer
                wrote on 22 Oct 2019, 20:25 last edited by CivilEngineer
                #7

                I made some progress..

                I'm now using QTextDocument. But I'm stuck how to set the right paper margins. Here's my code:

                printer = QPrinter(QPrinter.HighResolution)
                printer.setFullPage(True)
                printer.setPageMargins(10,10.0,10.0,10.0, QPrinter.Millimeter)
                dialog = QPrintDialog(printer)
                
                header = """
                <html>
                <table width=\"100%" border=1 cellspacing=0>\n
                <tr><td bgcolor=\"lightgray\"><font size=\"+1\">
                <b><i>Softwarename</i></b></font>\n
                <tr><td>Version 0.1 | Copyright |\n
                </table>
                    </html>"""
                
                html = header + "\n Content..."
                
                document = QtGui.QTextDocument()
                document.setHtml(html)
                document.setPageSize(???)
                

                I want to get rid of the extra space of 20 mm from the QTextDocument. Do you know how to do this?

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 23 Oct 2019, 14:55 last edited by
                  #8

                  HI
                  As @Denni-0 says. each driver might/will have a non printable area
                  where you cannot print.
                  (unless special label printer)
                  Did you try
                  printer.setPageMargins(0,0,0,0, QPrinter.Millimeter)
                  and see if it then right next to the non printable area ?

                  1 Reply Last reply
                  1
                  • C Offline
                    C Offline
                    CivilEngineer
                    wrote on 23 Oct 2019, 18:52 last edited by
                    #9

                    Thanks for your advice. As I'm using a PDF printer the printable area of a printer doesn't need to be considered.

                    I have read in another thread, that QTextDocument just puts a border of 20 mm as a standard. And with the given printer.setPageMargins(10,10.0,10.0,10.0, QPrinter.Millimeter) the actual margin from the left is exactly 30 mm.

                    So the question is how to tell QTextDocument to make no extra margins. Unfortunately I'm not able to understand how to use the Qt Documentation..

                    JonBJ 1 Reply Last reply 23 Oct 2019, 19:12
                    0
                    • C CivilEngineer
                      23 Oct 2019, 18:52

                      Thanks for your advice. As I'm using a PDF printer the printable area of a printer doesn't need to be considered.

                      I have read in another thread, that QTextDocument just puts a border of 20 mm as a standard. And with the given printer.setPageMargins(10,10.0,10.0,10.0, QPrinter.Millimeter) the actual margin from the left is exactly 30 mm.

                      So the question is how to tell QTextDocument to make no extra margins. Unfortunately I'm not able to understand how to use the Qt Documentation..

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on 23 Oct 2019, 19:12 last edited by JonB
                      #10

                      @CivilEngineer said in Creating print option for simple calculation tool:

                      I have read in another thread, that QTextDocument just puts a border of 20 mm as a standard.

                      Can you supply a link to wherever you read that?

                      Or, are you just talking about https://doc.qt.io/qt-5/qtextdocument.html#documentMargin-prop? You should check what your QTextDocument ::documentMargin() currently returns. If it's non-zero you can alter via setDocumentMargin(). The docs sate "The default is 4.", so you might save a bit of what you are looking for by reducing to 0?

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        CivilEngineer
                        wrote on 23 Oct 2019, 19:24 last edited by CivilEngineer
                        #11

                        Unfortunately I missed the Thread where I read it. But I made some progres anyway. This line "document.setPageSize(QtCore.QSizeF(printer.pageRect().size()))" seems do to what I want.
                        With this there are all margins gone. But now the text height got extremely small :/

                        Here's my example:

                        def printcontent(self):
                            # Create printer
                            header = """
                            <html>
                            <table width=\"100%" border=1 cellspacing=0>\n
                            <tr><td bgcolor=\"lightgray\"><font size=\"+1\">
                            <b><i>Softwarename</i></b></font>\n
                            <tr><td>Version 0.01 | © Copyright 2019 |\n
                            </table>
                            </html>"""
                            html = header + "<p>Content...</p>"
                            printer = QPrinter(QPrinter.HighResolution)
                            #printer.setFullPage(True)
                            printer.setPageMargins(0,0,0,0, QPrinter.Millimeter)
                            printer.setPageSize(QPrinter.A4)
                            dialog = QPrintDialog(printer)
                        
                            document = QtGui.QTextDocument()
                            document.setHtml(html)
                            document.setPageSize(QtCore.QSizeF(printer.pageRect().size()))
                        

                        Edit: Here's the link where it's said that the margin is 20 mm by default: click me

                        JonBJ 1 Reply Last reply 23 Oct 2019, 19:57
                        0
                        • C CivilEngineer
                          23 Oct 2019, 19:24

                          Unfortunately I missed the Thread where I read it. But I made some progres anyway. This line "document.setPageSize(QtCore.QSizeF(printer.pageRect().size()))" seems do to what I want.
                          With this there are all margins gone. But now the text height got extremely small :/

                          Here's my example:

                          def printcontent(self):
                              # Create printer
                              header = """
                              <html>
                              <table width=\"100%" border=1 cellspacing=0>\n
                              <tr><td bgcolor=\"lightgray\"><font size=\"+1\">
                              <b><i>Softwarename</i></b></font>\n
                              <tr><td>Version 0.01 | © Copyright 2019 |\n
                              </table>
                              </html>"""
                              html = header + "<p>Content...</p>"
                              printer = QPrinter(QPrinter.HighResolution)
                              #printer.setFullPage(True)
                              printer.setPageMargins(0,0,0,0, QPrinter.Millimeter)
                              printer.setPageSize(QPrinter.A4)
                              dialog = QPrintDialog(printer)
                          
                              document = QtGui.QTextDocument()
                              document.setHtml(html)
                              document.setPageSize(QtCore.QSizeF(printer.pageRect().size()))
                          

                          Edit: Here's the link where it's said that the margin is 20 mm by default: click me

                          JonBJ Online
                          JonBJ Online
                          JonB
                          wrote on 23 Oct 2019, 19:57 last edited by JonB
                          #12

                          @CivilEngineer
                          The size of what text height? You're not outputting much.

                          <font size=\"+1\">

                          You sure that's right?

                          Separate matter: your content is going to go after the </html>. Not good.

                          1 Reply Last reply
                          0

                          1/12

                          12 Oct 2019, 19:34

                          • Login

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