Printing love

Published Wednesday September 19th, 2007
11 Comments on Printing love
Posted in KDE, Qt

Being able to do good printing one of those requests that come up very often. probably more often then “World peace” and “How to avoid hangovers after drinking too much”.
Like with world peace, the final answer is not going to be provided for quite some years yet. The topic is just too complex for one mind to answer (and we have some great minds in our communities). Now, we generally avoid the question altogether and just go and struggle though the headaces, which is mostly ok.

I’m often the perfectionist of the herd, aiming for the correct long-term solution. That helps with open source apps like KOffice2 which has seen a long development cycle which is really starting to pay off. My next bullet point is to give printing the love it deserves. And the printing code has been lonesome.

We are on a path of improvements, and I’m quite happy to see a constant set of improvements in Qt for printing. Which will mean a lot of applications out there will improve at the same time.
Qt4 has introduced a way to print to PDFs directly from a QPrinter instance, which is a great step forward since we used to rely on apps like ghostview to convert from postscript. This was known to have various problems. People are starting to use this more and more. But it has made another problem more clearly an itch to scratch.
The second longest request must have been to allow us to set custom page sizes. For example to print an envelope instead of a whole A4. So I decided to scratch that itch, and today I committed this change which you will find in the Qt4.4 snapshots tonight. There are 2 new methods on QPrinter. Namely; QPrinter::setPaperSize(Unit unit, QSizeF size) and QSizeF paperSize(Unit unit);
You can now do something like;

myPrinter.setPaperSize(QPrinter::Millimeter, QSizeF(162, 114));

Work is ongoing for printing, any feedback appreciated!

Do you like this? Share it
Share on LinkedInGoogle+Share on FacebookTweet about this on Twitter

Posted in KDE, Qt


s/piece/peace/g πŸ™‚ looks funny otherwise

Matt says:

Excellent, good to see custom page sizes finally implemented.

Chris says:

Well, I would like to have an answer for “How to avoid hangovers after drinking too much” as well ;-). Apart from that, nice new feature!

Christoph Bartoschek says:

Is there a reason that one has to use QPrinter for painting to PDF? For SVG there is the QSVGGenerator class. Why is no such class for PDF?

Will Stokes says:

It’s funny you wrote about this now, as I have been working on adding printing support for our app for the last week or so. Thus far I’m very happy with the quality printing Qt4.3 can already produce, but since you’re looking for feedback I figured I’d give you some. We’re not so interested in custom paper sizes (although your API change sounds perfect), but there are a couple things we would like to see fixed:

-on the Mac the page setup and print dialogs should be able to be sheets. Based on some new API functions in the upcoming Qt4.4 I get the impression this may be fixed in 4.4?

-it is somewhat painful to pull up the print dialog on OSX. Why is this?

-the page range controls in the print dialog are unusable on OSX

-if I start my app, pull up the page setup dialog, switch the orientation from the portrait default to landscape, print, quit, start my app again, pull up page setup I find the orientation has reverted to portrait. Shouldn’t the default orientation survive app instances? Aka if I print in Firefox using landscape should my Qt app default to landscape as well if I start it next and print there?

Rolf says:

Great news. Does the custom page size stuff also cover “endless”? I have a thermo-roll-printer in a project where QT is used and this an open problem.

Repre Hensor says:

Just for the record: envelope sizes are *not* custom page sizes, usually. Printers that can print envelopes do have these sizes defined in their PPDs, and their names are well-known (they’re just not named “A4” or “A3”.

So if Qt3 and Qt4 didn’t even support pre-defined envelope sizes, its feature set on that area is even more poor than I thought.

Real ‘custom’ sizes are those that do support *any* size I do define manually, for whatever reason I want to do that (I may want to print on a printer that can support those sizes — and believe me, there *are* printers out there that can do that, they may only have min/max limits for width and height which they can physically process, but everything in between is allowed…).

From what I guess from your code snippet, this is what Qt will support in future, which is great.

David Johnson says:

My big bugaboo with Qt printing, is the lack of decent WYSIWYG support. See the text editor demo for an example of this. This problem seems to be related to fonts and resolution. The only workaround I could come up with was to print to a QPixmap, and then draw the same pixmap to the preview widget and printer.

Will there be any improvements in this area?

Barry says:

One of the things I’d love to see for Qt Printing is more examples.

For instance, how do I print out a QTreeView with headers reproduced on each page?

Thanks for the updates =)

Thomas Witt says:

Nice, but what is the Unit argument for? In other words what does QPrinter know about unit conversions that the client doesn’t? What did I miss?

Thomas Zander says:

thanks for the feedback πŸ™‚

Your application will certainly know what length of paper you would use for the data you want to print, so ‘endless’ is not a state that is required.
That said; you can certainly make your page 12meters tall without problems on the QPrinter side.

pre-defined envelope sizes certainly always worked. The issue was with printing to a printer (like PDF) that doesn’t have these sizes pre-defined. Now the application can provide it.

Take a look at QTextDocument::setUseDesignMetrics(bool)

The unit argument is an enum; with things like Point, Inch as well as Millimeter. So you can also do a myPrinter.setPaperSize(QPrinter::Inch, QSizeF(6.1, 4.23));
This makes it so the client doesn’t need to convert.

Commenting closed.

Get started today with Qt Download now