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?