Unexpected results with KPrinter and QPaintDeviceMetrics

Michael Goffioul sw286000 at swing.be
Fri Dec 27 18:51:35 GMT 2002

Due to internal reasons, a synchronization step needs to be done between the
options set in the print dialog, and the internal PS generator (QPrinter).
corresponds to KPrinter::preparePrinting (which is protected). However, this
function is automatically called when calling QPainter::begin() on the
object. There's 2 solutions:

1) the normal one:

KPrinter printer;
if (printer.setup())
   QPainter painter(&printer);
   QPaintDeviceMetrics metrics(&printer);
   // you should now have the correct values

2) solution 1) is only valid if you intend to print something, which is
usually the
case. Indeed, QPainter::end() will send the PS to the selected printer. If
want to avoid that, the workaround is to inherits from KPrinter like this:

class MyPrinter : public KPrinter
   // constructors, and so on
   void doPrepare() { preparePrinting(); }
   void doFinish() { finishPrinting(); }

and then:

MyPrinter printer;
QPaintDeviceMetrics metrics(&printer);
// you should have the correct values

I plan to add such utility functions to the KPrinter API and add a mechanism
to avoid such problems, for 3.2.


More information about the kde-core-devel mailing list