1

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

2 Answers2

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!

Community
  • 1
  • 1
0

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

Bemipefe
  • 1,397
  • 4
  • 17
  • 30