[SOLVED]Problem creating a QOpenGLContext
I am attempting to use a QOpenGLContext in conjunction with a QWindow embedded in a QWidget using: @QWidget::createWindowContainer@
My problem begins when i attempt to initialize glew like this.
@ glewExperimental = GL_TRUE;
GLenum err = glewInit();
qDebug() << QThread::currentThread();
qDebug() << QOpenGLContext::currentContext();
if(err != GLEW_OK)
QString s = reinterpret_cast<const char*>(glewGetErrorString(err));
qDebug() << s;
The QOpenGLContext::currentContext output in this section outputs QObject(0x0), which indicates to me that the current context is null(is this true?). glewInit() returns "Missing GL Version" possibly because of the invalid context. This following section of code is called after i initialize my class that inherits QWindow and has a OpenGLSurface type along with my QOpenGLContext in a separate class (Same Thread as verified by the debugger output above and in the following section of code). context and format are both member variables of my class that inherits QWindow, and are initialized in the constructor.
format = QSurfaceFormat(QSurfaceFormat::DebugContext);
format = QSurfaceFormat();
qDebug() << "context creation failed";
qFatal("Opengl context invalid!");
qDebug() << "context: " << &context;
qDebug() << QOpenGLContext::currentContext();
The above section of code is the root of the issue. Is this how you are supposed to initialize a QOpenGLContext? I think that I am doing it wrong because context.create() returns true and so does context.isValid(), however when i output the context to the console in the second to last line I get something that looks valid "context: QOpenGLContext(0x1df6a4)", however in the next line when I call the static current context function that is supposed to output the last context to call makeCurrent(), I get the same invalid output as in the glew initialization section, "QObject(0x0)". Am I creating my context incorrectly or is it likely some other issue outside this section of code?
In case anybody else is having trouble using opengl with a QWindow embedded in a QWidget my problems were resolved by showing the window on its own without calling createWindowContainer to create a container for the window
I have successfully created a QT application with an OpenGL window inside my applications main window. I think this is what you are trying to do.
Here is how my application does it.
- Create a new QT Widgets Application
- Open the mainwindow.ui (form)
- Drag a Widget container onto the form and stretch it to the size of the OpenGL window you desire.
- Click on the new widget inside the form, and right click to bring up the pop-up window. In that window select 'Promote' item. In the dialog that pops up choose to create a new class that is of type QGLWidget, with protected QGLFunctions type, and give the new class a name. This will create the .cpp, and .h for the new class and will associate it with the form item.
- The new QGLWidget class should include a member function: initializeGL. This function should call initializeGLFunctions(), and it should also initialize the shader, textures, and OpenGL objects you want to use.
- The paintGL() function should repaint your objects and texture them with textures already loaded.
The details of doing many of the above steps can be quite daunting for a newbie. You will want to have a copy of the OpenGL Bible handy since most of the operations you need to do are OpenGL operations. But you can get a quick idea of how to do most of it by looking at the cube QT example. The cube example can provide most of what you need to know to draw within the context, including how to setup the [v,f]shader and textures. The above instructions tell you how to get an OpenGL window inside a dialog or form. Most OpenGL functions don't require you reference a context directly. The context is managed opaquely by the QGLWidget class. You may be working hard to get something you really don't need. Look carefully at the QT cube example. Hope this helps...
Ok thank you for your reply, however, my one concern with this method is control over the paintGL function. If I were to use a QGLWidget instead of a QWindow would i have control over when paintGL is called? Using a QWindow I explicitly call swapBuffers, but with QGLWidget it seems like Qt will handle the rendering internally. Is this true or can I control when a QGLWidget renders?
I didn't realize you had found a solution, and I think you are correct about PaintGL(). I don't think you can directly control when it is called. I haven't tried combining OpenGL features with QPainter, although Chapter 20 of "C++ GUI Programming with Qt 4" by Jasmin Blanchette, and Mark Summerfield, does discuss how to do this. If you have any more issues, you might try looking at their book. Cheers!