Results 1 to 5 of 5

Thread: QPrintPreviewDialog shows only blank grey area

  1. #1
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default QPrintPreviewDialog shows only blank grey area

    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:

    Qt Code:
    1. QPrinter printer(QPrinter::ScreenResolution);
    2. printer.setPaperSize(QPrinter::A4);
    3. printer.setFullPage(true);
    4. QPrintPreviewDialog preview(&printer);
    5. connect(&preview, SIGNAL(paintRequested(QPrinter *)), this, SLOT(print(QPrinter *)));
    6. preview.setMinimumHeight(500);
    7. preview.exec();
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. ...
    2. QPainter painter( printer );
    3. painter.setRenderHint(QPainter::Antialiasing);
    4.  
    5. int w = printer->pageRect().width();
    6. int h = printer->pageRect().height();
    7. QRect region( 0, 0, w, h );
    8. QRectF diagramSize(region.left(),region.top(),region.width(), region.height());
    9.  
    10. //scene is a QGraphicsScene
    11. scene->render(&painter,diagramSize,scene->itemsBoundingRect());
    To copy to clipboard, switch view to plain text mode 

    Any ideas as to why it might be happening?

    Thanks in advance

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,688
    Thanks
    13
    Thanked 1,598 Times in 1,526 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QPrintPreviewDialog shows only blank grey area

    Does the Windows 8 machine have a default printer set?

  3. #3
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QPrintPreviewDialog shows only blank grey area

    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!

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,688
    Thanks
    13
    Thanked 1,598 Times in 1,526 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QPrintPreviewDialog shows only blank grey area

    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.

  5. The following user says thank you to ChrisW67 for this useful post:

    regular (5th February 2014)

  6. #5
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QPrintPreviewDialog shows only blank grey area

    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:

    Qt Code:
    1. QPrinterInfo def = QPrinterInfo::defaultPrinter();
    2.  
    3. //if there is no default printer set the print preview won't show
    4. if(def.isNull() || def.printerName().isEmpty())
    5. {
    6. if(QPrinterInfo::availablePrinters().isEmpty())
    7. {
    8. QMessageBox::critical(this, tr("Print error"), tr("Cannot proceed because there are no available printers in your system."), QMessageBox::Ok);
    9. }
    10. else
    11. {
    12. def = QPrinterInfo::availablePrinters().first();
    13. }
    14. }
    15.  
    16. QPrinter printer(def, QPrinter::ScreenResolution);
    17. .....
    To copy to clipboard, switch view to plain text mode 
    Last edited by regular; 5th February 2014 at 11:36.

Similar Threads

  1. Pratical example of QPrintPreviewDialog
    By vcp in forum Qt Programming
    Replies: 5
    Last Post: 24th July 2012, 17:24
  2. QPrintPreviewDialog.
    By cydside in forum Qt Programming
    Replies: 2
    Last Post: 19th June 2009, 18:31
  3. QPrintPreviewDialog icons
    By quipu5 in forum Qt Programming
    Replies: 0
    Last Post: 19th January 2009, 14:36
  4. Qt 4.4.0 Problem with QPrintPreviewDialog
    By ad5xj in forum Qt Programming
    Replies: 2
    Last Post: 30th May 2008, 15:01
  5. issue about blank area appear
    By cherrydou in forum Qt Programming
    Replies: 5
    Last Post: 23rd May 2008, 08:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.