473,395 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Printing a file

Hello!

I am currently working on an application where the user is able to
create new worksheets and to delete existing ones. All of these
worksheets have the same structure (--> template?), only some values
should be changed. A minimal example would be something like this:

Name: ...........
Role: ............
Address: ............

The values are stored in a SQLite database. Now I would like to offer
the possibility to print out a single record on a DinA4 paper. In order
to do this, the dots (...) above of course have to be replaced by the
current record's values and the different parts have to fit on one page.

Unfortunately I don't know how to realize this, since also some images
and different boxes should be printed out. As the whole application is
based on QT, QPrinter might be used, but I couldn't find any examples
how to use it.

What do you suggest? Which format should the template have? (XML, etc.?)

Any hints appreciated!

Cheers,
Fabian Steiner
Feb 28 '06 #1
10 3145
Fabian Steiner wrote:
Unfortunately I don't know how to realize this, since also some images
and different boxes should be printed out. As the whole application is
based on QT, QPrinter might be used, but I couldn't find any examples
how to use it.


QPrinter is easy to use. You just draw to the page the same way as you talk
to the screen with a QPainter.

prnt = qt.QPrinter()
# you can also vary options like colour, doc name, dpi here

# display dialog box to user (you can actually leave this out)
if prnt.setup():
painter = qt.QPainter()
painter.begin(printer)
# do stuff to draw to painter
painter.end(printer)
# do this between each page
printer.newPage()

# ... more pages can be printed to a painter

It's very easy to do. If you want to handle multiple pages and so on,
there's a bit of work to do to interface to the dialog to get the
user-selected page range, etc.

Jeremy

--
Jeremy Sanders
http://www.jeremysanders.net/
Feb 28 '06 #2
Jeremy Sanders wrote:
Fabian Steiner wrote:
Unfortunately I don't know how to realize this, since also some images
and different boxes should be printed out. As the whole application is
based on QT, QPrinter might be used, but I couldn't find any examples
how to use it.

[...]
It's very easy to do. If you want to handle multiple pages and so on,
there's a bit of work to do to interface to the dialog to get the
user-selected page range, etc.


That's where QPrintDialog comes in:

http://doc.trolltech.com/4.1/qprintdialog.html

It's also secretly available in Qt 3 via the QPrinter.setup() method:

printer = QPrinter()
printer.setup()
# Now, paint onto the printer as usual.

David

Feb 28 '06 #3
Hi!

Thank you so far, but now I got stuck again :-/
Jeremy Sanders wrote:
QPrinter is easy to use. You just draw to the page the same way as you talk
to the screen with a QPainter.

prnt = qt.QPrinter()
# you can also vary options like colour, doc name, dpi here

# display dialog box to user (you can actually leave this out)
if prnt.setup():
painter = qt.QPainter()
painter.begin(printer)
# do stuff to draw to painter
painter.end(printer)
# do this between each page
printer.newPage()

This is what I have so far:

app = QApplication(sys.argv)
printer = QPrinter(QPrinter.PrinterResolution)
if printer.setup():
printer.setPageSize(printer.A4)
painter = QPainter(printer)
metrics = QPaintDeviceMetrics(painter.device())
marginHeight = 6
marginWidth = 8
body = QRect(marginWidth, marginHeight, metrics.widthMM() - 2 *
marginWidth, metrics.heightMM() - 2 * marginHeight)
painter.drawRect(body)
painter.end()

Doing so I hoped to get a rectangle which is as big as an A4 paper (with
a small border), but unfortunately it is much smaller. Moreover, I ask
myself whether it is necessary that in order to write text on the paper,
I always have to pass the proper x, y values to QPainter.drawText().
Isn't there any other possibility? How do I get these values?
Thanks in advance,
Fabian Steiner
Mar 1 '06 #4
Fabian Steiner wrote:
This is what I have so far:

app = QApplication(sys.argv)
printer = QPrinter(QPrinter.PrinterResolution)
if printer.setup():
printer.setPageSize(printer.A4)
painter = QPainter(printer)
metrics = QPaintDeviceMetrics(painter.device())
marginHeight = 6
marginWidth = 8
body = QRect(marginWidth, marginHeight, metrics.widthMM() - 2 *
marginWidth, metrics.heightMM() - 2 * marginHeight)
painter.drawRect(body)
painter.end()

Doing so I hoped to get a rectangle which is as big as an A4 paper (with
a small border), but unfortunately it is much smaller.
Surely you meant to use

body = QRect(marginWidth, marginHeight,
metrics.width() - 2 * marginWidth,
metrics.height() - 2 * marginHeight)
Moreover, I ask myself whether it is necessary that in order to write
text on the paper, I always have to pass the proper x, y values to
QPainter.drawText().
Isn't there any other possibility? How do I get these values?


That depends on what kind of text you're drawing (paragraphs of text
vs. simple labels). See the application.py example in the examples3
directory of the PyQt3 distribution for code that implements a simple
text editor with support for printing. Information about text and font
metrics can be found with the QFontMetrics class:

http://doc.trolltech.com/3.3/qfontmetrics.html

PyQt4 supports Qt 4's new rich text facilities, so it's easier to
format text for printing than it is in Qt 3. A more advanced rich text
editor is only available in the C++ Qt 4 demos, but there are other
examples bundled with PyQt4 that show how to print "simple" documents:

http://www.riverbankcomputing.co.uk/

Finally, it's worth pointing out that there's a higher concentration of
people with experience in these matters reading the PyQt/PyKDE mailing
list:

http://mats.imk.fraunhofer.de/mailman/listinfo/pykde

Good luck with your printing,

David

Mar 1 '06 #5
David Boddie wrote:
That's where QPrintDialog comes in:

http://doc.trolltech.com/4.1/qprintdialog.html

It's also secretly available in Qt 3 via the QPrinter.setup() method:

printer = QPrinter()
printer.setup()
# Now, paint onto the printer as usual.


No - that was in my example. The work I was refering to was taking the
user's input to the dialog and writing the pages to the device in the right
order (I don't think this is done automatically).

Jeremy

--
Jeremy Sanders
http://www.jeremysanders.net/
Mar 2 '06 #6
Sorry about that. I must have just skipped over the setup() call in
your code. If you're creating highly customized content then I think
you'll always need to think about getting the pages to the printer in
the right order.

For rich text documents, there's code that does this in the Qt 3 text
drawing demo (see the filePrint() method in the
examples/demo/textdrawing/textedit.cpp file).

In Qt 4, the demos/textedit demo does this with a lot less code.

Or are you think of something else?

David

Mar 2 '06 #7
David Boddie wrote:
Sorry about that. I must have just skipped over the setup() call in
your code. If you're creating highly customized content then I think
you'll always need to think about getting the pages to the printer in
the right order.

For rich text documents, there's code that does this in the Qt 3 text
drawing demo (see the filePrint() method in the
examples/demo/textdrawing/textedit.cpp file).

In Qt 4, the demos/textedit demo does this with a lot less code.

Or are you think of something else?


Thank you very much for this hint! Thanks to this example I was able to
print out my first pages :)

But some questions still remain. At the moment I am using
QSimpleRichtext and a personal HTML-File. I had a look at the
example.html of textedit.cpp (/usr/share/doc/qt-4.1.1/demos/textedit)
and found out that it contains quite a lot of proprietary HTML elements,
attributes and CSS style definitions. So far I didn't even know that
QSimpleRichText even supports CSS since I couldn't find anything related
to this point in the official docs (--> e.g. QStylesheet).

Is there any tool out there with which I can write those special HTML
files? I am quite familiar with HTML and CSS but I don't want to waste
my time with that.

Regards,
Fabian Steiner
Mar 3 '06 #8
Fabian Steiner wrote:
David Boddie wrote:
In Qt 4, the demos/textedit demo does this with a lot less code.

Or are you think of something else?


Thank you very much for this hint! Thanks to this example I was able to
print out my first pages :)


That's good to hear. :-)
But some questions still remain. At the moment I am using
QSimpleRichtext and a personal HTML-File. I had a look at the
example.html of textedit.cpp (/usr/share/doc/qt-4.1.1/demos/textedit)
and found out that it contains quite a lot of proprietary HTML elements,
attributes and CSS style definitions. So far I didn't even know that
QSimpleRichText even supports CSS since I couldn't find anything related
to this point in the official docs (--> e.g. QStylesheet).
I think I may have confused you by mentioning Qt 4. Since you are using
QSimpleRichText, you must be using Qt 3, so you should probably ignore
what I said about the /usr/share/doc/qt-4.1.1/demos/textedit demo. :-/
Is there any tool out there with which I can write those special HTML
files? I am quite familiar with HTML and CSS but I don't want to waste
my time with that.


You don't need to include all those style attributes in the HTML.
Anyway, that's a different version of Qt to the one you are using, so
you can safely ignore it. You should probably look at the text drawing
part of the demo included in Qt 3 (examples/demo/textdrawing) and see
how printing is done for the rich text editor there (in the
TextEdit::filePrint() function). Translating it to Python _shouldn't_
be a problem.

I hope I didn't confuse you too much by talking about two different
versions of Qt at the same time.

Let us know how it goes.

David

Mar 3 '06 #9
Fabian Steiner <li***@fabis-site.net> wrote:
I am currently working on an application where the user is able to
create new worksheets and to delete existing ones. All of these
worksheets have the same structure (--> template?), only some values
should be changed. A minimal example would be something like this:

Name: ...........
Role: ............
Address: ............

The values are stored in a SQLite database. Now I would like to offer
the possibility to print out a single record on a DinA4 paper. In order
to do this, the dots (...) above of course have to be replaced by the
current record's values and the different parts have to fit on one page.

Unfortunately I don't know how to realize this, since also some images
and different boxes should be printed out. As the whole application is
based on QT, QPrinter might be used, but I couldn't find any examples
how to use it.

What do you suggest? Which format should the template have? (XML, etc.?)


I would either use something like ReportLab to create PDF or some
external type-setting language like LaTeX, *roff or docbook if they are
availabled.
Florian
--
No no no! In maths things are usually named after Euler, or the first
person to discover them after Euler.
[Steven D'Aprano in <pa****************************@REMOVETHIScyber.co m.au>]
Mar 3 '06 #10
Fabian Steiner <li***@fabis-site.net> wrote:
I am currently working on an application where the user is able to
create new worksheets and to delete existing ones. All of these
worksheets have the same structure (--> template?), only some values
should be changed. A minimal example would be something like this:

Name: ...........
Role: ............
Address: ............

The values are stored in a SQLite database. Now I would like to offer
the possibility to print out a single record on a DinA4 paper. In order
to do this, the dots (...) above of course have to be replaced by the
current record's values and the different parts have to fit on one page.

Unfortunately I don't know how to realize this, since also some images
and different boxes should be printed out. As the whole application is
based on QT, QPrinter might be used, but I couldn't find any examples
how to use it.

What do you suggest? Which format should the template have? (XML, etc.?)


I would either use something like ReportLab to create PDF or some
external type-setting language like LaTeX, *roff or docbook if they are
availabled.


Florian
--
No no no! In maths things are usually named after Euler, or the first
person to discover them after Euler.
[Steven D'Aprano in <pa****************************@REMOVETHIScyber.co m.au>]
Mar 3 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jody Gelowitz | last post by:
I am having a problem with printing selected pages. Actually, the problem isn't with printing selected pages as it is more to do with having blank pages print for those pages that have not been...
9
by: Jody Gelowitz | last post by:
I am trying to find the definition of "Safe Printing" and cannot find out exactly what this entitles. The reason is that I am trying to print contents from a single textbox to no avail using the...
0
by: DotNetDummy | last post by:
Hi all, I am trying to set the printing setting e.g duplex mode etc. on a default printer when I.E object started printing a particular html doc. Here's the partial code, any help would be...
5
by: Patrick De Ridder | last post by:
How can I turn what I want to print 90 degrees using the logic below? Please tell me the code with which to make the modification. Many thanks, Patrick. using System.ComponentModel; using...
6
by: Bruno Hardmeier | last post by:
Hi How can I redirect a print job to a file? There is a checkbox in the print dialog, but I would like to do it in code indicating a filename. Thanks Bruno Hardmeier
4
by: Rob T | last post by:
I have a small VB program that has a printing module...very simple....and works great. However, If I try to print to a generic printer, I get the following error: "The data area passed to a...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
8
by: Neo Geshel | last post by:
Greetings. BACKGROUND: My sites are pure XHTML 1.1 with CSS 2.1 for markup. My pages are delivered as application/xhtml+xml for all non-MS web clients, and as text/xml for all MS web...
2
by: coldblood22 | last post by:
Well in my Application i am using the java Printable interface to print the GUI. The printing is done fine but once done with the printing my program breaks off with the database file without. Well...
18
by: Brett | last post by:
I have an ASP.NET page that displays work orders in a GridView. In that GridView is a checkbox column. When the user clicks a "Print" button, I create a report, using the .NET Framework printing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.