Adding a graphical user interface around a QGLViewer
.
A simple viewer example included in a user interface designed with designer
. The
interface.ui
file is the description of the interface created with
designer
.
You can install the QGLViewer designer plugin so that the QGLViewer appears as a standard Qt
widget in the designer's widget tabs. The viewer is fully functional and be manipulated when you
test your interface in designer
. See the installation
pages for details on the plugin installation.
An alternative to the plugin technique is to use the qglviewer.cw
(custom widget)
file located in this directory (also available in the QGLViewer header file directory). It
describes all the QGLViewer signals and slots. Add a "Custom Widget" using the
Tools/Custom designer menu, and use Load Descriptions... to load the
.cw
file. This file can be extended with new signals and slots, in case you added
some to your QGLViewer sub-class, in order to create a new custom widget description.
Here we use three slots and three signals (axis, grid and fps) to connect to and from the interface and the viewer.
#include <QGLViewer/qglviewer.h> class Viewer : public QGLViewer { public : Viewer(QWidget *parent, const char *name); protected : virtual void draw(); virtual QString helpString() const; };
#include "interface.h" #include <math.h> // Constructor must call the base class constructor. Viewer::Viewer(QWidget *parent, const char *name) : QGLViewer(parent, name) { restoreStateFromFile(); help(); } void Viewer::draw() { // Draws a spiral 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(); } QString Viewer::helpString() const { QString text("<h2>I n t e r f a c e</h2>"); text += "A GUI can be added to a QGLViewer widget using Qt's <i>designer</i>.<br><br>"; text += "You can install the QGLViewer designer plugin to make the QGLViewer appear as a "; text += "standard Qt widget in the designers' widget tabs. See installation pages for details.<br><br>"; text += "An other option is to add a <i>Custom Widget</i>. All the available QGLViewer's signals and slots are "; text += "listed in a <code>qglviewer.cw</code> (custom widget) file, located in the QGLViewer <code>include</code> directory."; return text; }
/**************************************************************************** ** Form interface generated from reading ui file 'myInterface.ui' ** ** Created: Thu Jul 7 10:55:22 2005 ** by: The User Interface Compiler ($Id: qt/main.cpp 3.3.3 edited Nov 24 2003 $) ** ** WARNING! All changes made in this file will be lost! ****************************************************************************/ #ifndef FORM1_H #define FORM1_H #include <qvariant.h> #include <qpixmap.h> #include <qwidget.h> class QVBoxLayout; class QHBoxLayout; class QGridLayout; class QSpacerItem; class Viewer; class QCheckBox; class QPushButton; class myInterface : public QWidget { Q_OBJECT public: myInterface( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 ); ~myInterface(); QCheckBox* FPSCheckBox; QCheckBox* AxisCheckBox; QPushButton* QuitButton; QCheckBox* GridCheckBox; Viewer* viewer; protected: QGridLayout* Form1Layout; protected slots: virtual void languageChange(); private: QPixmap image0; }; #endif // FORM1_H
#include "interface.h" #include "myInterface.h" #include <qapplication.h> int main(int argc, char** argv) { // Read command lines arguments. QApplication application(argc,argv); // Instantiate the interface. myInterface *main_window = new myInterface(NULL); // Make the viewer window visible on screen. main_window->setCaption("Interface Example"); main_window->show(); // Set the viewer as the application main widget. application.setMainWidget(main_window); // Run main loop. return application.exec(); }
/**************************************************************************** ** Form implementation generated from reading ui file 'myInterface.ui' ** ** Created: Thu Jul 7 10:55:22 2005 ** by: The User Interface Compiler ($Id: qt/main.cpp 3.3.3 edited Nov 24 2003 $) ** ** WARNING! All changes made in this file will be lost! ****************************************************************************/ #include "myInterface.h" #include <qvariant.h> #include <qcheckbox.h> #include <qpushbutton.h> #include <qlayout.h> #include <qtooltip.h> #include <qwhatsthis.h> #include "interface.h" /* * Constructs a myInterface as a child of 'parent', with the * name 'name' and widget flags set to 'f'. */ myInterface::myInterface( QWidget* parent, const char* name, WFlags fl ) : QWidget( parent, name, fl ) { if ( !name ) setName( "Form1" ); Form1Layout = new QGridLayout( this, 1, 1, 6, 2, "Form1Layout"); FPSCheckBox = new QCheckBox( this, "FPSCheckBox" ); Form1Layout->addWidget( FPSCheckBox, 1, 0 ); AxisCheckBox = new QCheckBox( this, "AxisCheckBox" ); Form1Layout->addWidget( AxisCheckBox, 1, 2 ); QuitButton = new QPushButton( this, "QuitButton" ); Form1Layout->addWidget( QuitButton, 1, 3 ); GridCheckBox = new QCheckBox( this, "GridCheckBox" ); Form1Layout->addWidget( GridCheckBox, 1, 1 ); viewer = new Viewer( this, "viewer" ); Form1Layout->addMultiCellWidget( viewer, 0, 0, 0, 3 ); languageChange(); resize( QSize(673, 438).expandedTo(minimumSizeHint()) ); clearWState( WState_Polished ); // signals and slots connections connect( QuitButton, SIGNAL( released() ), this, SLOT( close() ) ); connect( FPSCheckBox, SIGNAL( toggled(bool) ), viewer, SLOT( setFPSIsDisplayed(bool) ) ); connect( AxisCheckBox, SIGNAL( toggled(bool) ), viewer, SLOT( setAxisIsDrawn(bool) ) ); connect( GridCheckBox, SIGNAL( toggled(bool) ), viewer, SLOT( setGridIsDrawn(bool) ) ); connect( viewer, SIGNAL( FPSIsDisplayedChanged(bool) ), FPSCheckBox, SLOT( setChecked(bool) ) ); connect( viewer, SIGNAL( axisIsDrawnChanged(bool) ), AxisCheckBox, SLOT( setChecked(bool) ) ); connect( viewer, SIGNAL( gridIsDrawnChanged(bool) ), GridCheckBox, SLOT( setChecked(bool) ) ); } /* * Destroys the object and frees any allocated resources */ myInterface::~myInterface() { // no need to delete child widgets, Qt does it all for us } /* * Sets the strings of the subwidgets using the current * language. */ void myInterface::languageChange() { setCaption( tr( "Form1" ) ); FPSCheckBox->setText( tr( "FPS" ) ); AxisCheckBox->setText( tr( "Axis" ) ); QuitButton->setText( tr( "Quit" ) ); GridCheckBox->setText( tr( "Grid" ) ); }
Back to the examples main page.