Using the Qt signal-slot mechanism to connect a QGLViewer to your scene.
This very simple application uses the Qt signal-slot callback mechanism to link the QGLViewer and
your Scene. The two class hence remain totally independent. 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 <QGLViewer/qglviewer.h> #include "callback.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 = 200.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 <QGLViewer/qglviewer.h> #include <qapplication.h> #include <qmessagebox.h> #include "callback.h" using namespace std; void help() { QString text("<h2>C a l l b a c k</h2>"); text += "This example is conceptually the same as simpleViewer."; text += "The difference is that it uses the Qt signal/slot mechanism "; text += "instead of deriving the QGLViewer class. "; text += "A Scene::draw() function is connected to the QGLViewer::drawNeeded() "; text += "signal. The two classes are otherwise completely independant."; QMessageBox::information(NULL, "Callback exemple", text); } 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(); }
Go back to the examples main page