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. Print a text file with Python and Qt platform independent
Forum Update on Monday, May 27th 2025

Print a text file with Python and Qt platform independent

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 1.0k 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.
  • S Offline
    S Offline
    SGaist
    Lifetime Qt Champion
    wrote on 28 Jun 2020, 18:07 last edited by
    #2

    Hi,

    Use the QtPrintSupport module.

    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
    3
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 28 Jun 2020, 18:09 last edited by
      #3

      Hi
      You just use QPrinter
      https://doc.qt.io/qtforpython/PySide2/QtPrintSupport/QPrinter.html
      If you are using a QTExtEdit for the editor you can ask it to print it directly
      editor->document()->print(&printer);
      (c++ syntax)

      1 Reply Last reply
      2
      • P Offline
        P Offline
        PythonQTMarlem
        wrote on 28 Jun 2020, 18:49 last edited by
        #4

        @mrjj said in Print a text file with Python and Qt platform independent:

        print(&printer)

        i uses QPlainTextEdit.
        i tried this:

        self.texteditor.document().print(printer)
        

        but it crashes:
        Process finished with exit code -1073740791 (0xC0000409)

        What can I do?

        1 Reply Last reply
        0
        • P Offline
          P Offline
          PythonQTMarlem
          wrote on 28 Jun 2020, 19:51 last edited by
          #5

          new try:

                  def dateidruckentest1():
                      self.statusBar().showMessage("Datei wird gedruckt")
                      drucker = QPrinter
                      drucker.setPrinterName("Standarddrucker")
                      druckerdialog = QPrintDialog(drucker,self)
                      if druckerdialog.exec() == QDialog.Rejected: return
                      self.texteditor.print(drucker)
          

          It cashes: Process finished with exit code -1073740791 (0xC0000409)

          M 1 Reply Last reply 28 Jun 2020, 19:54
          0
          • P PythonQTMarlem
            28 Jun 2020, 19:51

            new try:

                    def dateidruckentest1():
                        self.statusBar().showMessage("Datei wird gedruckt")
                        drucker = QPrinter
                        drucker.setPrinterName("Standarddrucker")
                        druckerdialog = QPrintDialog(drucker,self)
                        if druckerdialog.exec() == QDialog.Rejected: return
                        self.texteditor.print(drucker)
            

            It cashes: Process finished with exit code -1073740791 (0xC0000409)

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 28 Jun 2020, 19:54 last edited by mrjj
            #6

            Hi
            Im not sure why it should crash.

            What line do it crash in ?

            self.texteditor is allocated ?

            Also not being into python.

            is
            printer = QPrinter()
            the same as
            drucker = QPrinter
            with no () ?

            Also did you try the sample from the docs `?

            printer = QPrinter()
            printer.setOutputFormat(QPrinter.PdfFormat)
            printer.setOutputFileName("/foobar/nonwritable.pdf")
            QPainter painter
            if painter.begin(printer):  # failed to open file
                print "failed to open file, is it writable?"
                return 1
            
            painter.drawText(10, 10, "Test")
            if !printer.Page():
                print "failed in flushing page to disk, disk full?"
                return 1
            
            painter.drawText(10, 10, "Test 2")
            painter.end()
            

            Just to see it does work.

            1 Reply Last reply
            0
            • P Offline
              P Offline
              PythonQTMarlem
              wrote on 28 Jun 2020, 20:18 last edited by
              #7

              This errormessage comes:
              C:\Projekte\Python\PQT-Texteditor>C:\Projekte\Python\PQT-Texteditor\venv\Scripts\python.exe C:/Projekte/Python/PQT-Texteditor/PQTTexteditor.py
              QMainWindowLayout::addItem: Please use the public QMainWindow API instead
              Traceback (most recent call last):
              File "C:/Projekte/Python/PQT-Texteditor/PQTTexteditor.py", line 195, in dateidruckentest1
              drucker.setPrinterName("Standarddrucker")
              TypeError: setPrinterName(self, str): first argument of unbound method must have type 'QPrinter'

              the exmple in the docs don't help, cause is painter and not the content of an editfield.

              M 1 Reply Last reply 28 Jun 2020, 20:27
              0
              • P PythonQTMarlem
                28 Jun 2020, 20:18

                This errormessage comes:
                C:\Projekte\Python\PQT-Texteditor>C:\Projekte\Python\PQT-Texteditor\venv\Scripts\python.exe C:/Projekte/Python/PQT-Texteditor/PQTTexteditor.py
                QMainWindowLayout::addItem: Please use the public QMainWindow API instead
                Traceback (most recent call last):
                File "C:/Projekte/Python/PQT-Texteditor/PQTTexteditor.py", line 195, in dateidruckentest1
                drucker.setPrinterName("Standarddrucker")
                TypeError: setPrinterName(self, str): first argument of unbound method must have type 'QPrinter'

                the exmple in the docs don't help, cause is painter and not the content of an editfield.

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 28 Jun 2020, 20:27 last edited by
                #8

                @PythonQTMarlem said in Print a text file with Python and Qt platform independent:

                the exmple in the docs don't help, cause is painter and not the content of an editfield.

                It was more too see it does work. (and not crash like your own code)

                You didnt answer
                printer = QPrinter()
                is the same as
                printer = QPrinter

                it will allocate a QPrinter object ?
                Only reason i could see it crash if its unallocated.

                1 Reply Last reply
                1
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 28 Jun 2020, 20:40 last edited by
                  #9

                  No it's not the same. Missing the parenthesis, no object is created. The left side becomes equal to the right side and thus there, you have a new name for the QPrinter class.

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

                  M 1 Reply Last reply 28 Jun 2020, 20:53
                  3
                  • S SGaist
                    28 Jun 2020, 20:40

                    No it's not the same. Missing the parenthesis, no object is created. The left side becomes equal to the right side and thus there, you have a new name for the QPrinter class.

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 28 Jun 2020, 20:53 last edited by
                    #10

                    @SGaist
                    ok so that explains the crashing.

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      PythonQTMarlem
                      wrote on 29 Jun 2020, 06:55 last edited by
                      #11

                      @SGaist
                      You are a hero.

                              def dateidruckentest1():
                                  self.statusBar().showMessage("Datei wird gedruckt")
                                  drucker = QPrinter()
                                  drucker.setPrinterName("Standarddrucker")
                                  druckerdialog = QPrintDialog(drucker,self)
                                  if druckerdialog.exec() == QDialog.Rejected: return
                                  self.texteditor.print(drucker)
                      

                      drucker = QPrinter(), so it works. Thank you!

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 29 Jun 2020, 18:56 last edited by
                        #12

                        Great !

                        Since you have it working now, please mark the thread as solved using the "Topic Tools" button or the three doted menu beside the answer you deem correct so that other forum users may know a solution has been found :-)

                        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

                        11/12

                        29 Jun 2020, 06:55

                        • Login

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