Adding a graphical user interface around a QGLViewer
.
Qt's designer
has been used to create a very simple interface example, described by a
.ui
user interface description file.
Install the QGLViewer designer plugin so that the QGLViewer appears in the designer's widgets
tabs. You can then connect signals and slots to and from the viewer. The viewer is fully
functional and can be manipulated when you test your interface in designer
.
The plugin code is in the designerPlugin
directory. Start from there to create
plugins for the classes you will derive from QGLViewer. Select your architecture in the download page for details on the plugin compilation and installation.
With Qt version 2 or 3, 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, thus creating 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 : #if QT_VERSION < 0x040000 Viewer(QWidget *parent, const char *name); #else Viewer(QWidget *parent); #endif protected : virtual void draw(); virtual QString helpString() const; };
#include "interface.h" #include <math.h> // Constructor must call the base class constructor. #if QT_VERSION < 0x040000 Viewer::Viewer(QWidget *parent, const char *name) : QGLViewer(parent, name) #else Viewer::Viewer(QWidget *parent) : QGLViewer(parent) #endif { 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>. Signals and slots "; text += "can then be connected to and from the viewer.<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 (with Qt version 2 or 3) is to add a <i>Custom Widget</i> in designer. "; text += "All the available QGLViewer's signals and slots are listed in a <code>qglviewer.cw</code> "; text += "(custom widget) file, located in the QGLViewer <code>include</code> directory."; return text; }
/**************************************************************************** ** Form interface generated from reading ui file 'viewerInterface.Qt3.ui' ** ** Created: Tue Nov 28 11:18:38 2006 ** by: The User Interface Compiler ($Id: qt/main.cpp 3.3.4 edited Nov 24 2003 $) ** ** WARNING! All changes made in this file will be lost! ****************************************************************************/ #ifndef INTERFACE_H #define INTERFACE_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 ViewerInterface : public QWidget { Q_OBJECT public: ViewerInterface( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 ); ~ViewerInterface(); QCheckBox* FPSCheckBox; QCheckBox* AxisCheckBox; QPushButton* QuitButton; QCheckBox* GridCheckBox; Viewer* viewer; protected: QGridLayout* InterfaceLayout; protected slots: virtual void languageChange(); private: QPixmap image0; }; #endif // INTERFACE_H
#include <qapplication.h> #if QT_VERSION >= 0x040000 # include "ui_viewerInterface.Qt4.h" class ViewerInterface : public QDialog, public Ui::Dialog { public: ViewerInterface() { setupUi(this); } }; #else # include "interface.h" # if QT_VERSION >= 0x030000 # include "viewerInterface.Qt3.h" # else # include "viewerInterface.Qt2.h" # endif #endif int main(int argc, char** argv) { QApplication application(argc,argv); ViewerInterface vi; #if QT_VERSION < 0x040000 application.setMainWidget(&vi); #else vi.setWindowTitle("interface"); #endif vi.show(); return application.exec(); }
/**************************************************************************** ** Form implementation generated from reading ui file 'viewerInterface.Qt3.ui' ** ** Created: Tue Nov 28 11:18:54 2006 ** by: The User Interface Compiler ($Id: qt/main.cpp 3.3.4 edited Nov 24 2003 $) ** ** WARNING! All changes made in this file will be lost! ****************************************************************************/ #include "viewerInterface.Qt3.h" #include <qvariant.h> #include <qcheckbox.h> #include <qpushbutton.h> #include <qlayout.h> #include <qtooltip.h> #include <qwhatsthis.h> #include "interface.h" /* * Constructs a ViewerInterface as a child of 'parent', with the * name 'name' and widget flags set to 'f'. */ ViewerInterface::ViewerInterface( QWidget* parent, const char* name, WFlags fl ) : QWidget( parent, name, fl ) { if ( !name ) setName( "Interface" ); InterfaceLayout = new QGridLayout( this, 1, 1, 6, 2, "InterfaceLayout"); FPSCheckBox = new QCheckBox( this, "FPSCheckBox" ); InterfaceLayout->addWidget( FPSCheckBox, 1, 0 ); AxisCheckBox = new QCheckBox( this, "AxisCheckBox" ); InterfaceLayout->addWidget( AxisCheckBox, 1, 2 ); QuitButton = new QPushButton( this, "QuitButton" ); InterfaceLayout->addWidget( QuitButton, 1, 3 ); GridCheckBox = new QCheckBox( this, "GridCheckBox" ); InterfaceLayout->addWidget( GridCheckBox, 1, 1 ); viewer = new Viewer( this, "viewer" ); InterfaceLayout->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 */ ViewerInterface::~ViewerInterface() { // no need to delete child widgets, Qt does it all for us } /* * Sets the strings of the subwidgets using the current * language. */ void ViewerInterface::languageChange() { setCaption( tr( "Interface" ) ); FPSCheckBox->setText( tr( "FPS" ) ); AxisCheckBox->setText( tr( "Axis" ) ); QuitButton->setText( tr( "Quit" ) ); GridCheckBox->setText( tr( "Grid" ) ); }
Back to the examples main page.