Get HWND on windows with Qt5 (from WId)

前端 未结 5 1183
梦如初夏
梦如初夏 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:43

    winId() worked for me on Qt 5.1 at least it has the same value when I'm using

    bool Widget::nativeEvent(const QByteArray & eventType, void * message, long * result)
    {
        MSG* msg = reinterpret_cast<MSG*>(message);
        qDebug() << msg->hwnd;
    
        return false;
    }
    

    and

    qDebug() << winId();
    
    0 讨论(0)
  • 2020-11-29 07:49
    #include <QtGui/5.0.0/QtGui/qpa/qplatformnativeinterface.h>
    
    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<HWND>(interface->nativeResourceForWindow(QByteArrayLiteral("handle"), window));
        }
        return 0; 
    }
    
    0 讨论(0)
  • 2020-11-29 08:01

    You may try:

    (HWND)QWidget::winId();
    
    0 讨论(0)
  • 2020-11-29 08:02

    Try this function: QWindowsNativeInterface::nativeResourceForWindow

    0 讨论(0)
  • 2020-11-29 08:05

    In Qt5 winEvent was replaced by nativeEvent:

    bool winEvent(MSG* pMsg, long* result)
    

    is now

    bool nativeEvent(const QByteArray & eventType, void * message, long *result)
    

    And in EcWin7::winEvent you have to cast void to MSG:

    bool EcWin7::winEvent(void * message, long * result)
    {
        MSG* msg = reinterpret_cast<MSG*>(message);
        if (msg->message == mTaskbarMessageId)
        {
          ...
    

    I was able to get the application to work! Just replace:

     mWindowId = wid;
    

    with

     mWindowId = (HWND)wid;
    
    0 讨论(0)
提交回复
热议问题