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 Updated to NodeBB v4.3 + New Features

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 2 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
    PythonQTMarlem
    wrote on last edited by
    #1

    Hello,

    I'm developing a text editor with Python and Qt that should be platform independent.
    In other words, it should be used on Windows, Ubuntu and MacOS
    can.
    I am looking for a way to print a text file.
    The printing of the text file is said to be under the above operating systems
    function.

    Question:
    How can I program with Python and Qt

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on 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
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on 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 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 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)

            mrjjM 1 Reply Last reply
            0
            • P PythonQTMarlem

              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)

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on 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 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.

                mrjjM 1 Reply Last reply
                0
                • P PythonQTMarlem

                  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.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 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
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 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

                    mrjjM 1 Reply Last reply
                    3
                    • SGaistS SGaist

                      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.

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

                      @SGaist
                      ok so that explains the crashing.

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        PythonQTMarlem
                        wrote on 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
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 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

                          • Login

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