Get HWND on windows with Qt5 (from WId)

前端 未结 5 1184
梦如初夏
梦如初夏 2020-11-29 07:00

I am trying to convert a Qt4 Application to Qt5. The only thing I couldn\'t figure out is how to get the HWND of a Widget. The program uses EcWin7 to show the progr

5条回答
  •  無奈伤痛
    2020-11-29 07:49

    #include 
    
    static QWindow* windowForWidget(const QWidget* widget) 
    {
        QWindow* window = widget->windowHandle();
        if (window)
            return window;
        const QWidget* nativeParent = widget->nativeParentWidget();
        if (nativeParent) 
            return nativeParent->windowHandle();
        return 0; 
    }
    
    HWND getHWNDForWidget(const QWidget* widget)
    {
        QWindow* window = ::windowForWidget(widget);
        if (window && window->handle())
        {
            QPlatformNativeInterface* interface = QGuiApplication::platformNativeInterface();
            return static_cast(interface->nativeResourceForWindow(QByteArrayLiteral("handle"), window));
        }
        return 0; 
    }
    

提交回复
热议问题