PDA

View Full Version : Printing and Paper Geometry



croftj
18th December 2007, 15:08
I'm trying to figure out the paper geometry when I print. I'm going for letter size paper, but if I print a box to the paper rectangle or to the page rectangle, the boxes are too big. When I print out the sizes from the QPrinter object, they're way out of wack too. This seems to be the case for two trypes of printers (HP PhotoSmart and HP OfficeJet) and any of the paper sizes.

Here's what I get when I query the QPrinter object for letter size paper with the OfficeJet:


logical DPI X: 98, logical DPI Y: 98
physical DPI X: 1200, physical DPI Y: 1200

device Width: 784
device Height: 1017

device Width in inches (width / 25.4): 30.8661
device height in inches (height / 25.4): 40.0394

device Width in mm: 784
device height in mm: 1017

Page Rect width: 784 height: 1017
top left: 25,12, top right: 808,12
bottom left: 25,1028, bottom right: 808,1028

Paper Rect width: 833 height: 1078
top left: 0,0, top right: 832,0
bottom left: 0,1077, bottom right: 832,1077

croftj
19th December 2007, 12:16
As a second note, the first example was using Linux. I have since run the same program on Windows and get similar results.

Any idea of what I may be doing wrong, or is Qt busted when it comes to printers?

jacek
19th December 2007, 15:56
#include <QApplication>
#include <QPrintDialog>
#include <QPrinter>

#include <QtDebug>

int main( int argc, char ** argv )
{
QApplication app( argc, argv );

QPrinter p;
QPrintDialog pd( &p );
pd.exec();

QRect pr = p.paperRect();
double r = p.resolution();

qDebug() << "QPrinter:";
qDebug() << "paper =" << pr;
qDebug() << "page =" << p.pageRect();
qDebug() << "res. =" << r;
qDebug() << "inSize =" << pr.width() / r << "x" << pr.height() / r;

qDebug() << "QPaintDevice:";
qDebug() << "pDpi =" << p.physicalDpiX() << "x" << p.physicalDpiY();
qDebug() << "lDpi =" << p.logicalDpiX() << "x" << p.logicalDpiY();
qDebug() << "size =" << p.width() << "x" << p.height();
qDebug() << "mmSize =" << p.widthMM() << "x" << p.heightMM();

return 0;
}


The results:

Xerox Phaser (A4):

$ ./printer
QPrinter:
paper = QRect(0,0 735x1041)
page = QRect(17,17 701x1006)
res. = 89
inSize = 8.25843 x 11.6966
QPaintDevice:
pDpi = 1200 x 1200
lDpi = 89 x 89
size = 701 x 1006
mmSize = 200 x 287


Xerox Phaser (Letter):

$ ./printer
QPrinter:
paper = QRect(0,0 757x979)
page = QRect(17,17 722x944)
res. = 89
inSize = 8.50562 x 11
QPaintDevice:
pDpi = 1200 x 1200
lDpi = 89 x 89
size = 722 x 944
mmSize = 206 x 269

Xerox WorkCentre (Letter):

$ ./printer
QPrinter:
paper = QRect(0,0 757x979)
page = QRect(15,15 728x951)
res. = 89
inSize = 8.50562 x 11
QPaintDevice:
pDpi = 1200 x 1200
lDpi = 89 x 89
size = 728 x 951
mmSize = 208 x 271

So everything seems to be OK (I have Qt 4.3.2).

Edit: The same on Qt 4.3.3.

croftj
19th December 2007, 18:12
Can you do me a favor and add the following lines of code and tell me if the rectangles show on the paper? For me they don't. I only see the left and bottom of the page rectangle and the left side of the paper rectangle.



QPainter painter(&p);
painter.drawRect(p.pageRect());
painter.setPen(QPen(QColor("blue")));
painter.drawRect(pr);


Thanks

jacek
19th December 2007, 19:19
I've managed to print the page rectange using following code:

painter.translate( -p.pageRect().topLeft() );
painter.drawRect( p.pageRect().adjusted( 1, 0, 0, -1 ) );

The first line is needed because:

The rectangle returned by pageRect() usually lies inside the rectangle returned by paperRect(). You do not need to take the positions and sizes of these area into account when using a QPainter with a QPrinter as the underlying paint device; the origin of the painter's coordinate system will coincide with the top-left corner of the page rectangle, and painting operations will be clipped to the bounds of the drawable part of the page.
(It's from Printing with Qt (http://doc.trolltech.com/4.3/printing.html).)

The call to adjusted() just corrects some rounding errors which aren't necessarily Qt's fault.

croftj
19th December 2007, 21:29
Thanks, the call to translate got me a lot closer. I still have to adjust it by 5 or more for the one printer, but like I said, it's a lot closer. Maybe this is why they have such large margins on the built in printing of the html documents.