Qt4版本的实现
方法1:
通过继承QWidget的类中重新实现winEvent接口,以接收在消息参数中传递的本机Windows事件。
bool QWidget::winEvent(MSG *message, long *result)
1
方法2:
通过继承QCoreApplication的类中重新实现winEventFilter接口,以接收在消息参数中传递的本机Windows事件。
bool QCoreApplication::winEventFilter(MSG *msg, long *result)
1
Qt5版本实现
方法1:
通过继承QWidget的类中重新实现winEvent接口,以接收在消息参数中传递的eventType标识的本机平台事件。
bool QWidget::nativeEvent(const QByteArray &eventType, void *message, long *result)
1
方法2:
通过继承QAbstractNativeEventFilter的类中重新实现nativeEventFilter接口:
bool QAbstractNativeEventFilter::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
1
并安装到中:
void QCoreApplication::installNativeEventFilter(QAbstractNativeEventFilter *filterObj)
1
或安装到:
void QAbstractEventDispatcher::installNativeEventFilter(QAbstractNativeEventFilter *filterObj)
1
特别地:不同平台对应的eventType类型有:
平台 事件类型(eventType) 消息类型(message) 结果类型(result)
Windows “windows_generic_MSG” MSG * LRESULT
macOs “NSEvent” NSEvent * 无
XCB(Linux) “xcb_generic_event_t” xcb_generic_event_t * 无
/******************************************************
继承Qt的基类QAbstractNativeEventFilter
class HHNativeEventFilter : public QAbstractNativeEventFilter
{
protected:
bool nativeEventFilter(const QByteArray &eventType, void *message, long *)
{
if (eventType == "windows_generic_MSG"
|| eventType == "windows_dispatcher_MSG")
{
PMSG msg = static_cast<PMSG>(message);
if(msg->message == WM_CLOSE )
{
qApp->exit();
}
}
return false;
}
};
利用QApplication注册类对象
app.installNativeEventFilter(new NativeEventFilter);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)