Search code examples
c++qt4qglwidget

QMessageBox in initializeGL calls initializeGL one more time


void MyGlWidget::initializeGL() {
    try {
        throw std::exception();
    } catch(...) {        
        QMessageBox::critical(this, tr("Exception"), 
            tr("Exception occured"));
    }    
}

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:

void MyGlWidget::initializeGL() {
    if(in_initializeGL_)
        return;
    in_initializeGL_ = true;

    try {
        throw std::exception();
    } catch(...) {        
        QMessageBox::critical(this, tr("Exception"), 
        tr("Exception occured"));
    }

    in_initializeGL_ = false;
}

But this leads to crash. So I decided to show error in paintGL()(it also shows 2 messageboxes):

void MyGlWidget::paintGL() {
    if(in_paintGL_)
        return;
    in_paintGL_ = true;

    if (!exception_msg_.isEmpty()) {
        QMessageBox::critical(this, tr("Exception"), 
            exception_msg_);
        exception_msg_.clear();
    }

    // rendering stuff 

    in_paintGL_ = false;
}

void MyGlWidget::initializeGL() {
    try {
        throw std::exception();            
    } catch(...) {        
        exception_msg_ = "Exception in initializeGL()";
    }
}

This solves the problem but the code ugly. Is there a more nice solution of this problem?

Qt4.7 VS2008


Solution

  • Here is the solution: http://labs.qt.nokia.com/2010/02/23/unpredictable-exec/

    void MyGlWidget::initializeGL() {
        try {
            throw std::exception();        
        } catch(...) {        
            getExceptionMessage(&exception_msg_);
            QMessageBox *msgbox = new QMessageBox(QMessageBox::Warning, 
                                                  "Exception", 
                                                  exception_msg_, 
                                                  QMessageBox::Ok, 
                                                  this);
            msgbox->open(0, 0);
        }
    }