2

I’m newbie with Qt, and experiencing one problem I cannot deal with for, like, a month. The situation is like this: I’ve OpenSceneGraph project (which is OpenGL) and trying to make Qt interface inside the 3d scene. I think its not necessary how I deal with that, but if someone wants to know more here is thread with more info on OSG forum (though I didnt get solution there). The problem is, when any key on keyboard is clicked, Qt controls jump around the screen and dont react on any (mouse or keyboard) events anymore. The entire program continues to work, though. To summarize, my question is like: is there a way to make Qt widgets ignore all keypresses? I’ve searched a lot, but couldnt find any working solution.

Thanks in advance!

Kilazi
  • 308
  • 3
  • 14

1 Answers1

2

Read a bit about events in Qt. There is a section about event filtering (but please don't jump straight to it :P).

SHORT ANSWER :

void Qwidget::setEnabled ( bool );

The drawback is that it disable also mouse events, change the widget style and that's a bummer.

LONG ANSWER : FILTER EVENTS

One possibility is to filter all events on the Qt application. I suppose the function which launch your Qt code looks like this (if different post here):

int main(int argc, char* argv[]){
    QApplication app(argc, argv);
    QWidget toplevelwidget1;
    toplevelwidget1.show()
    //stufff
    return app.exec();
} 
//doesnt have to exactly like this.

you can to set an event filter on app variable. It is the more elegant solution but it is too complicated because it filters native events and will require some work...

What you can do instead is filter only your top level widgets or windows (the one without parents). You define an event filter (which is a QObject) like :

class KeyboardFilter: public QObject
{
   Q_OBJECT
   ...

   protected:
     bool eventFilter(QObject *obj, QEvent *event);
 };

  bool KeyboardFilter::eventFilter(QObject *obj, QEvent *event)
  {
      //for all events from keyboard, do nothing
      if (event->type() == QEvent::KeyPress || 
          event->type() == QEvent::KeyRelease ||
          event->type() == QEvent::ShortcutOverride ||
          ) {
          return true;
      } else {
        // for other, do as usual (standard event processing)
        return QObject::eventFilter(obj, event);
      }
  }

Then you set the filter on the desired widgets using:

 myDesiredWidgetorObject->installEventFilter(new KeyboardFilter(parent));

And that's it!

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
  • Thanks for the quick answer. I've read the article and tried your "Long answer" solution. Though it did not work, probably I'm missing something: what is meant in your "..." in class declaration? And other thing, how come the "new KeyboardFilter(parent)" has some parent, if it's meant to be used to widgets who have no parent? :P One thing is bothering me also: keypress makes widget jump into one place of a screen not simulatesonly, but one by one (one click - button jumps, other click - other button jumps unto prevous one). Anyway, thank for this answer, atleast I have something to learn now! – Kilazi Oct 17 '12 at 12:25
  • Didn't work? I proposed an idea of solution, not something which will compile directly and work... One solution is to be conservative, and use this filter on all the widgets you create regardless of top level or not. It `should` work. – UmNyobe Oct 17 '12 at 12:52
  • 1) the `...` is just a way to say blah-blah-blah. 2)The parent thing is a separate concept and `KeyboardFilter` is a subclass of QObject, so he can have a parent. Now you can set it to null if you want but it depends on your code. Whether the widgets have a parent or not is not relevant... 3) these `button jumps` are really weird,nothign can be said without seeing the source code. – UmNyobe Oct 17 '12 at 12:54
  • Interesting fact: even when I set widget->setEnabeled(false), and click on keyboard button, still same bug. So i dont think anything I can do with widget will help. – Kilazi Oct 18 '12 at 10:44