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

漁獲()消息框顯示並執行再次進入initializeGL(),並顯示第二個消息框QMessageBox提示在initializeGL調用initializeGL一次

我試圖通過一個布爾值,以避免這種變量:

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

回答

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); 
    } 
} 
相關問題