【问题标题】:Get HWND on windows with Qt5 (from WId)使用 Qt5 在 Windows 上获取 HWND(来自 WId)
【发布时间】:2012-12-12 11:34:49
【问题描述】:

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

程序调用 EcWin7.init(this->winId()); 我需要某种方式将此 ID 转换为 HWND id 或其他方式得到这个。

【问题讨论】:

  • 嗯,这很奇怪。 QWidget::winId() 应该在 Windows 上返回 HWND,就像在 Qt4 中一样。

标签: c++ windows qt user-interface qt5


【解决方案1】:

试试这个功能:QWindowsNativeInterface::nativeResourceForWindow

【讨论】:

    【解决方案2】:

    你可以试试:

    (HWND)QWidget::winId();
    

    【讨论】:

      【解决方案3】:
      #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; 
      }
      

      【讨论】:

      【解决方案4】:

      在 Qt5 中 winEventnativeEvent 取代:

      bool winEvent(MSG* pMsg, long* result)
      

      现在

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

      EcWin7::winEvent 中,您必须将void 转换为MSG

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

      我能够让应用程序运行!只需替换:

       mWindowId = wid;
      

       mWindowId = (HWND)wid;
      

      【讨论】:

      • 不幸的是,有很多错误。并非所有事件都传递给真正的小部件,其中很多都传递给顶级小部件。
      【解决方案5】:

      winId() 在 Qt 5.1 上为我工作 至少在我使用时它具有相同的价值

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

      qDebug() << winId();
      

      【讨论】:

        猜你喜欢
        • 2011-07-19
        • 2013-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-01
        • 2015-11-07
        相关资源
        最近更新 更多