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