admin 管理员组

文章数量: 1184232

介绍Qt4和Qt5获取Windows系统事件的方法。

Qt4版本的实现

方法1:

  • 通过继承QWidget的类中重新实现winEvent接口,以接收在消息参数中传递的本机Windows事件。
bool QWidget::winEvent(MSG *message, long *result)

方法2:

  • 通过继承QCoreApplication的类中重新实现winEventFilter接口,以接收在消息参数中传递的本机Windows事件。
bool QCoreApplication::winEventFilter(MSG *msg, long *result)

Qt5版本实现

方法1:

  • 通过继承QWidget的类中重新实现winEvent接口,以接收在消息参数中传递的eventType标识的本机平台事件。
bool QWidget::nativeEvent(const QByteArray &eventType, void *message, long *result)

方法2:

  • 通过继承QAbstractNativeEventFilter的类中重新实现nativeEventFilter接口:
bool QAbstractNativeEventFilter::nativeEventFilter(const QByteArray &eventType, void *message, long *result)

安装到中:

void QCoreApplication::installNativeEventFilter(QAbstractNativeEventFilter *filterObj)

或安装到:

void QAbstractEventDispatcher::installNativeEventFilter(QAbstractNativeEventFilter *filterObj)
  • 特别地:不同平台对应的eventType类型有:
平台事件类型(eventType)消息类型(message)结果类型(result)
Windows“windows_generic_MSG”MSG *LRESULT
macOs“NSEvent”NSEvent *
XCB(Linux)“xcb_generic_event_t”xcb_generic_event_t *

本文标签: 接口 事件 系统 QT Windows