PDA

View Full Version : QPrintPreviewDialog shows only blank grey area



regular
4th February 2014, 21:13
Hi,

I have a problem with QPrintPreviewDialog in Windows 8.1 environment. When I open it, it just has a grey background, without any pages on it. If I actually send a print job from the dialog though, it prints properly. Also, the problem only seems to appear on Windows 8 - it shows up fine in Windows 7. Here's the code that opens the preview dialog:


QPrinter printer(QPrinter::ScreenResolution);
printer.setPaperSize(QPrinter::A4);
printer.setFullPage(true);
QPrintPreviewDialog preview(&printer);
connect(&preview, SIGNAL(paintRequested(QPrinter *)), this, SLOT(print(QPrinter *)));
preview.setMinimumHeight(500);
preview.exec();

And the print() slot contains this (among other layouting code):


...
QPainter painter( printer );
painter.setRenderHint(QPainter::Antialiasing);

int w = printer->pageRect().width();
int h = printer->pageRect().height();
QRect region( 0, 0, w, h );
QRectF diagramSize(region.left(),region.top(),region.widt h(), region.height());

//scene is a QGraphicsScene
scene->render(&painter,diagramSize,scene->itemsBoundingRect());

Any ideas as to why it might be happening?

Thanks in advance

ChrisW67
4th February 2014, 21:47
Does the Windows 8 machine have a default printer set?

regular
4th February 2014, 22:28
No, it didn't have one set as default. I just set the XPS document printer as default and that fixed the problem. Thanks a lot!

But is that normal behavior? If a client machine doesn't have a default printer set as mine didn't, that wouldn't be desired behavior I think? Is there a way to get around it in the code or is that what it should do?

Thanks again!

ChrisW67
5th February 2014, 07:31
The problem is that the page size of a non-existent printer is zero, resulting in no visible output. If you had a printer, any printer, then the preview will always show a blank first page even if nothing is painted.

QPrinterInfo::defaultPrinter() returns a QPrinterInfo object where QPrinterInfo::isNull() is true if there is no default printer. In that case you could use QPrintDialog or otherwise select a printer to use and then create your QPrinter object.

regular
5th February 2014, 09:54
Sounds good, thanks again!

EDIT: A quick note for anyone else having the problem, ussing just QPrinterInfo::isNull() didn't help because that only seems to return true if there are no printers at all on the system (and still returns false if there is no default printer set) so I had to check the printer's name too:



QPrinterInfo def = QPrinterInfo::defaultPrinter();

//if there is no default printer set the print preview won't show
if(def.isNull() || def.printerName().isEmpty())
{
if(QPrinterInfo::availablePrinters().isEmpty())
{
QMessageBox::critical(this, tr("Print error"), tr("Cannot proceed because there are no available printers in your system."), QMessageBox::Ok);
}
else
{
def = QPrinterInfo::availablePrinters().first();
}
}

QPrinter printer(def, QPrinter::ScreenResolution);
.....