【问题标题】:Cant use context made by external QOpenGLWidget?不能使用外部 QOpenGLWidget 制作的上下文?
【发布时间】:2017-01-07 20:25:50
【问题描述】:

调试时的确切崩溃是:

下级停止了,因为它触发了异常。 在线程 0 中停止的原因:0x7fed96c6cda 异常,代码:0x0000005:读取访问冲突:0x0,flags=0x0。

然后异常指向下面代码中的这一行:

if(QOpenGLContext::currentContext()->isValid())

下面的代码足以为我重现异常。 子类化 QOpenGLWidget 类并在尝试访问上下文之前使子类调用 initializeGL() 并不能解决问题。

#include <QApplication>
#include <QOpenGLWidget>
#include <QOpenGLContext>
#include <QDebug>

void initialize(QOpenGLWidget * renderArea)
{
    renderArea->makeCurrent();
    if(QOpenGLContext::currentContext()->isValid())
    {
        qInfo() << "Valid.";
    }
}

int main(int argc, char *argv[])
{
    QSurfaceFormat format;
    format.setVersion(3,3);
    format.setProfile(QSurfaceFormat::CoreProfile);
    QSurfaceFormat::setDefaultFormat(format);

    QApplication a(argc, argv);

    QOpenGLWidget * glw = new QOpenGLWidget;
    initialize(glw);

    return a.exec();
}

【问题讨论】:

    标签: c++ qt exception


    【解决方案1】:

    我现在明白了答案:您必须在事件循环开始后进行所有这些 opengl 初始化。

    “MainWindow”类在接收信号“onEventLoopStarted”时执行所有opengl初始化的固定代码:

    #include "mainwindow.h"
    #include <QApplication>
    #include <QOpenGLWidget>
    #include <QOpenGLContext>
    #include <QDebug>
    #include <QTimer>
    
    int main(int argc, char *argv[])
    {
        QSurfaceFormat format;
        format.setVersion(3,3);
        format.setProfile(QSurfaceFormat::CoreProfile);
        QSurfaceFormat::setDefaultFormat(format);
    
        QApplication a(argc, argv);
    
        MainWindow w;
        w.resize(512, 512);
        w.show();
    
        QTimer::singleShot(0, &w, SLOT(onEventLoopStarted()));
    
        return a.exec();
        //Window receives event and begins to initialize.
    }
    

    【讨论】:

    • 15 分钟是您的问题和答案之间经过的时间。你应该花更多的时间来考虑你的问题。
    • 实际上是提出这个问题来帮助我解决这个问题。但是,您可能是对的。
    猜你喜欢
    • 1970-01-01
    • 2016-02-11
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多