I tried to build a simple OpenGL App with Qt 5.5.1, and everything is fine up until I try to use openGL native function calls like glClearColor.
The Widget actually compiles and produces a black screen, but after I try to use any openGL native function it doesn't even link, but produces the error:
glwidget.cpp:10: error: undefined reference to '_imp__glClearColor@16'
Here is the .pro file:
QT += core gui opengl
CONFIG += windows
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Vehicle_simulation
TEMPLATE = app
SOURCES += main.cpp\
simulation.cpp \
glwidget.cpp
HEADERS += simulation.h \
glwidget.h
FORMS += simulation.ui
I'm using Desktop Qt mingw492_32 kit. The strange thing though is that I did not find libQtOpenGL.so in the lib folder. Is my QT installation faulty? I tried to reinstall it multiple times, but it didn't help. Where can I download that specific library?
linking it to the project would solve the problem, but I can't seem to find it anywhere.
The problem is the openGL module is missing from the QT installation, it's not that I am unable to link to it.
After I opened up an example program by chance, just to take a look at the implementation; I found that the same QOpenGLWidget
has been implemented in a new example program.
After a bit of analysis I managed to figure out the problem.
QT has an internal openGL implementation and native openGL support as well. The widget I used inherited from QOpenGLWidget
, but the openGL function calls (like glClearColor
) tried to access native openGL implementation in Qt. which Since those were not included in my Qt build, the project would not build.
To fix this one has to either run a custom Qt core build, or use the openGL wrapper provided by Qt.
I used the Qt wrapper in the end, which was also used by the example program Cube. the class used to implement the widget (called GLWidget
in my implementation) has to inherit not only from QOpenGLWidget
, but also from QOpenGLFunctions
. The latter part was missing from my implementation. The source code for it is the following:
glwidget.h (initial):
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QOpenGLFunctions>
#include <QOpenGLWidget>
class GLWidget : public QOpenGLWidget
{
Q_OBJECT
public:
explicit GLWidget(QWidget *parent);
protected:
void initializeGL();
void paintGL();
void resizeGL(int w, int h);
};
#endif // GLWIDGET_H
glwidget.h (fixed):
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QOpenGLFunctions>
#include <QOpenGLWidget>
class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
Q_OBJECT
public:
explicit GLWidget(QWidget *parent);
protected:
void initializeGL() Q_DECL_OVERRIDE;
void paintGL() Q_DECL_OVERRIDE;
void resizeGL(int w, int h) Q_DECL_OVERRIDE;
};
#endif // GLWIDGET_H
Before any gl* functions are used the function initializeOpenGLFunctions();
has to be called, otherwise a very cryptic run-time error will pop up.
i.e.:
void GLWidget::initializeGL(){
initializeOpenGLFunctions();
glClearColor(1,0,0,1);
}
Hope this will help someone else as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With