Cocoa WebView on os X not firing mouse hover events without having to click and hold left mouse key

我的梦境 提交于 2020-01-05 08:17:30

问题


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:

  1. Re-implement QApplication::macEventFilter() in your app
  2. Disable Alien widgets for your app or for just the QMacNativeCocoaWidget by doing

    setAttribute(Qt::WA_PaintOnScreen)
    
  3. In macEventFilter(), check if the event is a MouseMove event:

    NSEvent *e = reinterpret_cast<NSEvent *>(event);
    if ([e type] == NSMouseMoved)
    
  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!