473,785 Members | 2,990 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3193
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(p rinter)
# do stuff to draw to painter
painter.end(pri nter)
# 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(p rinter)
# do stuff to draw to painter
painter.end(pri nter)
# do this between each page
printer.newPage ()

This is what I have so far:

app = QApplication(sy s.argv)
printer = QPrinter(QPrint er.PrinterResol ution)
if printer.setup() :
printer.setPage Size(printer.A4 )
painter = QPainter(printe r)
metrics = QPaintDeviceMet rics(painter.de vice())
marginHeight = 6
marginWidth = 8
body = QRect(marginWid th, marginHeight, metrics.widthMM () - 2 *
marginWidth, metrics.heightM M() - 2 * marginHeight)
painter.drawRec t(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.drawTe xt().
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(sy s.argv)
printer = QPrinter(QPrint er.PrinterResol ution)
if printer.setup() :
printer.setPage Size(printer.A4 )
painter = QPainter(printe r)
metrics = QPaintDeviceMet rics(painter.de vice())
marginHeight = 6
marginWidth = 8
body = QRect(marginWid th, marginHeight, metrics.widthMM () - 2 *
marginWidth, metrics.heightM M() - 2 * marginHeight)
painter.drawRec t(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(marginWid th, 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.drawTe xt().
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::fileP rint() 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************ *************** *@REMOVETHIScyb er.com.au>]
Mar 3 '06 #10

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

Similar topics

4
4858
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 selected. For example, if I were to have 5 pages with every second page printing, I would get the following results: Page 1 = Print OK Page 2 = Blank Page 3 = Print OK Page 4 = Blank
9
4118
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 PrintDialog control under a security setting with only SafePrinting allowed. I have attached a sample project that I am using to try to accomplish this. The print dialog appears, but when I press the Print button, I get an exception (at the end...
0
1649
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 appreciated. I have been using system.drawing.printing method but not much help since drawing.printing required printDocument() object which specially
5
10151
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 System.Drawing; using System.Drawing.Printing; using System.IO;
6
1903
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
3172
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 system call is too small". I found the following article, that I assume is similar to my problem, which is of little help: http://support.microsoft.com/default.aspx?scid=kb;en-us;822779 Any suggestions?
1
5721
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 of a Microsoft book, "Visual Basic,Net Step by Step" in Chapter 18. All but the bottom two subroutines will open a text file, and then allow me to use the above controls, example 1. The bottom two subroutines will print a graphic file, example...
8
5912
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 clients (Internet Explorer). My flash content was originally brought in via the “flash satay” method, but I have since used some server-side magic do deliver one <objecttag
2
1440
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 i haven't made any calls to close the database after printing. But after printing, none of the modules recognize the database file. I manually tried to close and open the database after printing but still it is not able to access the tables. I am...
18
11316
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 classes, for each of the checked rows in the GridView. This works fine in the Visual Studio 2005 development environment on localhost. But, when I move the page to the web server, I get the error "Settings to access printer...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10327
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10151
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.