Printing a file

F

Fabian Steiner

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
 
J

Jeremy Sanders

Fabian said:
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
 
D

David Boddie

Jeremy said:
Fabian said:
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
 
F

Fabian Steiner

Hi!

Thank you so far, but now I got stuck again :-/


Jeremy said:
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
 
D

David Boddie

Fabian said:
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
 
J

Jeremy Sanders

David said:
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
 
D

David Boddie

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
 
F

Fabian Steiner

David said:
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
 
F

Florian Diesch

Fabian Steiner said:
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
 
D

David Boddie

Fabian said:
David Boddie wrote:

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
 
F

Florian Diesch

Fabian Steiner said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,255
Latest member
TopCryptoTwitterChannels

Latest Threads

Top