<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>libQGLViewer callback example</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <link href="../qglviewer.css" rel="stylesheet" type="text/css" /> <link rel="shortcut icon" href="../images/qglviewer.ico" type="image/x-icon" /> <link rel="icon" href="../images/qglviewer.icon.png" type="image/png" /> </head> <body> <table class="banner"> <tr> <td align="center"><a href="../index.html"><b>Home</b></a></td> <td align="center"><a href="../refManual/hierarchy.html"><b>Documentation</b></a></td> <td align="center"><a href="../download.html"><b>Download</b></a></td> <td align="center" class="qindexHL"><a href="index.html"><b>Screenshots</b></a></td> <td align="center"><a href="../developer.html"><b>Developer</b></a></td> </tr> </table> <h1>The callback example</h1> <center> <img src="../images/callback.jpg" width="300" height="200" alt="callback"/> </center> <p> Using the Qt signal-slot mechanism to connect a QGLViewer and your scene. </p> <p> 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 <code>drawNeeded()</code> QGLViewer signal is connected to the Scene <code>drawScene()</code> slot. </p> <h2>callback.h</h2> <pre> <span class="dir">#include <qobject.h> </span> <span class="key">class </span>QGLViewer; <span class="com">// The Scene class must derive from QObject in order to allow a </span><span class="com">// signal slot mechanism. </span><span class="key">class </span>Scene : <span class="key">public </span>QObject { Q_OBJECT <span class="com">// must include this in order to use Qt signals/slots </span> <span class="key">public </span>: Scene(<span class="typ">const </span>QGLViewer* <span class="typ">const </span>v); <span class="key">public </span>slots: <span class="typ">void </span>drawScene(); };</pre> <h2>callback.cpp</h2> <pre> <span class="dir">#include <QGLViewer/qglviewer.h> </span><span class="dir">#include </span><span class="dstr">"callback.h"</span><span class="dir"> </span><span class="dir">#include <math.h> </span> Scene::Scene(<span class="typ">const </span>QGLViewer* <span class="typ">const </span>v) { <span class="com">// Connect the viewer signal to our draw function slot </span> connect(v, SIGNAL(drawNeeded()), <span class="key">this</span>, SLOT(drawScene())); } <span class="com">// Draws a spiral </span><span class="typ">void </span>Scene::drawScene() { <span class="typ">const float </span>nbSteps = <span class="num">500.0</span>; glBegin(GL_QUAD_STRIP); <span class="key">for </span>(<span class="typ">float </span>i=<span class="num">0</span>; i<nbSteps; ++i) { <span class="typ">float </span>ratio = i/nbSteps; <span class="typ">float </span>angle = <span class="num">21.0</span>*ratio; <span class="typ">float </span>c = cos(angle); <span class="typ">float </span>s = sin(angle); <span class="typ">float </span>r1 = <span class="num">1.0 </span>- <span class="num">0.8</span>*ratio; <span class="typ">float </span>r2 = <span class="num">0.8 </span>- <span class="num">0.8</span>*ratio; <span class="typ">float </span>alt = ratio <span class="num">- 0.5</span>; <span class="typ">const float </span>nor = .<span class="num">5</span>; <span class="typ">const float </span>up = sqrt(<span class="num">1.0</span>-nor*nor); glColor3f(<span class="num">1</span>-ratio, .<span class="num">2 </span>, ratio); glNormal3f(nor*c, up, nor*s); glVertex3f(r1*c, alt, r1*s); glVertex3f(r2*c, alt+<span class="num">0.05</span>, r2*s); } glEnd(); }</pre> <h2>main.cpp</h2> <pre> <span class="dir">#include <QGLViewer/qglviewer.h> </span><span class="dir">#include <qapplication.h> </span><span class="dir">#include <qmessagebox.h> </span><span class="dir">#include </span><span class="dstr">"callback.h"</span><span class="dir"> </span> <span class="key">using namespace </span>std; <span class="typ">void </span>help() { QString text(<span class="str">"<h2>C a l l b a c k</h2>"</span>); text += <span class="str">"This example is conceptually the same as simpleViewer."</span>; text += <span class="str">"The difference is that it uses the Qt signal/slot mechanism "</span>; text += <span class="str">"instead of deriving the QGLViewer class. "</span>; text += <span class="str">"A Scene::draw() function is connected to the QGLViewer::drawNeeded() "</span>; text += <span class="str">"signal. The two classes are otherwise completely independant."</span>; QMessageBox::information(NULL, <span class="str">"Callback exemple"</span>, text); } <span class="typ">int </span>main(<span class="typ">int </span>argc, <span class="typ">char</span>** argv) { <span class="com">// Read command lines arguments. </span> QApplication application(argc,argv); <span class="com">// Instantiate the viewer. </span> QGLViewer v; <span class="com">// Make the viewer window visible on screen. </span> v.show(); <span class="com">// Restore the previous viewer state. </span> v.restoreFromFile(); <span class="com">// Create a scene, giving a pointer to the associated viewer. </span> Scene s(&v); <span class="com">// Set the viewer as the application main widget. </span> application.setMainWidget(&v); help(); <span class="com">// Run main loop. </span> <span class="key">return </span>application.exec(); }</pre> <p> <a href="index.html">Go back</a> to the examples main page </p> <p> <a href="http://validator.w3.org/check/referer"><img src="../images/xhtml.png" alt="Valid XHTML 1.0!" height="31" width="88" border="0"/></a> <a href="http://jigsaw.w3.org/css-validator/check/referer"><img src="../images/css.png" width="88" height="31" alt="Valid CSS!" border="0"/></a> <i>Last modified on Thursday, February 5, 2004.</i> </p> </body> </html>