Qt-interest Archive, August 2007
Re: QPainter::setWindow() Problem with QPrinter
Message 1 in thread
Niklas Hofmann wrote:
> The answer:
> http://trolltech.com/developer/task-tracker/index_html?id=173213&method=entry
>
>
> Greetings
>
> Niklas
>
> Niklas Hofmann schrieb:
>> Hi!
>>
>> I have a problem with QPainter::setWindow() on QPrinter.
>>
>> When I create a PDF-Document the following way, it works perfectly:
>>
>> QPrinter printer(QPrinter::HighResolution);
>> printer.setFullPage(true);
>> printer.setPageSize(QPrinter::A4);
>> printer.setOrientation(QPrinter::Portrait);
>> printer.setOutputFileName(pdffilename);
>> printer.setOutputFormat(QPrinter::PdfFormat);
>>
>> QPainter painter(&printer);
>> painter.setWindow(0, 0, 2100, 2970);
>>
>> // ... some drawing operations ...
>>
>> I can draw with 0.1 mm units and cover the full page perfectly.
>>
>> If I want to print directly to a real printing device the following
>> way, the output is very small placed in the left-top corner of the page.
>>
>> QPrinter printer(QPrinter::HighResolution);
Set this to QPrinter printer(QPrinter::ScreenResolution);
>> printer.setFullPage(true);
>> printer.setPageSize(QPrinter::A4);
>> printer.setOrientation(QPrinter::Portrait);
>>
>> QPrintDialog printDialog(&printer);
>> if(printDialog.exec() != QDialog::Accepted)
>> return false;
>>
>> QPainter painter(&printer);
>> painter.setWindow(0, 0, 2100, 2970);
>>
>> // ... some drawing operations ...
>>
>>
>> Is it a QT bug, or is it just me.. :-)?
>>
>> Does somebody know a solution?
>>
>> Thank you!! :-)
>>
>> Greetings
>>
>> Niklas
--
[ signature omitted ]
Message 2 in thread
Hi!
>>> QPrinter printer(QPrinter::HighResolution);
>
> Set this to QPrinter printer(QPrinter::ScreenResolution);
No, its not a solution :-) It is a bug in the QT library. This is a
workaround:
> Hi Niklas,
>
> The answer appears to be: 0.6505...
>
> #include <QtGui>
>
> void print(QPaintDevice *target, double workaround = 1.0)
> {
> QPainter painter(target);
>
> painter.setWindow(0, 0, 2100, 2970);
> painter.setViewport(0, 0, target->width() * workaround,
target->height() * workaround);
>
> painter.drawRect(0, 0, 50, 50);
> painter.drawRect(2100-50, 2970-50, 50, 50);
> }
>
> int main(int argc, char *argv[])
> {
> QApplication app(argc, argv);
>
> QPrinter softPrinter(QPrinter::HighResolution);
> softPrinter.setFullPage(true);
> softPrinter.setPageSize(QPrinter::A4);
> softPrinter.setOrientation(QPrinter::Portrait);
> softPrinter.setOutputFileName("softPrinter.pdf");
> softPrinter.setOutputFormat(QPrinter::PdfFormat);
>
> print(&softPrinter);
>
> QPrinter realPrinter(QPrinter::HighResolution);
> realPrinter.setFullPage(true);
> realPrinter.setPageSize(QPrinter::A4);
> realPrinter.setOrientation(QPrinter::Portrait);
>
> QPrintDialog dialog(&realPrinter);
> dialog.exec();
> print(&realPrinter, 0.6505);
>
> return 0;
> }
Greetings
Niklas
--
[ signature omitted ]