Home Documentation Download Screenshots Developper

callback

callback

Using the QT callback signal-slot to connect a QGLViewer and your scene.

This very simple application uses the QT callback signal-slot mechanism to link the QGLViewer and your Scene. The drawNeeded() QGLViewer signal is connected to the Scene drawScene() slot.

callback.h

#include <qobject.h>

class QGLViewer;

// The Scene class must derive from QObject in order to allow a
// signal slot mechanism.
class Scene : public QObject
{
  Q_OBJECT        // must include this in order to use Qt signals/slots
  
public :
  Scene(const QGLViewer* const v);
  
public slots:
  void drawScene();
};

callback.cpp

#include "callback.h"
#include "qglviewer.h"
#include <math.h>

Scene::Scene(const QGLViewer* const v)
  : QObject()
{
  // Connect the viewer signal to our draw function slot
  connect(v, SIGNAL(drawNeeded()), this, SLOT(drawScene()));
}

// Draws a spiral
void Scene::drawScene()
{
  const float nbSteps = 500.0;
  glBegin(GL_QUAD_STRIP);
  for (float i=0; i<nbSteps; ++i)
    {
      float ratio = i/nbSteps;
      float angle = 20.0*ratio;
      float c = cos(angle);
      float s = sin(angle);
      float r1 = 1.0 - 0.8*ratio;
      float r2 = 0.8 - 0.8*ratio;
      float alt = ratio - 0.5;
      const float nor = .5;
      const float up = sqrt(1.0-nor*nor);
      glColor3f(1-ratio, .2 , ratio);
      glNormal3f(nor*c, up, nor*s);
      glVertex3f(r1*c, alt, r1*s);
      glVertex3f(r2*c, alt+0.05, r2*s);
    }
  glEnd();
}

main.cpp

#include "callback.h"
#include "qglviewer.h"
#include <qapplication.h>

int main(int argc, char** argv)
{
  // Read command lines arguments.
  QApplication application(argc,argv);

  // Instantiate the viewer.
  QGLViewer v;

  // Make the viewer window visible on screen.
  v.show();

  // Create a scene, giving a pointer to the associated viewer.
  Scene s(&v);

  // Set the viewer as the application main widget.
  application.setMainWidget(&v);
  
  // Run main loop.
  return application.exec();
}

Back to the main page

Valid XHTML 1.0! Valid CSS!