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

在 catch() 中显示消息框,执行再次进入 initializeGL(),并显示第二个消息框

我试图通过 bool 变量避免这种情况:

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;
}

但这会导致崩溃。所以我决定在paintGL()中显示错误(它还显示了2个消息框):

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()";
    }
}

这解决了问题,但代码很难看。这个问题有更好的解决方案吗?

Qt4.7 VS2008

4

1 回答 1

1

这是解决方案: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);
    }
}
于 2010-11-28T20:11:04.497 回答