It's copy of KWindowSystem::setMainWindow, but it was missing
e7249deae417cdf9fb2983b06aa118c4ae4e22df:
Verify that QWindow::fromWinId returned a QWindow before using it
Foreign window support is not available on all platforms. E.g. on
Wayland this returns a nullptr. Thus we cannot use it.