Reputation: 13
I used to work with Qt 5.1.1 and OpenGL used to work fine.
Now I've installed Qt 5.6 and it seems like it is not straightforward the OpenGL application as 5.1.1 is.
The GL functions (even including it with #include <QOpenGLFunctions>
returns "undefined reference". Maybe because it is a very new version, I'm not finding anything to help me out on that.
The main question is: How do proceed for using OpenGL with Qt 5.6 version? Any of GL functions works.
I see in some Qt Manual examples that initializeOpenGLFunctions()
makes it work, but this statement is not recognized by my code (I've tried to #include lots of things).
The code is the most basic:
glwidget.h:
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QGLWidget>
class GLWidget : public QGLWidget
{
Q_OBJECT
public:
explicit GLWidget(QWidget *parent = 0);
void initializeGL();
void paintGL();
void resizeGL(int w, int h);
};
#endif // GLWIDGET_H
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
glwidget.cpp:
#include "glwidget.h"
#include <QWidget>
#include <QOpenGLFunctions>
GLWidget::GLWidget(QWidget *parent) :
QGLWidget(parent)
{
}
void GLWidget::initializeGL()
{
glClearColor(1,1,0,1);
}
void GLWidget::paintGL()
{
}
void GLWidget::resizeGL(int w, int h)
{
}
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
test.pro:
QT += core gui opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Test
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
glwidget.cpp
HEADERS += mainwindow.h \
glwidget.h
FORMS += mainwindow.ui
Upvotes: 1
Views: 884
Reputation: 1733
I assume your "undefined reference error" was pointing at the glColor()
functions you were trying to use.
In order to be able to use QOpenGLFunctions
such as glColor()
, you have to make your GLWidget
to also inherit from QOpenGLFunctions
; for example:
#include <QOpenGLFunctions>
class GLWidget: public QGLWidget, protected QOpenGLFunctions
{ // ...
};
See this example for more details.
Note: it is strongly advise against using QGLWidget
in new code in favor of QOpenGLWidget
.
Upvotes: 2