QKeyEvent on whole applicacation [solved]
-
Hi Guys, I have simple question. How could I catch QKeyEvent in whole application (in all widgets)? In need to proccess key pressing in my application in one place (independently on what child widget is focused on). I know there is eventfiletr but I exactly dont know how to use it. Or if someone could send me link for tutorial about this topic, I'll be happy. Thanks David
-
You can subclass QApplication and reimplement QApplication::notify - to handle the events you are interested in globally:
@
bool MyApplication::notify(QObject *receiver, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
// Handle
}
return QApplication::notify(receiver, event);
}
@It should be noted that this is rather intrusive. Be sure to call the base implementation unless you are very convinced that you don't want to deliver the event to the proper receiver.
-
It is one of two different ways to process Qt events globally (at least to my knowledge) using the Qt API. The second way to achieve a similar result is to install an event filter on the QApplication instance you want to monitor. I must admit that I simply forgot to mention the second possibility :). The major advantage of the second way is that you can avoid subclassing QApplication. Which one is actually preferrable depends on the use case (and personal preference of course). I, for my part, do prefer the method I suggested. In my experience problems caused by erroneous event filters are often hard to spot.