Qt with OpenGL

Not the best place to ask this question but I will try asking it here anyway! (Seeing as this is the only place I am semi active in)

I am trying Qt creator 2.6 (which is used with the source code from qt v.5.0) seeing as a lot of people recommend qt for a GUI. All of the examples/tutorials I see are including QtOpenGL and then are using glVertex etc without anything else needed. However whenever I try and use any immediate mode GL calls it says error: C3861: 'glVertex2f': identifier not found

When I check the GL version with glGetString(GL_VERSION) it says OpenGL ES 2.0 (which means no immediate mode I know). Am I missing something obvious or do I need to link the libraries myself? I'm guessing it's just me being stupid somehow.

EDIT: I have already added QT += core gui opengl to the project file.
Last edited on
#include <GL/gl.h>

> do I need to link the libraries myself?
You will need to link the libraries where the definitions are.
However, `identifier not found' is a compile error.
Interesting I thought QtOpenGL would include that, seems to solve my problems though.

I am getting a black screen now when trying to override the initializeGL()/resizeGL(int w, int h)/paintGL() methods. So will see if I can make any progress there (any advice would be helpful too).
Well I'm out of ideas right now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void Opengl::initializeGL()
{
    glClearColor(1, 1, 0, 1);
}

void Opengl::resizeGL(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0,w,0,h,-1,1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void Opengl::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(1,0,0);
    glBegin(GL_POLYGON);
      glVertex2f(0,0);
      glVertex2f(100,500);
      glVertex2f(500,100);
    glEnd();
}


I have overloaded these 3 methods which should get called automatically by qt. I have a widget in the creator and have promoted it to this class. But still all I see is http://puu.sh/24PJh
Post your main.
Oops sorry about that.

None of the tutorials I have seen have touched main because they promote the widget used for opengl directly from the ui form and then they say all of the other functions get called without me having to do it directly, but they might of missed out some key element. None of these were written by me.

1
2
3
4
5
6
7
8
9
10
11
12
13
 
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;

    w.show();

    return a.exec();
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
//Main Window .cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"  // Form with ui elements

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Main Window .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 


I guess if I could figure out how to write it directly in code it would be just as good.
Last edited on
Full example, please

Taken from http://programanddesign.com/cpp/qt-opengl-code-example/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <QtGui/QApplication>
#include <QtOpenGL/QGLWidget>

class OpenGL: public QGLWidget{
protected:
	void initializeGL(){
		glClearColor(0,1,1,0);
	}

	void resizeGL(int w, int h){
		glViewport(0, 0, w, h);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glOrtho(0,w,0,h,-1,1);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
	}

	void paintGL(){
		glClear(GL_COLOR_BUFFER_BIT);

		glColor3f(1,0,0);
		glBegin(GL_POLYGON);{
			glVertex2f(0,0);
			glVertex2f(100,500);
			glVertex2f(500,100);
		}glEnd();
	}
};

int main(int argc, char **argv){
	QApplication app(argc, argv);

	OpenGL window;
	//window.resize(800,600); //tilling windows manager
	window.show();

	return app.exec();
}

Link against -l{GL,Qt{Core,Gui,OpenGL}}
Last edited on
Yeah I have tried copying that example directly and it all runs fine, but I still seem to be getting just a black screen.

I don't know if I want to go through the hassle of trying to get it working on netbeans instead.

EDIT: After trying to get Netbeans working for what felt like 10000 hours (I am starting to be amazed anyone ever gets qt working) it seems to actually display the triangle. The only difference between qt creator and netbeans was the version of qt I used (seeing as netbeans didn't seem to like qt 5.0). For now I am just going to blame that and stay with netbeans.

Thanks for all of the advice ne555. I will go mess with the creator more later on.
The same code works in the older version of creator, so the moral of the story is never use 5.0 for anything.
Last edited on
Topic archived. No new replies allowed.