Results 1 to 6 of 6

Thread: QMessageBox in initializeGL calls initializeGL one more time

  1. #1
    Join Date
    Sep 2008
    Posts
    25
    Thanks
    3
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QMessageBox in initializeGL calls initializeGL one more time

    Qt Code:
    1. void MyGlWidget::initializeGL() {
    2. try {
    3. throw std::exception();
    4. } catch(...) {
    5. QMessageBox::critical(this, tr("Exception"),
    6. tr("Exception occured"));
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    in catch() messagebox is shown and execution goes into initializeGL() again, and shows a second message box

    I'm trying to avoid this via a bool variable:

    Qt Code:
    1. void MyGlWidget::initializeGL() {
    2. if(in_initializeGL_)
    3. return;
    4. in_initializeGL_ = true;
    5.  
    6. try {
    7. throw std::exception();
    8. } catch(...) {
    9. QMessageBox::critical(this, tr("Exception"),
    10. tr("Exception occured"));
    11. }
    12.  
    13. in_initializeGL_ = false;
    14. }
    To copy to clipboard, switch view to plain text mode 
    But this leads to crash. So I decided to show error in paintGL()(it also shows 2 messageboxes):

    Qt Code:
    1. void MyGlWidget::paintGL() {
    2. if(in_paintGL_)
    3. return;
    4. in_paintGL_ = true;
    5.  
    6. if (!exception_msg_.isEmpty()) {
    7. QMessageBox::critical(this, tr("Exception"),
    8. exception_msg_);
    9. exception_msg_.clear();
    10. }
    11.  
    12. // rendering stuff
    13.  
    14. in_paintGL_ = false;
    15. }
    16.  
    17. void MyGlWidget::initializeGL() {
    18. try {
    19. throw std::exception();
    20. } catch(...) {
    21. exception_msg_ = "Exception in initializeGL()";
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 
    This solves the problem but the code is ugly. Is there a more nice solution of this problem?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QMessageBox in initializeGL calls initializeGL one more time

    Does this double showing of QMessageBox also happens when you construct the messagebox with a NULL parent?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Sep 2008
    Posts
    25
    Thanks
    3
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMessageBox in initializeGL calls initializeGL one more time

    Yes.
    The call stack is(from down to up):
    ...
    initializeGL()
    updateGL()
    QMessageBox::critical()
    initializeGL()

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QMessageBox in initializeGL calls initializeGL one more time

    hmm... my guess is that it has to do with QMessageBox showing it self, while you are in a paintEvent() - that maybe forces a general paintEvent() on all the applications visible widgets, resulting in a recursive call.
    But that is only a guess.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMessageBox in initializeGL calls initializeGL one more time

    Opening the QMessageBox using one of the static functions starts a new event loop. This is an Unpredictable exec() issue.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

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

    DIMEDROLL (28th November 2010)

  7. #6
    Join Date
    Sep 2008
    Posts
    25
    Thanks
    3
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default [Solved] QMessageBox in initializeGL calls initializeGL one more time

    Thanks! Nice article and solution.
    Qt Code:
    1. void MyGlWidget::initializeGL() {
    2. try {
    3. throw std::exception();
    4. } catch(...) {
    5. getExceptionMessage(&exception_msg_);
    6. QMessageBox *msgbox = new QMessageBox(QMessageBox::Warning,
    7. "Exception",
    8. exception_msg_,
    9. this);
    10. msgbox->open(0, 0);
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 
    Don't forget to use "new QMessageBox", because "QMessageBox msgbox;" will be deleted on scope exit. open() doesn't block the execution.

Similar Threads

  1. How to use IOCTL calls with QT?
    By augusbas in forum Qt Programming
    Replies: 3
    Last Post: 15th November 2010, 07:55
  2. free/delete calls
    By jcoop in forum Newbie
    Replies: 1
    Last Post: 7th March 2009, 09:04
  3. QTabBar::tabInserted(int..) never calls
    By khcbabu in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2009, 17:04
  4. Polymorphism with static calls
    By niko in forum General Programming
    Replies: 12
    Last Post: 9th November 2007, 09:52
  5. paintGL() and initializeGL() help
    By mickey in forum Newbie
    Replies: 14
    Last Post: 26th February 2006, 17:15

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.