Qt + OpenGL Code Example
I wanted to develop a game using OpenGL but I was having trouble deciding on a windowing library. Somebody suggested I try Qt, so I decided to give it another shot. I heard it was good, but the editor it came with was so foreign and unintuitive. Unfortunately, this overshadowed what a beautiful library Qt really is! It takes a little getting used to, but it really is well designed.
Turns out Netbeans 6.8 (and perhaps earlier versions) have support for Qt, so it's pretty easy to get set up. It's probably way too early to be posting any code, but I'm going to anyways because I'm way too excited about this (yeah, I live a sad life).
main.cpp
#include <QtOpenGL/QGLWidget>
#include "GLWidget.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
GLWidget window;
window.resize(800,600);
window.show();
return app.exec();
}
GLWidget.h
#define _GLWIDGET_H
#include <QtOpenGL/QGLWidget>
class GLWidget : public QGLWidget {
Q_OBJECT // must include this if you use Qt signals/slots
public:
GLWidget(QWidget *parent = NULL);
protected:
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void keyPressEvent(QKeyEvent *event);
};
#endif /* _GLWIDGET_H */
All those functions you see above will get called for you automatically. No need to register any callbacks!
GLWidget.cpp
#include "GLWidget.h"
GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent) {
setMouseTracking(true);
}
void GLWidget::initializeGL() {
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glDisable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glEnable(GL_POLYGON_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0, 0, 0, 0);
}
void GLWidget::resizeGL(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, 0, h); // set origin to bottom left corner
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void GLWidget::paintGL() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0);
glBegin(GL_POLYGON);
glVertex2f(0,0);
glVertex2f(100,500);
glVertex2f(500,100);
glEnd();
}
void GLWidget::mousePressEvent(QMouseEvent *event) {
}
void GLWidget::mouseMoveEvent(QMouseEvent *event) {
printf("%d, %d\n", event->x(), event->y());
}
void GLWidget::keyPressEvent(QKeyEvent* event) {
switch(event->key()) {
case Qt::Key_Escape:
close();
break;
default:
event->ignore();
break;
}
}
It just draws a red triangle and tracks your mouse movement. You need setMouseTracking(true); in order for the mouse move callback to fire when you aren't holding a button down. I need this for my game.
Anyway, that's all I've got so far, but that should get you up and running with Qt + OpenGL anyway, the rest is native OpenGL and your typical game layout; you shouldn't need too much more Qt-specific stuff. Although, Qt does seem to have some nice OpenGL helpers and other neat stuff I have to dig into yet
Leave a comment if you need some help getting set up on Netbeans/Ubuntu: there are a couple quirks but nothing too terrible.
August 7th, 2010 - 08:12
The code is work fine for me.
Thanks for the post.
August 12th, 2010 - 06:33
Thanks a lot.
August 30th, 2010 - 13:35
Thanks!
Exactly what I was looking for. A simple clean, starting example.
October 2nd, 2010 - 02:41
printf was not declared in this scope….
#include
helped
and printf seam to be buffered, so you can mention about it in tutorial
thx for tutorial
October 2nd, 2010 - 02:42
#include “stdio.h”
June 26th, 2011 - 03:54
instead of printf() you could also use qDebug().
July 5th, 2011 - 22:59
Thanks for the tip! Didn’t know about that.
October 11th, 2011 - 01:06
I am coming from a C++ and QT orientation. Happened to run into your page where OpenGL integration with QT is discussed. Great topic.
I tried downloading the code you had shown above into my C++ ready netbeans IDE to see if it would run on its own. Unfortunately, it failed. I believe some OpenGL library for windows is required.
What to install for OpenGL? How? Perhaps that will enable the above code to run.
October 15th, 2011 - 18:43
Hey Sai, you need to make sure in your .pro file you have this line:
QT = core gui opengl
that tells Qt to use the OpenGL module
You also need to make sure you are linking to the opengl32.lib and glu32.lib
You can right click on the project folder on the left and then click “Add library” Just add both those libraries by pasting in their respective paths.
Hope that helps!
October 16th, 2011 - 12:24
Hi guys,
Thanks for answering him, Art! I’ve forgotten what the process is myself, it’s been awhile since I’ve done any C++ programming, nevermind Qt + OpenGL!
Mark
November 26th, 2011 - 13:30
It works very good. Thanks!
January 8th, 2012 - 10:42
Thanks for example. Really work)))
February 10th, 2012 - 11:22
Thanks. It was a ladder for me.
April 28th, 2012 - 07:56
Looks nice. But when i want to compile it, he says that he didn’t found the function gluOrtho2D. What can i do??
May 8th, 2012 - 06:53
gluOrtho2D was not declared in this scope