[osg-users] OSG 3.2.1 and Qt5 Widget integration

Can Olcek can.olcek at gmail.com
Mon Aug 3 07:57:27 PDT 2015


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.

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








More information about the osg-users mailing list