Print a text file with Python and Qt platform independent
-
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 -
Hi,
Use the QtPrintSupport module.
-
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) -
@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?
-
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)
-
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)
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.
-
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.
-
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.
@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 = QPrinterit will allocate a QPrinter object ?
Only reason i could see it crash if its unallocated. -
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.
-
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.
-
@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!
-
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 :-)