Creating print option for simple calculation tool
-
wrote on 12 Oct 2019, 19:34 last edited by CivilEngineer
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)
Any ideas how to start?
-
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 ?
-
wrote on 13 Oct 2019, 20:08 last edited by
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.
-
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.
-
wrote on 17 Oct 2019, 20:22 last edited by
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.
-
Despite the Qt version, most of the book is still relevant for Qt 5.
-
wrote on 22 Oct 2019, 20:25 last edited by CivilEngineer
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?
-
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 ? -
wrote on 23 Oct 2019, 18:52 last edited by
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..
-
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..
wrote on 23 Oct 2019, 19:12 last edited by JonB@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 viasetDocumentMargin()
. The docs sate "The default is 4.", so you might save a bit of what you are looking for by reducing to 0? -
wrote on 23 Oct 2019, 19:24 last edited by CivilEngineer
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
-
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
wrote on 23 Oct 2019, 19:57 last edited by JonB@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/12