An issue with embedding in Qt, an external window

Another very specific issue that I encountered while working on tooling for White Rabbit Engine. This one cost me three evenings to figure out :( 

For a while in the tooling, we are using a multi-process approach:

Hub (Qt application) <-> Client (Qt application)

Hub is the main process using QT. Inside it, we have QMainWindow to which we embed the client window. The code to do that is very simple:

auto clientWindow = QWindow::fromWinId(clientWindowsId);
auto widget = QWidget::createWindowContainer(
clientWindow, this);
setCenterWidget(widget);

You can find this example almost everywhere. The problem I encountered was that my client window had keyboard input only once when a window was created. If I lost focus nothing related to the keyboard worked. Neither shortcuts, neither textboxes, nothing. 

While investigating I noticed that QWidget::createWindowContainer internally created QWindowContainer that never received QEvent::FocusIn which I assumed was a problem. A little bit of debugging I figured out that I could just add to the creation process:

setFocusProxy(widget);

This resolved my issue and QWindowContainer stated receiving QEvent::FocusIn. Sadly the keyboard input was still not there. This leads me to a dead end. I was trying to find some answers on the internet (I even just now checked if AI would help me but no luck). 

In the act of desperation, I decided to play with the flow of the client app: 
  1. Create window
  2. Connect to host
  3. Setup window
  4. Show window
This setup was to ensure that the setting up of the window would happen outside of the vision for the user. Otherwise, they would see a window popping up and then moving to its final position. But well it was worth trying if showing a window before connecting to the host would change something. As you probably guess it did. Turned out that because for some reason this helped. Why? I'm not really sure. 

I have only some suspicions. 
  • Maybe code to show the window creates some stuff that is needed for the proper functioning of keyboard focus.
  • Maybe the embedding code stored some state for the window (I had not seen it in qwindowcontainer.cpp but who knows)
Still, after three days of fighting with this code, I'm satisfied that it worked. I'm a little bit sad that I don't know the exact reason why it does not work (if any of you know I would be supper happy to listen to the answer :) ) but sadly we need to choose our fights. There is the rest of the engine to take care of.

To add a small cherry on top I tried to run the client in a minimized state, just to not show the window setup and it worked perfectly fine. I have consistent keyboard focus while switching between sub-windows. 

It only cost us 3 days of work that we could spend on some other things馃槶

Comments

Popular posts from this blog

Query commands execution

W.U. 0x21

WRE