MedicalVisualization
QGL Class Concept
As opposed to overriding the paintEvent method inherited from QWidget, an OpenGL window needs to inherit from QGLWidget and override its paintGL method, which contains the OpenGL rendering calls for a particular 3D scene.
By starting a timer in the widget’s constructor that triggers a repaint event we get a consistent animation frame rate.
#ifndef QGLWINDOW_H
#define QGLWINDOW_H
#include <QtGui/QWidget>
#include <QtOpenGL/qgl.h>
class QGLWindow: public QGLWidget
{
public:
static const double fps; // animated frames per second
//! default ctor
QGLWindow(QWidget *parent = 0)
: QGLWidget(parent)
{
setFormat(QGLFormat(QGL::DoubleBuffer|QGL::DepthBuffer));
startTimer((int)(1000.0/fps)); // ms=1000/fps
}
//! dtor
~QGLWindow()
{}
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();
void timerEvent(QTimerEvent *)
{
repaint();
}
};
#endif
#define QGLWINDOW_H
#include <QtGui/QWidget>
#include <QtOpenGL/qgl.h>
class QGLWindow: public QGLWidget
{
public:
static const double fps; // animated frames per second
//! default ctor
QGLWindow(QWidget *parent = 0)
: QGLWidget(parent)
{
setFormat(QGLFormat(QGL::DoubleBuffer|QGL::DepthBuffer));
startTimer((int)(1000.0/fps)); // ms=1000/fps
}
//! dtor
~QGLWindow()
{}
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();
void timerEvent(QTimerEvent *)
{
repaint();
}
};
#endif