PDA

View Full Version : Printing a pixmap full page: strange behavior on Windows



Pieter from Belgium
28th August 2007, 10:00
In my application I print a pixmap, full page. Under Linux this works fine, but on Windows, only part of the pixmap is printed, with a high zoom-in level.

In attachment, both pdfs.

The printing code fragment:



QPixmap pixmapToPrint = window->GetImageGraphicsView()->PaintToPixmap();
QPainter painter(GetPrinter());
QSize size = pixmapToPrint->size();
QRect viewport = painter.viewport();

qDebug() << "pixmap size: " << size; // output: QSize(382,393)
qDebug() << "painter viewport: " << viewport; // output: QRect(0,0 4958x7016)

size.scale(viewport.size(), Qt::KeepAspectRatio);
painter.setViewport(viewport.x(),viewport.y(),size .width(),size.height());
painter.setWindow(pixmapToPrint->rect());

qDebug() << "corrected painter viewport: " << painter.viewport(); // output: QRect(0,0 4958x5100)
qDebug() << "corrected painter window: " << painter.window(); // output: QRect(0,0 382x393)

painter.drawPixmap(0,0,*pixmapToPrint);

wysota
30th August 2007, 09:54
Does GetPrinter() include setting up the printer?

Pieter from Belgium
31st August 2007, 08:27
GetPrinter() returns an instance variable created when first needed using



mpPrinter = new QPrinter(QPrinter::PrinterResolution);


And configurated using a QPrintDialog:



QPrintDialog setupPrinterDialog(GetPrinter(),this);
if (setupPrinterDialog.exec())
{
<<here the code mentioned earlier>>
}

matthy
3rd September 2007, 07:20
I've got the same problem. Thought it relied on the printer. Hope now that anybody could help me.

wysota
3rd September 2007, 10:07
Are you sure this is correct?


painter.drawPixmap(0,0,*pixmapToPrint);

It shouldn't even compile...

Pieter from Belgium
3rd September 2007, 10:32
I'm sorry. I distilled the code fragment from the actual code, leaving out irrelevant elements, and changed a pointer to a pixmap into a pixmap, but not in a consequent manner.

Should have been:



painter.drawPixmap(0,0,pixmapToPrint);

matthy
4th September 2007, 06:24
My Code is similar to the code in the Image Viewer example:
QPrinter *printer = new QPrinter(QPrinter::HighResolution);
QPrintDialog dialog(printer, this);

printer->setOrientation(QPrinter::Landscape);

if (dialog.exec()) {
QPainter painter(printer);
QRect rect = painter.viewport();
QSize size = diagram->imageLabel->pixmap()->size();

size.scale(rect.size(), Qt::KeepAspectRatio);
painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
painter.setWindow(diagram->imageLabel->pixmap()->rect());
painter.drawPixmap(0, 0, *diagram->imageLabel->pixmap());
}

I have to say, that the Imageviewer example has the same problem after I installed QT4.3.0. With QT 4.1.1 the example and my code worked.:crying:

matthy
5th October 2007, 14:52
After installation of the new Qt version 4.3.2 it worked again.:):):D

Pieter from Belgium
3rd March 2008, 12:29
Some time ago I've found what the problem is:
I used the deprecated QPrinter::PrinterResolution printer mode, which has been documented to behave in a non-portable way. I've changed it to QPrinter::HighResolution and everything works fine.

Thanks for your ideas.