Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to get notification of mouse down in menu bar?

how to get notification of mouse down in menu bar?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.7k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    davecotter
    wrote on last edited by davecotter
    #1

    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!

    Gojir4G 1 Reply Last reply
    0
    • D Offline
      D Offline
      davecotter
      wrote on last edited by
      #6

      @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!

      1 Reply Last reply
      0
      • D davecotter

        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!

        Gojir4G Offline
        Gojir4G Offline
        Gojir4
        wrote on last edited by
        #2

        @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);
        }
        
        M 1 Reply Last reply
        1
        • Gojir4G Gojir4

          @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);
          }
          
          M Offline
          M Offline
          mpergand
          wrote on last edited by mpergand
          #3

          @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;
          }
          
          1 Reply Last reply
          1
          • D Offline
            D Offline
            davecotter
            wrote on last edited by davecotter
            #4

            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 ?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mpergand
              wrote on last edited by mpergand
              #5

              @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).

              1 Reply Last reply
              1
              • D Offline
                D Offline
                davecotter
                wrote on last edited by
                #6

                @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!

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved