[osg-users] OSG 3.2.1 and Qt5 Widget integration

Pierre-Jean Petitprez pierre-jean.petitprez at inria.fr
Mon Aug 3 08:25:10 PDT 2015


Hi Sebastian,

I use Qt 5 and OSG 3.2.1 with no problem passing the key events from Qt 
to OSG.

In your Qt widget subclass you'll have a method which gets the key events:
void keyPressEvent(QKeyEvent* e)
{
         const char *keyData = e->text().toLatin1().data();
gw->getEventQueue()->keyPress(osgGA::GUIEventAdapter::KeySymbol(*keyData));
}
provided that gw is your osgViewer::GraphicsWindow.

Note that for the meta keys (like ALT, CTRL, SHIFT), you'll have to 
check something like:
  switch(e->key())
     {
     case Qt::Key_Shift:
         key = osgGA::GUIEventAdapter::KEY_Shift_L; //left key is chosen 
in an arbitrary way since Qt doesn't differ left or right
         break;
     case Qt::Key_Control :
         key = osgGA::GUIEventAdapter::KEY_Control_L;
         break;
     case Qt::Key_Alt :
         key = osgGA::GUIEventAdapter::KEY_Alt_L;
         break;
     }
since there is no "value" in e->text().

And the same can be achieved for the keyReleaseEvent, 
mousePress/ReleaseEvent, etc. by creating a new GUIEventAdapter from the 
Qt event and then send it to the event queue.

Hope that helps.

Cheers
Pierre-Jean


Le 03/08/2015 17:01, Sebastian Messerschmidt a écrit :
> Am 03.08.2015 um 16:57 schrieb Can Olcek:
>> Hi Sebastian,
>>
>> I have almost completed the example. My original implementation is a 
>> little bit complex than this. Thanks to the couple of private replies 
>> and discussion, I will post it tomorrow.
>>
>> But for keyboard inputs, I'm using an event filter.
>
> Okay, I know how to use EventFilters etc.
> The point is, that the qt4 implementation somehow passed the events to 
> OSG, while the qt5 seems to fail to pass any keys.
>
> Cheers
> Sebastian
>>
>> Something like this:
>>
>>
>> Code:
>>
>>
>> class QInputFilter : public QObject
>> {
>>    Q_OBJECT
>>
>> protected:
>>    bool eventFilter(QObject *obj, QEvent *event);
>>
>>    void onKeyPress(QKeyEvent *e);
>>    void onKeyRelease(QKeyEvent *e);
>> };
>>
>> bool QInputFilter::eventFilter(QObject *obj, QEvent *event)
>> {
>>    switch(event->type())
>>    {
>>      case QEvent::KeyPress:
>>        onKeyPress(static_cast<QKeyEvent *>(event));
>>        break;
>>      case QEvent::KeyRelease:
>>        onKeyRelease(static_cast<QKeyEvent *>(event));
>>        break;
>>    }
>>      return QObject::eventFilter(obj, event);
>> }
>>   void QInputFilter::onKeyPress(QKeyEvent *e)
>> {
>>    if(e->isAutoRepeat())
>>    {
>>      e->ignore();
>>      return;
>>    }
>>      unsigned int key = e->key();
>>    // add pressed keys and add changed keys for current frame
>>    // renderwidget will clear changed keys at the end of frame
>>    Input::PrivateAccess::pressedKeys().insert(key);
>>    Input::PrivateAccess::changedKeys().insert(key);
>>          e->accept();
>> }
>>   void QInputFilter::onKeyPress(QKeyEvent *e)
>> {
>>    if(e->isAutoRepeat())
>>    {
>>      e->ignore();
>>      return;
>>    }
>>      unsigned int key = e->key();
>>    // remove released keys and add changed keys for current frame
>>    // renderwidget will clear changed keys at the end of frame
>>    Input::PrivateAccess::pressedKeys().erase(e->key());
>>    Input::PrivateAccess::changedKeys().insert(e->key());
>>
>>    e->accept();
>> }
>>
>>
>>
>>
>>
>> Add input listener to your Qt app:
>>
>>
>> Code:
>>
>>
>> sdt::Author::QInputFilter inputFilter;
>> app.installEventFilter(&inputFilter);
>>
>>
>>
>>
>>
>> I have almost fully static Input class to access keys and mouse 
>> states during each frame (paintGL()) I've actually tried to implement 
>> Unity3D like approach so inside cull or update traversal I can use 
>> Input::getButton(), Input::getKey(), Input::isKeyUp(), etc. methods.
>>
>> I can add full implemention of this to my full example.
>>
>> Cheers,
>> Can
>>
>> ------------------
>> Read this topic online here:
>> http://forum.openscenegraph.org/viewtopic.php?p=64591#64591
>>
>>
>>
>>
>>
>> _______________________________________________
>> osg-users mailing list
>> osg-users at lists.openscenegraph.org
>> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org 
>>
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org




More information about the osg-users mailing list