使用Qt5在Windows上获取HWND(来自WId)

Jos*_*sef 31 c++ windows user-interface qt qt5

我正在尝试将Qt4应用程序转换为Qt5.我唯一想知道的是如何获得Widget 的HWND.该程序使用EcWin7显示win 7+上任务栏图标的进度,但需要一个HWND.在将Q_WS_WIN更改为Q_OS_WIN之后,lib本身似乎编译正常.在Windows上的Qt4中,WId只是HWND的typedef ,所以这没问题.在Qt5中,情况不再如此.我找到了一些可以提供线索的邮件列表发布,但似乎QPlatformNativeInterface不再Qt5的公共API的一部分了.

该程序调用EcWin7.init(this-> winId()); 我需要某种方式将此ID转换为HWND ID或其他方式来获取此ID.

MrE*_*mar 23

在Qt5 winEvent被替换为nativeEvent:

bool winEvent(MSG* pMsg, long* result)
Run Code Online (Sandbox Code Playgroud)

就是现在

bool nativeEvent(const QByteArray & eventType, void * message, long *result)
Run Code Online (Sandbox Code Playgroud)

EcWin7::winEvent你必须施展voidMSG:

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

我能够让应用程序工作!只需更换:

 mWindowId = wid;
Run Code Online (Sandbox Code Playgroud)

 mWindowId = (HWND)wid;
Run Code Online (Sandbox Code Playgroud)


Kin*_*gon 11

#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; 
}
Run Code Online (Sandbox Code Playgroud)

  • 佐证,并进一步详细说明如何编译:http://lists.qt-project.org/pipermail/interest/2013-June/007650.html (3认同)