Host a HWND component in a QML application

  • 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.

    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... }•

  • Hello,
    I can only share my experiences. My "teacher" was this example. Yes, you can get HWND of window only. You should implement paint() method like example and render your content into the area occupied by the element. Area for rendering can be chosen as glViewport(x(), windowHeight - y() - height(), width(), height());. But (as I've understood) QQuickWindow has only two time for rendering: beforeRendering & afterRendering: it mean you can have only top or bottom rendered item.
    Then, I've found second way for rendering: render to FBO and draw as QQuickFramebufferObject. More information is here.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.