问题
We have a QWidget based application that was previously using a QWindow for OpenGL rendering. To fit that window in our application we had to use
QWidget QWidget::createWindowContainer(QWindow);
Previously we only used external to Qt OpenGL libraries for rendering.
We have a desire to switch from using QWindow to some kind of QWidget for compatibility with touch gestures, and generally better overall compatibility with the rest of our application. The latest recommended OpenGL compatible QWidget seems to be QOpenGLWidget, so we are trying to use that.
glContext
is an OpenGLContext that we manage ourselves.
The problem now is that QOpenGLWidget is not a QSurface, so I cannot call
glContext->makeCurrent(this);
to make the context current on my custom QOpenGLWidget like we could before with our custom QWindow and our own OpenGLContext.
If I try to use QOpenGLWidget::makeCurrent();
then this uses the wrong context and tries to do stuff with some magical QT handled context or something I don't understand.
The result is that after creating our OpenGLContext when we try to call
glFunctions = glContext->versionFunctions<QOpenGLFunctions_3_3_Core>();
if(!glFunctions->initializeOpenGLFunctions())
printf("Could not initialize OpenGL functions.");
It always fails to initialize the OpenGL functions.
I have been reading over all the other resources I can find on the subject and came across this older Stack Overflow question that was similar: QOpenGLWidget with shared OpenGL context?
The answer to that question did not solve my issue because we are using QApplication, not QGuiApplication as this is a QWidget based application.
QGuiApplicationPrivate::init()
is never called and the QOpenGLContext::globalShareContext()
returns a null pointer since it is not initialized.
Unfortunately I cannot wait for QOpenGLWidget::initializeGL()
to be called to initialize the QT managed OpenGLContext
since we already have lots of OpenGL resources over various classes that attempt to get initialized before that.
Has anyone else come across this same problem?
Any suggestions?
回答1:
I am doing a little guess work here.
Probably your OpenGL is defaulting to ANGLE. And your previous application OpenGL calls are platform specific OpenGL API.
try setting below attribute to your QApplication
object.
appObject->setAttribute(Qt::AA_UseDesktopOpenGL, true);
then try calling
QOpenGLWidget::makeCurrent();
If things are still not working, configure your QT libraries for desktop OpenGL.
configure -opengl desktop
the below link gives you some information on Qt approach to OpenGL. http://doc.qt.io/qt-5/windows-requirements.html
Put in comments, if this is not helping you, will delete the answer.
来源:https://stackoverflow.com/questions/47058621/qopenglwidget-with-qapplication