1
votes

I am handling a key pressed event in a widget by overloading a void keyPressEvent ( QKeyEvent* eventPtr ) method.

The problem is that when I launch two instances of my application (from the Qt Creator) only one of them effectively handles the key pressed event. Even when I set focus on the second window it does not receive the events. When I set focus on the first one it actually does receive the event.

All other functionality works fine when two applications are launched.

Is it an expected behavior or a known issue or maybe I am missing something?

UPD: I found out that some key presses (e.g. letters) are actually received by both windows. But only one window receives arrow key presses. When I create an instance of my widget I call widget->setFocus() on it. This makes the widget receive arrow key presses. But this does not work when two instances of the application are launched.

Maybe I should use event filter instead?

2
How do you know that the second application does not receive events?vahancho
@vahancho the code inside the keyPressEvent is being executed only in one instance of the application.Kolyunya
@vahancho also I can see in the debugger that the control does not reack the keyPressEvent because the breakpoint is not being reached.Kolyunya
I have doubts that you can debug two instances simultaneously with QtCreator. How do you start two instances in the same time and why?vahancho
@vahancho I do not debug two instances simultaneously. I launched one debug session and one regular one. And only regual one received the events. Well, lets forget about the debugging. When I just launched (non-debug) two instances of an app from Qt Creator it was obvious that only one of them received key pressed event because the visual effect which was programmatically executed inside event handler occurred only in one window. Also the qDebug() inside the event handler printed something only to the first window's console.Kolyunya

2 Answers

1
votes

As you didn't provide any code I'm not 100% sure that this is the cause of your problem, but if you start two instances of QtCreator, qDebug() will not produce any output in the second instance of QtCreator.
If you use that (or maybe cout / printf), then try doing something in the keyPressEvent handler which you can see in your application, like changing the background colour or displaying a message box.

Unless you installed a global key hook, and your keyPressEvent handler eats all the key events, there is no way one application would eat the events of the application which has the focus.

1
votes

When running a program from Qt Creator, you can see messages from qDebug in Qt Creator's "Application Output" window. However, if you run more than one program this way, at the same time, only one will be attached to output its messages there.

What you can do is run one from Qt Creator and the second from the command line using cmd.exe (Windows) or Terminal.app (OSX). This way, the 2nd program will output its qDebug messages in the command or terminal program window.