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

in catch() messagebox gezeigt, und die Ausführung geht in initializeGL() wieder, und zeigt ein zweites MeldungsfeldQMessageBox in initializeGL ruft initializeGL einen weiteres Mal

Ich versuche, dies über eine Bool zu vermeiden 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; 
} 

Aber das führt zum Absturz. Also beschloss ich, Fehler zu zeigen, in paintGL() (es zeigt auch 2 Message):

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

Dies löst das Problem aber den Code hässlich. Gibt es eine schönere Lösung für dieses Problem?

Qt4.7 VS2008

Antwort

1

Hier ist die Lösung: 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); 
    } 
} 
Verwandte Themen