[osg-users] MousepressEvent not able to capture from osg::Viewer by Qt

107 views
Skip to first unread message

Sujan Dasmahapatra

unread,
May 8, 2013, 1:04:59 AM5/8/13
to OpenSceneGraph Users
I am trying to implement a mouse press event with QT and OSG but unable to do it. Pls help.

Below is my ViewerWidget class which is derived from QWidget and osg::CompositeViewer......In the constructor I got gView which is a osg::GLWidget, I am trying override mousePressEvent but when I click message not coming. What I am doing wrong pls help..

My gView pointer is of class CSGraphicsView as below
/////////////////////////////////////////////////////////////////////////////////
#ifndef GVIEW_H
#define GVIEW_H
#include <osg\GraphicsContext>
#include <osgQt\GraphicsWindowQt>
#include <QtGui\qwidget.h>
#include <QtCore\qobject.h>
#include <QtGui\qmouseeventtransition.h>
class CSGraphicsView : public osgQt::GLWidget
{
public:
CSGraphicsView(QWidget* parent=0);
~CSGraphicsView();

virtual void mousePressEvent(QMouseEvent *e);
};

#endif // GVIEW_H


//Implementations is 
void CSGraphicsView::mousePressEvent(QMouseEvent *e)
{
QMessageBox msg;
msg.setText("Mouse pressed");
msg.exec();
}//But this msg is not coming when i am pressing mouse.
/////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////
ViewerWidget::ViewerWidget(osgViewer::ViewerBase::ThreadingModel threadingModel, osg::Group* scene) : QWidget()
    {
        setThreadingModel(threadingModel);

        gView = static_cast<CSGraphicsView*> (addViewWidget( createCamera(0,0,100,100), scene ));

        QGridLayout* grid = new QGridLayout;
        grid->addWidget( gView, 0, 0 );
        setLayout( grid );

        connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );
        _timer.start( 10 );
    }

osgQt::GLWidget* ViewerWidget::addViewWidget( osg::Camera* camera, osg::Group* scene )
{
osgViewer::View* view = new osgViewer::View;
        view->setCamera( camera );
        addView( view );
        
        view->setSceneData( scene );
        view->addEventHandler( new osgViewer::StatsHandler );
        view->setCameraManipulator( new osgGA::TrackballManipulator );
        
        gw = dynamic_cast<osgQt::GraphicsWindowQt*> ( camera->getGraphicsContext() ); 
        return gw ? gw->getGLWidget() : NULL;
}


osg::Camera* ViewerWidget::createCamera( int x, int y, int w, int h, const std::string& name, bool windowDecoration )
    {
        osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
        traits->windowName = name;
        traits->windowDecoration = windowDecoration;
        traits->x = x;
        traits->y = y;
        traits->width = w;
        traits->height = h;
        traits->doubleBuffer = true;
        traits->alpha = ds->getMinimumNumAlphaBits();
        traits->stencil = ds->getMinimumNumStencilBits();
        traits->sampleBuffers = ds->getMultiSamples();
        traits->samples = ds->getNumMultiSamples();
        
        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
        camera->setGraphicsContext( new osgQt::GraphicsWindowQt(traits.get()) );
        
        camera->setClearColor( osg::Vec4(0.2, 0.2, 0.6, 1.0) );
        camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );
        camera->setProjectionMatrixAsPerspective(
            30.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 1.0f, 10000.0f );
        return camera.release();
 }



void ViewerWidget::paintEvent( QPaintEvent* event )
{
frame();
}
/////////////////////////////////////////////////////////////////////////////////
--
Thanks & Regards
Sujan

Robert Milharcic

unread,
May 8, 2013, 3:59:00 AM5/8/13
to osg-...@lists.openscenegraph.org
On 8.5.2013 7:04, Sujan Dasmahapatra wrote:
> I am trying to implement a mouse press event with QT and OSG but
> unable to do it. Pls help.
>
> Below is my ViewerWidget class which is derived from QWidget and
> osg::CompositeViewer......In the constructor I got gView which is a
> osg::GLWidget, I am trying override mousePressEvent but when I click
> message not coming. What I am doing wrong pls help..
>
> [snip]
I don't know why this code didn't crash, I think it should. Anyway, you
should create an instance of the CSGraphicsView somewhere within
createCamera:

CSGraphicsView* gView = new CSGraphicsView;
gView->setGeometry(x, y, w, h);
traits->inheritedWindowData = new osgQt::GraphicsWindowQt::WindowData(
gView );

...
or you can pass an instance of the CSGraphicsView to the
osgQt::GraphicsWindowQt constructor.


Robert Milharcic
_______________________________________________
osg-users mailing list
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Sujan Dasmahapatra

unread,
May 8, 2013, 4:16:52 AM5/8/13
to OpenSceneGraph Users
Thanks Robert. It is working fine...

On the left mouse press I am showing a message using QMessagBox. But on the middle mouse press was having pan and right mouse press was having zoom in zoom out. How can I retain those functionalities. With this these functionalities are gone. 

void CSGraphicsView::mousePressEvent(QMouseEvent *e)
{
if(e->button()==Qt::LeftButton) 
{
QMessageBox msg;
msg.setText("Mouse pressed");
msg.exec();
}
}

Robert Milharcic

unread,
May 8, 2013, 5:40:40 AM5/8/13
to osg-...@lists.openscenegraph.org
On 8.5.2013 10:16, Sujan Dasmahapatra wrote:
Thanks Robert. It is working fine...

On the left mouse press I am showing a message using QMessagBox. But on the middle mouse press was having pan and right mouse press was having zoom in zoom out. How can I retain those functionalities. With this these functionalities are gone. 

void CSGraphicsView::mousePressEvent(QMouseEvent *e)
{
if(e->button()==Qt::LeftButton) 
{
QMessageBox msg;
msg.setText("Mouse pressed");
msg.exec();
}
}

Call base class implementation in a case where you need to retain original functionality:

void CSGraphicsView::mousePressEvent(QMouseEvent *e)
{
if(e->button()==Qt::LeftButton) 
{
QMessageBox msg;
msg.setText("Mouse pressed");
msg.exec();
}
        else
GLWidget::mousePressEvent(e);
}

Also, calling frame on paintEvent might not be enough. You should also call frame on every user input, on all mouse and keyboard events for example... or you can mount a QTimer and make it call frame at some interval.

Robert Milharcic

Sujan Dasmahapatra

unread,
May 8, 2013, 6:12:07 AM5/8/13
to OpenSceneGraph Users
Thats right Robert. Thanks a lot for your suggestions I appreciate it. Thanks Sujan


_______________________________________________
osg-users mailing list
osg-...@lists.openscenegraph.org
Reply all
Reply to author
Forward
0 new messages