Qt QML component like WPF HwndHost

We have a desktop windows application that uses the component that requires HWND

. In a WPF application, we use HwndHost

to render it. We are trying to make a Qt QML based application to do the same.

Can a component be placed HWND

in a QML application?

It works with QQuickWindow

, but the control I'm attaching takes up the entire area of ​​the application window. I would like to bind to a smaller scope like rectArea

in QML below. But a QQuickItem

does not have windId()

, but only its parent window()

. Maybe?
Here is my QML:

ApplicationWindow {
  width: 640
  height: 480
  visible: true
  Rectangle {
    objectName: "rectArea"
    id: rectangle1
    x: 0
    y: 0
    width: 200
    height: 200
    color: "#ffffff"


And here's a cpp snippet:

void setHwnd(QQmlApplicationEngine& m_engine) {
  auto root_objects = m_engine.rootObjects();
  m_rootObject = root_objects[0];
  auto rect_area = m_rootObject->findChild<QQuickItem*>("rectArea");
  HWND hWnd = reinterpret_cast<HWND>(rect_area->window()->winId());
  // use hWnd here, but it takes the entire window area...



source to share

2 answers

Once you have a QWindow (or whatever inherits from it), you can get the HWND by calling the winId

. You will need to do it like this:

QWindow pWindow;
// create pWindow
HWND hWnd = reinterpret_cast<HWND>(pWindow->winId());




A Rectangle

does not have its own HWND

. You can only get the HWND

whole window. You have to limit yourself to a rectangle, for example. when hosting an OpenGL app:

 glViewport(x(), windowHeight - y() - height(), width(), height());




All Articles