Qt-UI
QGL Example
← OpenGL | ● | GUI Concept →
Instead of overriding paintEvent(), we derive from QGLWidget and override paintGL() and place OpenGL calls in it.
We also override initializeGL() and resizeGL() as shown in the following example:
#include <QtGui/QApplication>
#include <QtGui/QWidget>
#include <QtOpenGL/qgl.h>
#include <GL/gl.h>
#include <GL/glu.h>
static const double fps=30.0; // animated frames per second
class MyQGLWidget: public QGLWidget
{
public:
//! default ctor
MyQGLWidget(QWidget *parent = 0)
: QGLWidget(parent)
{
setFormat(QGLFormat(QGL::DoubleBuffer|QGL::DepthBuffer));
startTimer((int)(1000.0/fps)); // ms=1000/fps
}
//! dtor
~MyQGLWidget()
{}
protected:
void initializeGL()
{
qglClearColor(Qt::black);
glEnable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
}
void resizeGL(int, int)
{
glViewport(0, 0, width(), height());
}
void paintGL()
{
// clear frame buffer
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// ... opengl rendering commands go here ...
// setup perspective matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0,1.0,0.1,10.0);
// setup model-view matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// define local rotated coordinate system
static double angle=0.0; // rotation angle in degrees
static const double omega=180.0; // rotation speed in degrees/s
glTranslated(0.0,0.0,-2.0);
glRotated(angle,0.0,1.0,0.0);
// render triangles
glBegin(GL_TRIANGLES);
glColor3f(0.0f,0.75f,0.0f);
glVertex3d(-0.5,-0.5,0.0);
glVertex3d(0.5,-0.5,0.0);
glVertex3d(0.0,0.5,0.0);
glColor3f(1.0f,0.75f,1.0f);
glVertex3d(0.0,-0.5,-0.5);
glVertex3d(0.0,-0.5,0.5);
glVertex3d(0.0,0.5,0.0);
glEnd();
// angle delta equals time delta times omega
double dt=1.0/fps;
angle+=dt*omega;
}
void timerEvent(QTimerEvent *)
{
repaint();
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
if (!QGLFormat::hasOpenGL()) return(1);
MyQGLWidget main;
main.show();
return(app.exec());
}
#include <QtGui/QWidget>
#include <QtOpenGL/qgl.h>
#include <GL/gl.h>
#include <GL/glu.h>
static const double fps=30.0; // animated frames per second
class MyQGLWidget: public QGLWidget
{
public:
//! default ctor
MyQGLWidget(QWidget *parent = 0)
: QGLWidget(parent)
{
setFormat(QGLFormat(QGL::DoubleBuffer|QGL::DepthBuffer));
startTimer((int)(1000.0/fps)); // ms=1000/fps
}
//! dtor
~MyQGLWidget()
{}
protected:
void initializeGL()
{
qglClearColor(Qt::black);
glEnable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
}
void resizeGL(int, int)
{
glViewport(0, 0, width(), height());
}
void paintGL()
{
// clear frame buffer
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// ... opengl rendering commands go here ...
// setup perspective matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0,1.0,0.1,10.0);
// setup model-view matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// define local rotated coordinate system
static double angle=0.0; // rotation angle in degrees
static const double omega=180.0; // rotation speed in degrees/s
glTranslated(0.0,0.0,-2.0);
glRotated(angle,0.0,1.0,0.0);
// render triangles
glBegin(GL_TRIANGLES);
glColor3f(0.0f,0.75f,0.0f);
glVertex3d(-0.5,-0.5,0.0);
glVertex3d(0.5,-0.5,0.0);
glVertex3d(0.0,0.5,0.0);
glColor3f(1.0f,0.75f,1.0f);
glVertex3d(0.0,-0.5,-0.5);
glVertex3d(0.0,-0.5,0.5);
glVertex3d(0.0,0.5,0.0);
glEnd();
// angle delta equals time delta times omega
double dt=1.0/fps;
angle+=dt*omega;
}
void timerEvent(QTimerEvent *)
{
repaint();
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
if (!QGLFormat::hasOpenGL()) return(1);
MyQGLWidget main;
main.show();
return(app.exec());
}
Access the example via WebSVN:
Checkout the Qt example via SVN:
svn co svn://schorsch.efi.fh-nuernberg.de/qt-examples/example-03
← OpenGL | ● | GUI Concept →