QMessageBox中的initializeGL再次调用initializeGL

发布于 2024-10-04 13:44:09 字数 1256 浏览 0 评论 0原文

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

在 catch() 中显示消息框,执行再次进入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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹊巢 2024-10-11 13:44:09

这是解决方案:
http://labs.qt.nokia.com/2010/02/ 23/不可预测的执行/

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

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文