| Home | Documentation | Download | Screenshots | Developer |
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.
#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();
};
#include "callback.h"
#include "qglviewer.h"
#include <math.h>
Scene::Scene(const QGLViewer* const v)
{
// 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 = 21.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();
}
#include "callback.h"
#include "qglviewer.h"
#include <qapplication.h>
using namespace std;
void help()
{
cout << endl << "\t\t- - C a l l b a c k - -" << endl << endl;
cout << "This example is conceptually the same as simpleViewer." << endl;
cout << "The difference is that it uses the Qt signal/slot mechanism" << endl;
cout << "instead of deriving the QGLViewer class." << endl << endl;
cout << "A Scene::draw() function is connected to the QGLViewer::drawNeeded()" << endl;
cout << "signal. The two classes are otherwise completely independant." << endl;
}
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();
// Restore the previous viewer state.
v.restoreFromFile();
// Create a scene, giving a pointer to the associated viewer.
Scene s(&v);
// Set the viewer as the application main widget.
application.setMainWidget(&v);
help();
// Run main loop.
return application.exec();
}
Back to the main page