Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to get notification of mouse down in menu bar?



  • i want one signal or notify callback the moment the user clicks in the menu bar, before any menus are actually shown, so i can do some quick pre-processing for some menu items. how do i do this?

    like menuBar::aboutToShow(), which doesn't exist? note i do not want the signal sent before EACH menu is shown, only once just before the user starts browsing the entire menu bar

    do i filter all events looking for a menu bar click? how would i detect that?

    note: i KNOW that's "not how it's done", i don't need suggestions for what else i should do, i just want to know if what i want is possible and if so, how?

    thanks!



  • @mpergand said in how to get notification of mouse down in menu bar?:

    You can retreive the mouse location with [event mouseLocation]

    But what i was saying is that the i don't GET an event for a click when the user clicks in the menu bar. Just nothing. So there is no way to ASK for for a mouse location.

    You can create an NSObject to receive the notification

    OMG that worked! YAY!



  • @davecotter
    You can try this.

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent), ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        ui->menuBar->installEventFilter(this);
    }
    
    bool MainWindow::eventFilter(QObject *obj, QEvent *event)
    {
        if(obj == ui->menuBar){
            if(event->type() == QEvent::MouseButtonPress){
                qDebug() << "Menu bar pressed";
                //Make post processing for menu
            } else if(event->type() == QEvent::MouseButtonRelease){
                qDebug() << "Menu bar released";
            }
    
        }
        return QObject::eventFilter(obj, event);
    }
    


  • @Gojir4

    I don't think it works on MacOs because the main menu is outside the window frame.
    I tryed with native event, I had a "mac_generic_NSEvent" by clicking on the main menu.
    Look promising , I didn't go any further anyway :)

    [edit]
    Got more Here

    bool CocoaNativeEventFilter::nativeEventFilter(const QByteArray &eventType, void *message, long *)
    {
        if (eventType == "mac_generic_NSEvent") {
            NSEvent *event = static_cast<NSEvent *>(message);
            if ([event type] == NSKeyDown) {
                // Handle key event
                qDebug() << QString::fromNSString([event characters]);
            }
        }
        return false;
    }
    


  • thanks for that.

    when i try to install that in my menubar object like this:

    ui->menuBar->installNativeEventFilter(s_nativeFilterP);
    

    I get this compile error:

    error: no member named 'installNativeEventFilter' in 'QMenuBar'
    

    so i attempt to install it in qApp:

    qApp->installNativeEventFilter(s_nativeFilterP);
    

    which compiles and runs, but the nativeFilter only gets called for all clicks NOT in the menu bar.

    but even if i get that to work, i still must then determine for myself if the user clicked in a menu bar which is complicated by the fact that each display may have a menu bar, i might miss a case.

    what i would like is to do is just hook into the existing OS notifications, something like this:

    void	InstallUpdateMenusEvenHandler()
    {
    	NSNotificationCenter*	notifyRef = [NSNotificationCenter defaultCenter];
    	NSApplication*		nsAppRef = [NSApplication sharedApplication];
    	
    	[notifyRef addObserver:nsAppRef selector: @selector(menuBarClicked:) name: NSMenuDidBeginTrackingNotification object: NULL];
    }
    

    but then how do i add menuBarClicked: to NSApplication? AFAIK it's not possible to subclass NSApp in Qt ?



  • @davecotter said in how to get notification of mouse down in menu bar?:

    which compiles and runs, but the nativeFilter only gets called for all clicks NOT in the menu bar.

    You can retreive the mouse location with [event mouseLocation]

    but then how do i add menuBarClicked: to NSApplication? AFAIK it's not possible to subclass NSApp in Qt ?

    You can create an NSObject to receive the notif and emit a signal from here (or use a custom event).



  • @mpergand said in how to get notification of mouse down in menu bar?:

    You can retreive the mouse location with [event mouseLocation]

    But what i was saying is that the i don't GET an event for a click when the user clicks in the menu bar. Just nothing. So there is no way to ASK for for a mouse location.

    You can create an NSObject to receive the notification

    OMG that worked! YAY!


Log in to reply