Program & Design Tips, tricks, tutorials, and tools on programming & web design

9Aug/0915

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 <QtGui/QApplication>
#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

#ifndef _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 <QtGui/QMouseEvent>
#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.

Filed under: C++ Leave a comment
Comments (15) Trackbacks (2)
  1. The code is work fine for me.
    Thanks for the post.

  2. Thanks a lot.

  3. Thanks!
    Exactly what I was looking for. A simple clean, starting example.

  4. 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

  5. #include “stdio.h”

  6. instead of printf() you could also use qDebug().

  7. Thanks for the tip! Didn’t know about that.

  8. 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.

  9. 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!

  10. 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

  11. It works very good. Thanks!

  12. Thanks for example. Really work)))

  13. Thanks. It was a ladder for me.

  14. Looks nice. But when i want to compile it, he says that he didn’t found the function gluOrtho2D. What can i do??

  15. gluOrtho2D was not declared in this scope


Leave a comment

(required)