Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting QML-Item winId/Handle from C++ Code
Forum Updated to NodeBB v4.3 + New Features

Getting QML-Item winId/Handle from C++ Code

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gilad Reich
    wrote on 31 Aug 2018, 14:15 last edited by
    #1

    What's the best approach of getting a QML handle (i.e. winId in QWidgets) of specific QML-Item from C++ code? in Qt Widgets I could simply retrieve the handle of any widget in order to draw with graphics api(In my case DirectX).
    Say we have this QML code:

    Window {
    id: rootWindow
    visible: true
    width: 640
    height: 480
    
    RendererForm {
    objectName: "rendererForm"
    id: rendererForm
    x: 120
    y: 35
    width:600
    }
    }
    

    And I tried with C++ to do so:

    QObject * window = engine.rootObjects().first();
    QObject * rect = window->findChild<QObject*>("rendererForm");
    if (rect)
    {
    QWindow * rectW = qobject_cast<QWindow *>(rect);
    if (rectW)
    {
    qDebug() << "hmmmmmmmm";
    qDebug() << rectW->winId();
    }
    }
    

    Sadly this won't work. I'm kind of getting into QML lately, so if someone can enlighten me, that would be amazing. Thanks in advance!

    1 Reply Last reply
    0
    • W Offline
      W Offline
      wrosecrans
      wrote on 1 Sept 2018, 22:21 last edited by
      #2

      I don't think there's any guarantee that anything except the top level Window will have anything like a native handle. Even with QtWidgets there's no guarantee that the individual widgets will all have native handles, unless you specifically poke them with WinId(). The composition modes and interactions between QML items don't map as well to native API definitions, so it makes even less sense. In-general, QML is pretty tied to OpenGL to trying to draw into the same Window with QML and DirectX will probably be difficult.

      Would it be possible to render offscreen using your native draw API's, and then use OpenGL texture interop so that your QML item just draws the result image as a texture on the QML side? I don't really have DirectX experience so my understanding of the interop is theoretical, but if you can render to a IDirect3DSurface9, https://www.khronos.org/registry/OpenGL/extensions/NV/WGL_NV_DX_interop.txt says you can access it in OpenGL as a 2D texture.

      1 Reply Last reply
      2
      • G Offline
        G Offline
        Gilad Reich
        wrote on 4 Sept 2018, 06:49 last edited by
        #3

        @wrosecrans said in Getting QML-Item winId/Handle from C++ Code:

        unless you specifically poke them with WinId().

        Thanks for the answer! But how would I poke them with WinId()? I'm kind of new to QML, I guess if it will make things harder, I'll stick to QWidgets application and use QDeclarative to use QML in certain places.

        Drawing with DirectX using the WinId did work for me pretty well, but I'm kind of trying to force it to draw in the center of the Rectangle/Item so I can show some properties aside with the renderer form.

        1 Reply Last reply
        0

        1/3

        31 Aug 2018, 14:15

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved