0

I've a plugin that returns me a HWND of a window that it creates. I need to show that window inside a QWidget. I need to know how I can insert the window to the QWidget by knowing its HWND pointer.

I'm making an example by creating a Qt widget, taking its HWND and then attaching it to the other QWidget. The window that must be attached can be done with any gui library (opengl, wxwidgets) so it's only an example.

When I create the widget, I retrieve the HWND pointer with this code (m_mainWindow is a QMainWindow):

// adapted from https://stackoverflow.com/questions/14048565/get-hwnd-on-windows-with-qt5-from-wid
#include <qpa/qplatformnativeinterface.h>
// ...
HWND Application::getWindowHandle() const {
  HWND handle{ nullptr };
  QWindow* window = m_mainWindow->windowHandle();
  if (!window) {
    const QWidget* nativeParent = m_mainWindow->nativeParentWidget();
    if (nativeParent) {
      window = nativeParent->windowHandle();
    }
  }
  if (window && window->handle()) {
    QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
    handle = static_cast<HWND>(nativeInterface->nativeResourceForWindow(QByteArrayLiteral("handle"), window));
  }
  return handle;
}

Then I create a QWidget and I try to put the HWND to it with this code:

void PluginWidget::setWindowHandle(HWNDhandle) {
  create((WId)handle);
}

But it does not work. The window remains separated and it's not shown inside my PluginWidget.

What's the correct way to retrieve the HWND of a Qt window, and how can I attach correctly a window identified by HWND into another QWidget?

Jepessen
  • 11,744
  • 14
  • 82
  • 149
  • [`QWidget::createWindowContainer`](https://doc.qt.io/qt-5/qwidget.html#createWindowContainer) allows you to 'wrap' a `QWindow` which can, in turn, be created from an HWND. – G.M. Mar 17 '20 at 10:08
  • You might try calling the SetParent Windows API (after "create" call), by using your QWidget's HWND as hWndNewParent parameter and the plugin HWND for the hWndChild: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setparent – VuVirt Mar 17 '20 at 10:09

0 Answers0