Hello,
I'm trying to extend a program with some printing functionality and
things aren't working out as expected. I'm hoping someone here can shed
some light on the issues I'm experiencing...

I stripped down my problems to a minimal working test case, which source
I have attached. The main 2 problems I'm facing here are:

1) I tried to print a border around the printable pageRect(). The debug
   shows me that paperRect() is bigger as the pageRect() as was to be
   expected from the documentation, but when I paint both pageRect and
   paperRect I still see them both?  And they extend to the left and
   bottom outside of the page ? I expected the pageRect to be clipped
   and the paperRect to be completely centered on the page...

2) I alway see the last page I try to print to the PDF. If I send the
   output to a real printer a get two pages printed out, but in the pdf
   only one shows up ?

Can somebody enlighten me to what is going on and correct me, since I have
the funny feeling that I'm not entirely grasping this matter.

Thanks for any suggestions!

David


#include <QtCore>
#include <QPainter>
#include <QPrinter>
#include <QMessageBox>
#include <QDebug>
#include <QtGui/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QString filename="tmp.pdf";
    QPrinter* printer = new QPrinter();
    printer->setOutputFormat(QPrinter::PdfFormat);
    printer->setOutputFileName(filename);
    printer->setOrientation(QPrinter::Portrait);
    printer->setPaperSize(QPrinter::A4);
    printer->setResolution(300);

    qDebug() << "printer status" <<printer->printerState();
    qDebug() << "printer->paperSize()" << printer->paperSize();
    qDebug() << "printer->paperRect()" << printer->paperRect();
    qDebug() << "printer->pageRect()" << printer->pageRect();
    qDebug() << "printer->resolution()" << printer->resolution();
    qreal left, top, right, bottom;

    printer->getPageMargins( &left, &top, &right, &bottom, QPrinter::DevicePixel );
    qDebug() << QString("getPageMargins( %1, %2, %3, %4, QPrinter::DevicePixel )").arg(left).arg(top).arg(right).arg(bottom);

    QPainter* painter = new QPainter(printer);
    painter->setPen(Qt::red);
    painter->drawText(100,100,"hello");
    delete painter;

    printer->newPage();

    painter = new QPainter(printer);
    QPen pen(Qt::red);
    pen.setWidth(3);
    painter->setPen(pen);
    painter->drawRect(printer->paperRect());
    pen.setWidth(1);
    pen.setColor(Qt::blue);
    painter->setPen(pen);
    painter->drawRect(printer->pageRect());
    painter->setPen(Qt::black);
    QRectF b(printer->pageRect());
    painter->drawText(b,"hello world");
    delete painter;

    delete printer;
    return 0;
}

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

Reply via email to