Results 1 to 4 of 4

Thread: QDialog does not appear until calculations are finished

  1. #1
    Join Date
    Mar 2017
    Location
    Erlangen, Germany
    Posts
    2
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default QDialog does not appear until calculations are finished

    Hi, I have a problem with the GUI for a laser beam propagation simulation I am currently writing with C++ and QT. The simulation itself works, but I implemented a dialog which should actually update a plot, using QCustomPlot and a progressBar. Unfortunately, the QDialog only appears after all the calculations are done, showing the plot of the final result and a progress bar at 100%.

    Is there any way to make sure my dialog is actually visible during the calculations?

    Here is how I create the dialog, after the preceding information has been processed:
    Qt Code:
    1. void bpmSettings::runBPM()
    2. {
    3. //...
    4. bpmdialog *b = new bpmdialog;
    5. b->show();
    6. this->close();
    7. }
    To copy to clipboard, switch view to plain text mode 
    Here is the header of the dialog:
    Qt Code:
    1. #ifndef BPMDIALOG_H
    2. #define BPMDIALOG_H
    3.  
    4. #include <QDialog>
    5. #include "vector2d.h" // a vector class I wrote which the simulation is based on
    6.  
    7. extern GaussData globalData;
    8.  
    9. namespace Ui {
    10. class bpmdialog;
    11. }
    12.  
    13. class bpmdialog : public QDialog
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit bpmdialog(QWidget *parent = 0);
    19. ~bpmdialog();
    20.  
    21. private:
    22. Ui::bpmdialog *ui;
    23. void envPlot(const vector2d<double>& field); // plots the vector2d field by using the QCustomPlot widget in the ui.
    24. void bpm(); // starts simulation
    25. };
    26.  
    27. #endif // BPMDIALOG_H
    To copy to clipboard, switch view to plain text mode 
    And this is how my implementation of the dialog looks like:
    Qt Code:
    1. bpmdialog::bpmdialog(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::bpmdialog)
    4. {
    5. ui->setupUi(this);
    6. bpm();
    7. }
    To copy to clipboard, switch view to plain text mode 
    I hope I provided enough information. The bpm() method is basically a for loop calculating a new 2d field in every iteration, by using a Gauss Seidel algorithm to solve a linear system. The 2d field is then plotted with the envPlot(field) method in every iteration.

    Thank you very much in advance for your help!
    Best regards,
    Richard

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDialog does not appear until calculations are finished

    1. Define bpm as slot.
    2. bpm should calculate only one 2d field.
    3. The skeleton should look like :
    Qt Code:
    1. bpmdialog::bpmdialog(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::bpmdialog)
    4. {
    5. ui->setupUi(this);
    6. QTimer::singleShot(0,SLOT(bpm()));
    7. }
    8.  
    9. bpmdialog::bpm()
    10. {
    11. //calculate one 2d field
    12. envPlot(field);
    13. QTimer::singleShot(0,SLOT(bpm()));
    14. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Lesiok for this useful post:

    Richard H (15th March 2017)

  4. #3
    Join Date
    Mar 2017
    Location
    Erlangen, Germany
    Posts
    2
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QDialog does not appear until calculations are finished

    Thanks a lot for the quick answer! I just tried calling my bpm() method the way you described without changing it, I think that might already solve the problem, at least for the first plot. Now QTimer::singleShot() takes three arguments, could you please explain what the second argument *receiver means and what I should put there?

    In the documentation it looks like this:
    void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, const char *member)

    http://doc.qt.io/qt-5/qtimer.html#singleShot

    Thank you!

    EDIT: ok sorry, that was a dumb question. I figured it out. It should be QTimer::singleShot(0, this, SLOT(bpm())). Thanks again!
    Last edited by Richard H; 15th March 2017 at 17:38. Reason: Link didn't work

  5. #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: QDialog does not appear until calculations are finished

    Just to add some background info:

    you didn't see the dialog because you called the long operation in the dialog's contructor.
    So your code didn't reach the show() before it was completed.

    By delaying its invocation with the timer you not only make the dialog constructor finish right aways and letting the main code show it, you also allow the dialog to actually process the show event that is triggered by show().

    I.e. GUI systems such as QtWidgets are highly event driven, letting the main thread, which handles the events for the UI, run long operations results in UI not updating.
    Hence Lesiok's suggestion to let bpm() ideally only calculate a subset and continuously trigger further calculations via the delay mechanism again.

    This allows the main thread to catch up with UI events between these delayed calls.

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    Richard H (16th March 2017)

Similar Threads

  1. Replies: 1
    Last Post: 16th October 2014, 20:41
  2. Replies: 14
    Last Post: 5th December 2013, 09:48
  3. Replies: 9
    Last Post: 25th March 2011, 21:22
  4. Replies: 2
    Last Post: 20th January 2011, 11:41
  5. complex calculations with qt
    By gt.beta2 in forum Newbie
    Replies: 5
    Last Post: 7th February 2009, 23:55

Tags for this Thread

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.