问题
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
回答1:
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:
- Re-implement QApplication::macEventFilter() in your app
Disable Alien widgets for your app or for just the QMacNativeCocoaWidget by doing
setAttribute(Qt::WA_PaintOnScreen)
In macEventFilter(), check if the event is a MouseMove event:
NSEvent *e = reinterpret_cast<NSEvent *>(event); if ([e type] == NSMouseMoved)
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"]];
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!
回答2:
In my case I was using a native application with a NSPanel and an embedded WebView. The hover event was never sent even if the element (a button) was clicked. I solved the problem by overriding the following methods of the NSPanel class:
- (BOOL)canBecomeKeyWindow
{
return YES;
}
- (BOOL)isMainWindow
{
return YES;
}
- (BOOL)isKeyWindow
{
return ([NSApp isActive]) ? YES : [super isKeyWindow];
}
Make NSView in NSPanel first responder without key window status
来源:https://stackoverflow.com/questions/16191279/cocoa-webview-on-os-x-not-firing-mouse-hover-events-without-having-to-click-and