Cocoa WebView on os X不触发鼠标悬停事件,而不必单击并按住鼠标左键 [英] Cocoa WebView on os X not firing mouse hover events without having to click and hold left mouse key
问题描述
我是新的Mac开发,我试图结合使用Cocoa WebView使用QMacCocoaViewContainer。我有视图加载我的HTML文件与多个CSS和JavaScript文件,但我遇到的问题是,鼠标悬停事件不会触发,当用户将鼠标移动视图。
I am new to Mac development and am trying to incorporate the use of a Cocoa WebView using the QMacCocoaViewContainer. I have the view loading my html file with multiple css and javascript files but the issue I am having is that the mouse hover events are not triggered when the user moves the mouse over the view.
我已经确定,如果用户单击并按住鼠标左键并移动鼠标,则会触发事件。我猜这是一个焦点问题,但没有成功解决这个问题。任何帮助将是伟大的
I have identified that if the user clicks and holds the left mouse button and moves the mouse then the events are triggered. I am guessing that it is a focus issue but have had no success in resolving this issue. Any help would be great
推荐答案
我发现一个类似的问题SO:嵌入可可webview到QT应用程序的事件问题,其中有一个答案草拟了。我可以确认在该答案的解决方案工作,但只是暗示。这是我做的:
I found a similar question on SO: Event issue when embed cocoa webview to QT application which has an answer sketched out. I can confirm that the solution in that answer works, but is only hinted at. Here is what I did:
- 在应用程式中重新导入QApplication :: macEventFilter()
-
通过执行
- Re-implement QApplication::macEventFilter() in your app
Disable Alien widgets for your app or for just the QMacNativeCocoaWidget by doing
setAttribute(Qt::WA_PaintOnScreen)
在macEventFilter()中,检查事件是否为MouseMove事件:
In macEventFilter(), check if the event is a MouseMove event:
NSEvent *e = reinterpret_cast<NSEvent *>(event);
if ([e type] == NSMouseMoved)
如果坐标位于WebView的边界中,然后将MouseMoved通知发送到通知中心:
If so, check if the coordinates are in the bounds of the WebView you have, and then post a MouseMoved notification to the Notification Center:
[[NSNotificationCenter defaultCenter]
postNotificationName:@"NSMouseMovedNotification" object:nil
userInfo:[NSDictionary dictionaryWithObject:e forKey:@"NSEvent"]];
检查事件的位置是否在WebView,记住Cocoa坐标的起点在底部,而在Qt(0,0)是左上角!
When checking if the event's position is in your WebView, remember that Cocoa coordinates have the origin at the bottom, while in Qt (0, 0) is top-left!
这篇关于Cocoa WebView on os X不触发鼠标悬停事件,而不必单击并按住鼠标左键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!