Results 1 to 8 of 8

Thread: The content doesn't show to preview when using QPrintPreviewDialog

  1. #1
    Join Date
    Jul 2013
    Posts
    6
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Windows

    Default The content doesn't show to preview when using QPrintPreviewDialog

    I am using Qt version 4.8.4 on Window 7. I don’t have conditions to test this issue on another platform such as Mac, Linux… and I use the following code to preview content

    Qt Code:
    1. void DemoClass::on_btnOK_clicked()
    2. {
    3. QPrinter printer;
    4. printer.setResolution(QPrinter::HighResolution);
    5. printer.setPaperSize(QPrinter::A4);
    6. printer.setOrientation(QPrinter::Portrait);
    7. printer.setFullPage(true);
    8.  
    9. QPrintPreviewDialog *printPreview = new QPrintPreviewDialog(&printer);
    10. connect(printPreview, SIGNAL(paintRequested(QPrinter*)), this, SLOT(print(QPrinter*)));
    11.  
    12. printPreview->setWindowTitle("Preview Dialog");
    13. Qt::WindowFlags flags(Qt::WindowTitleHint);
    14. printPreview->setWindowFlags(flags);
    15. printPreview->exec();
    16. }
    17.  
    18. void DemoClass::print(QPrinter *printer)
    19. {
    20. QPainter painter(printer);
    21. painter.setRenderHints(QPainter::Antialiasing |
    22. QPainter::TextAntialiasing |
    23. QPainter::SmoothPixmapTransform, true);
    24.  
    25. painter.drawText(100, 100, "Hello World! 123");
    26. }
    To copy to clipboard, switch view to plain text mode 

    On pushing the OK button, this dialog appears:

    09-09-2013 18-03-43.png

    As you see, the page is blank. The page doesn’t contain any content. Then I click the page setup button on the preview dialog and this appears:

    09-09-2013 18-04-23.png

    …without changing anything, I click OK and then the preview becomes correct:

    09-09-2013 18-05-18.png

    I really don’t understand what the reason is. How can I show the content correctly without changing page setup?

    Do you have any solutions?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: The content doesn't show to preview when using QPrintPreviewDialog

    The reason is that you didn't set up the printer object before showing the preview. Once you click the page setup button, the printer object gets initialized with proper values and the preview shows correctly.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2013
    Posts
    6
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Windows

    Default Re: The content doesn't show to preview when using QPrintPreviewDialog

    Quote Originally Posted by wysota View Post
    The reason is that you didn't set up the printer object before showing the preview. Once you click the page setup button, the printer object gets initialized with proper values and the preview shows correctly.
    Thanks for your clearly response! I understood more about my issue. As a newbie in Qt, I don't have enough experience to solve this issue so could you give me sample code in this case or you show me the way to modify my code to the preview shows correctly?

    Thanks!

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: The content doesn't show to preview when using QPrintPreviewDialog

    The easiest way is to use QPrintDialog on a printer object before using it for the first time. Otherwise don't use a predefined printer object but let the preview dialog create one for you.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jul 2013
    Posts
    6
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Windows

    Default Re: The content doesn't show to preview when using QPrintPreviewDialog

    I also used QPrintDialog object that you had mentioned. The preview content appeared well for the first time. But I couldn't get actions such as Zoom in, Zoom out, Fit width, Fit page....like QPrintPreviewDialog object. I have not solved this issue yet. Maybe I need to research deeply about Qt. Anyway, thanks for your help!

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: The content doesn't show to preview when using QPrintPreviewDialog

    You should first setup the printer using the printer dialog and then call the preview dialog. It's not that you should use one instead of the other.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jul 2013
    Posts
    6
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Windows

    Default Re: The content doesn't show to preview when using QPrintPreviewDialog

    Quote Originally Posted by wysota View Post
    You should first setup the printer using the printer dialog and then call the preview dialog. It's not that you should use one instead of the other.
    Could you show me some sample code to solve this issue? It will help me save a lot of working in this case. I researched documents and technologies that followed your instruction but I still didn't solve this issue

  8. #8
    Join Date
    Jul 2013
    Posts
    6
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Windows

    Default Re: The content doesn't show to preview when using QPrintPreviewDialog

    Hi all,

    Finally I have solved my issue. I have just removed the following code

    Qt Code:
    1. printer.setResolution(QPrinter::HighResolution);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. qgraphicsview can't preview by QPrintPreviewDialog
    By qylibohao in forum Qt Programming
    Replies: 1
    Last Post: 4th November 2011, 07:46
  2. QGraphicsTextItem doesn't show up
    By chapu in forum Qt Programming
    Replies: 6
    Last Post: 12th July 2010, 00:48
  3. Replies: 1
    Last Post: 11th June 2010, 09:47
  4. Read SMS in-box content & show it in QListWidget?
    By damodharan in forum Qt Programming
    Replies: 3
    Last Post: 4th June 2010, 05:51
  5. Replies: 12
    Last Post: 15th February 2006, 10:46

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
  •  
Qt is a trademark of The Qt Company.