Results 1 to 6 of 6

Thread: QPrintPreviewDialog doesn't update preview during execution

  1. #1
    Join Date
    Jul 2015
    Posts
    87
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default QPrintPreviewDialog doesn't update preview during execution

    Hi,

    i wrote a class PrintPreview to show a preview of of our data.
    It could be a Plot or a Report (with more data and the plot).

    I modified the QPrintPreviewDialog and add two QCheckBoxes

    PreviewPlotReport.png

    The QCheckboxes are autoexclusive and with

    Qt Code:
    1. checkPlot.setChecked( false or true );
    2. checkReport.setChecked( true or false );
    To copy to clipboard, switch view to plain text mode 
    i can set the initial render style and everthing works fine.
    I can see on first exec() either the "Plot" or the "Report".


    When i switch between "Plot" and "Report" style using the
    QCheckboxes inside the QToolBar during the QPrintPreviewDialog
    is executed, it doesn't work.

    If i click on "Report" my debug shows:
    Qt Code:
    1. void PrintPreview::render(QPrinter*)
    2. void PrintPreview::renderReport(QPrinter*)
    To copy to clipboard, switch view to plain text mode 

    If i click on "Plot" my debug shows:
    Qt Code:
    1. void PrintPreview::render(QPrinter*)
    2. void PrintPreview::renderPlot(QPrinter*)
    To copy to clipboard, switch view to plain text mode 

    Both rendering slots are called, but the preview itself is not updated.

    Qt Code:
    1. // connection paintReqnested
    2. connect( pPreview, SIGNAL(paintRequested(QPrinter*)), this, SLOT(render(QPrinter*)));
    3.  
    4. void PrintPreview::render(QPrinter *printer)
    5. {
    6. qDebug() << Q_FUNC_INFO;
    7. if( checkPlot.isChecked() )
    8. renderPlot( printer );
    9.  
    10. if( checkReport.isChecked() )
    11. renderReport( printer );
    12. }
    To copy to clipboard, switch view to plain text mode 

    Can anybody help why QPrintPreviewDialog doesn't update preview?
    Thx

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QPrintPreviewDialog doesn't update preview during execution

    If you are invoking your print preview dialog as modal (eg. using QDialog::exec()), then it is running its own local event loop. The main app event loop processing is suspended while a modal dialog is active, so any signals sent via the main event loop will not be received until after the dialog is closed and control returns to the main event loop.

  3. #3
    Join Date
    Jul 2015
    Posts
    87
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QPrintPreviewDialog doesn't update preview during execution

    The interessting thing is:

    1.) I have a rendered "Plot", i can see the "Plot" in preview window
    2.) Now i click on the "Report", the "Report" will be rendered but not shown
    3). The orientation is landscape, when i now switch to portrait or click again on landscape i can see the updated preview, my "Report"

    Could this be a workaround to trigger the action?

    Qt Code:
    1. QList<QToolBar *> qlPreviewToolbars = pPreview->findChildren<QToolBar *>();
    2. QToolBar *previewToolbar = qlPreviewToolbars.at(0);
    3. previewActions = previewToolbar->actions();
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void PrintPreview::render(QPrinter *printer)
    2. {
    3. qDebug() << Q_FUNC_INFO;
    4. if( checkPlot.isChecked() )
    5. renderPlot( printer );
    6.  
    7. if( checkReport.isChecked() )
    8. renderReport( printer );
    9.  
    10. // action for
    11. previewActions.at(7)->trigger(); // now my app crashes at this point?
    12. }
    To copy to clipboard, switch view to plain text mode 


    Added after 23 minutes:


    I found workaround for this:

    Qt Code:
    1. // constructor
    2. connect( pPreview, SIGNAL(paintRequested(QPrinter*)), this, SLOT(render(QPrinter*)));
    3. connect( &checkPlot, SIGNAL(released()), this, SLOT(render()));
    4. connect( &checkReport, SIGNAL(released()), this, SLOT(render()));
    5. d_data->styleChanged = false;
    6.  
    7.  
    8. void PrintPreview::render()
    9. {
    10. d_data->styleChanged = true;
    11. emit pPreview->paintRequested( printer );
    12. }
    13.  
    14. void PrintPreview::render(QPrinter *printer)
    15. {
    16. if(!d_data->styleChanged)
    17. {
    18. if( checkPlot.isChecked() )
    19. renderPlot( printer );
    20.  
    21. if( checkReport.isChecked() )
    22. renderReport( printer );
    23. }
    24. else
    25. {
    26. d_data->styleChanged = false;
    27.  
    28. // Workaround
    29. // when orientation is landscape, trigger again landscape (this updates preview)
    30. // when orientation is portait, trigger again portrait (this updates preview)
    31. if(printer->orientation() == QPrinter::Landscape )
    32. previewActions.at(8)->trigger(); // if landscape, trigger action landscape again and this emits later a paintRequest
    33. else
    34. previewActions.at(7)->trigger(); // if portrait, trigger action portrait again and this emits later a paintRequest
    35. }
    36. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by HappyCoder; 23rd September 2015 at 07:24.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QPrintPreviewDialog doesn't update preview during execution

    Quote Originally Posted by d_stranz View Post
    If you are invoking your print preview dialog as modal (eg. using QDialog::exec()), then it is running its own local event loop. The main app event loop processing is suspended while a modal dialog is active, so any signals sent via the main event loop will not be received until after the dialog is closed and control returns to the main event loop.
    I don't think that is true, that would make most applications unusable.

    The nested event loop used by QDialog::exec() just filters certain user event intended for the dialog's parent or the whole application (depending on the used modality setting).

    Easily testable by having a timer update a label in the dialog's parent.
    Not only will the timer event be processed, the label will also receive its paint event.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QPrintPreviewDialog doesn't update preview during execution

    I don't think that is true, that would make most applications unusable.

    The nested event loop used by QDialog::exec() just filters certain user event intended for the dialog's parent or the whole application (depending on the used modality setting).

    Easily testable by having a timer update a label in the dialog's parent.
    Not only will the timer event be processed, the label will also receive its paint event.
    Hmm, I suppose that's correct. I was thinking mostly in terms of user interaction with the parent and not "background" event processing. I guess I have never programmed a case such as the one you describe.

    That implies that a modal QDialog could send signals that would update a progress bar in the parent widget, right?

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QPrintPreviewDialog doesn't update preview during execution

    Quote Originally Posted by d_stranz View Post
    That implies that a modal QDialog could send signals that would update a progress bar in the parent widget, right?
    Yes.
    The signal doesn't used events in that case, but the paint event caused by the value change will be processed normally.

    Cheers,
    _

Similar Threads

  1. QGraphicsItem doesn't update
    By Vyivrain in forum Qt Programming
    Replies: 0
    Last Post: 6th April 2014, 19:51
  2. Replies: 7
    Last Post: 12th September 2013, 08:25
  3. qgraphicsview can't preview by QPrintPreviewDialog
    By qylibohao in forum Qt Programming
    Replies: 1
    Last Post: 4th November 2011, 07:46
  4. childrenBoundingRect doesn't update
    By sanku in forum Qt Programming
    Replies: 0
    Last Post: 24th March 2011, 10:29
  5. QGLWidget doesn't update
    By qtbnl in forum Qt Programming
    Replies: 12
    Last Post: 14th February 2011, 17:41

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.