4

We have a desktop windows application that uses a component that needs a HWND to be displayed. In a WPF application we use a HwndHost to display it. We are trying to make a Qt QML based application to do the same.

Is it possible to host a HWND component in a QML application?

It works with a QQuickWindow, but the control I attach takes the entire window application area. I would like to bind to a smaller area, like the rectArea in the QML below. But a QQuickItem does not have a windId(), only its parent window(). It is possible?
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 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...
}
Stefano Piovesan
  • 1,185
  • 3
  • 19
  • 37

2 Answers2

2

Once you have a QWindow (or anything that inherits from it), you can obtain HWND by calling winId method. You will need to typecast it like this:

QWindow pWindow;
// create pWindow
HWND hWnd = reinterpret_cast<HWND>(pWindow->winId());
seva titov
  • 11,720
  • 2
  • 35
  • 54
0

A Rectangle does not have its own HWND. You can only retrieve the HWND for the whole window. You have to restrict yourself to the rectangle are, e.g. when hosting a OpenGL application:

 glViewport(x(), windowHeight - y() - height(), width(), height());
Vertexwahn
  • 7,709
  • 6
  • 64
  • 90