I'm pretty new to OSG and Qt, and have a few questions.  I'm basically
trying to integrate OSG with Qt (similar to SoQt for Coin3D).  I tried
the SimpleViewerQT example that I found online, and a number of lines
of code needed to be changed (I'm working with Qt v.4).  I'm posting
all the code I have (a slightly modified SimpleViewerQT), with
questions at the end of this email:

#include <QtOpenGL>
#include <QWidget>
#include <osgProducer/Viewer>
#include <osgGA/TrackballManipulator>

class GraphicsWindowQT : public QGLWidget, virtual public osgProducer::Viewer
{
public:

   GraphicsWindowQT(QWidget* parent=0, const QGLWidget*
sharedWidget=0, Qt::WindowFlags f=0);
        virtual ~GraphicsWindowQT() {}

protected:

   virtual void resizeGL( int width, int height );
   virtual void keyPressEvent( QKeyEvent* event );
   virtual void keyReleaseEvent( QKeyEvent* event );
   virtual void mousePressEvent( QMouseEvent* event );
   virtual void mouseReleaseEvent( QMouseEvent* event );
   virtual void mouseMoveEvent( QMouseEvent* event );

   QTimer _timer;
};

--------

#include "GraphicsWindowQT.h"

GraphicsWindowQT::GraphicsWindowQT(QWidget* parent, const QGLWidget*
sharedWidget, Qt::WindowFlags f)
:QGLWidget(parent, sharedWidget, f)
{
   connect(&_timer, SIGNAL(timeout()), this, SLOT(updateGL()));
   _timer.start(10);
}

void GraphicsWindowQT::resizeGL( int width, int height )
{
   getEventQueue()->windowResize(0, 0, width, height );
}

void GraphicsWindowQT::keyPressEvent( QKeyEvent* event )
{
   getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol)
event->key() );
}

void GraphicsWindowQT::keyReleaseEvent( QKeyEvent* event )
{
   getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol)
event->key() );
}

void GraphicsWindowQT::mousePressEvent( QMouseEvent* event )
{
   int button = 0;
   switch(event->button())
   {
       case(Qt::LeftButton): button = 1; break;
       case(Qt::MidButton): button = 2; break;
       case(Qt::RightButton): button = 3; break;
       case(Qt::NoButton): button = 0; break;
       default: button = 0; break;
   }
   getEventQueue()->mouseButtonPress(event->x(), event->y(), button);
}

void GraphicsWindowQT::mouseReleaseEvent( QMouseEvent* event )
{
   int button = 0;
   switch(event->button())
   {
       case(Qt::LeftButton): button = 1; break;
       case(Qt::MidButton): button = 2; break;
       case(Qt::RightButton): button = 3; break;
       case(Qt::NoButton): button = 0; break;
       default: button = 0; break;
   }
   getEventQueue()->mouseButtonRelease(event->x(), event->y(), button);
}

void GraphicsWindowQT::mouseMoveEvent( QMouseEvent* event )
{
   getEventQueue()->mouseMotion(event->x(), event->y());
}

--------

#include "GraphicsWindowQT.h"

class SimpleViewerQT :  public GraphicsWindowQT, virtual public
osgProducer::Viewer
{
   public:

       SimpleViewerQT(QWidget* parent=0, const QGLWidget*
sharedWidget=0, Qt::WindowFlags f=0)
                :GraphicsWindowQT(parent, sharedWidget, f)
                {}

       virtual void initializeGL()
       {
           QGLWidget::initializeGL();
       }

       virtual void paintGL()
       {
                        realize();
           osgProducer::Viewer::frame();
       }

};

--------

#include <iostream>
#include <QApplication>
#include <osgDB/ReadFile>

#include "SimpleViewerQT.h"

int main( int argc, char **argv )
{
   QApplication a( argc, argv );

   if (argc<2)
   {
       std::cout << argv[0] <<": requires filename argument." << std::endl;
       return 1;
   }

   // load the scene.
   osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
   if (!loadedModel)
   {
       std::cout << argv[0] <<": No data loaded." << std::endl;
       return 1;
   }


   SimpleViewerQT* viewerWindow = new SimpleViewerQT;

   viewerWindow->setSceneData(loadedModel.get());
   viewerWindow->addCameraManipulator(new osgGA::TrackballManipulator);

   viewerWindow->show();
   a.connect( &a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()) );

   return a.exec();
}

--------

Questions... the original code had this in main:

viewerWindow->setCameraManipulator(new osgGA::TrackballManipulator);

There is no setCameraManipulator method associated with any of the
base or derived classes.  But this leads to my second question... I
don't have an osgViewer framework (I'm working under Mac OS X), I only
have osgProducer, which is where the Viewer class resides.  Has
osgViewer been removed, but people are still using it (I've seen posts
from this year and last year where osgViewer is being used)?  Since I
changed all instances of osgViewer:Viewer to osgProducer::Viewer, is
this causing the unfound call to the setCameraManipulator method in
the original code?  I have a program that compiles and executes, but I
can't manipulate the camera.  Oh, and the call to realize() had to be
added to the paintGL method in the SimpleViewerQT class (I only know
this because OSG screamed at me during runtime).

If there's a much simpler example, I'd love to see it.

Thanks.
_______________________________________________
osg-users mailing list
[email protected]
http://openscenegraph.net/mailman/listinfo/osg-users
http://www.openscenegraph.org/

Reply via email to