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. Mouse and QMenuBar
Qt 6.11 is out! See what's new in the release blog

Mouse and QMenuBar

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 822 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.
  • O Offline
    O Offline
    Oumayma
    wrote on last edited by
    #1

    Hello, I would like to know if there is any to disable the mouse events that occur when clicking on one of the menu options. As you can see in the image I have a white square that highlights the options of my menubar but when this happens if I move the mouse and place it on top of some option of my menubar and click to open the submenu as seen in the image. I would like this not to happen.
    Captura de pantalla (32).png

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      You want the top menus, NOT open the sub menu when clicked ?
      How will user then access the sub item ?

      O 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        You want the top menus, NOT open the sub menu when clicked ?
        How will user then access the sub item ?

        O Offline
        O Offline
        Oumayma
        wrote on last edited by
        #3

        @mrjj I want that , if I can deactivate it for a period of time, not forever.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Why not just disable the menu ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          O 1 Reply Last reply
          1
          • SGaistS SGaist

            Hi,

            Why not just disable the menu ?

            O Offline
            O Offline
            Oumayma
            wrote on last edited by
            #5

            @SGaist I had thought about that but the functionality that I have in the application does not help me to disable the menu.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Can you give more details so that we can devise a solution ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              O 1 Reply Last reply
              0
              • SGaistS SGaist

                Can you give more details so that we can devise a solution ?

                O Offline
                O Offline
                Oumayma
                wrote on last edited by
                #7

                @SGaist My application has two access modes: the conventional one with mouse and keyboard, and sweep mode where the menu options are highlighted with a QRubberband. For the second mode I use the preesmouseevent method so that the menus are displayed or an action is executed, that is why I want the conventional mouse events to not work when the user is in this mode.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Then one possible way would be to have a transparent widget on top of your UI that would block the interaction when in your swipe mode.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  O 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Then one possible way would be to have a transparent widget on top of your UI that would block the interaction when in your swipe mode.

                    O Offline
                    O Offline
                    Oumayma
                    wrote on last edited by
                    #9

                    @SGaist Another idea that has occurred to me is to block the movement of the mouse, when it is in sweep mode put the mouse cursor in the middle of the screen and that it cannot move from there but I don't know if this is possible.

                    mrjjM 1 Reply Last reply
                    0
                    • O Oumayma

                      @SGaist Another idea that has occurred to me is to block the movement of the mouse, when it is in sweep mode put the mouse cursor in the middle of the screen and that it cannot move from there but I don't know if this is possible.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #10

                      @Oumayma

                      Hi
                      You can use
                      https://doc.qt.io/qt-5/qwidget.html#grabMouse
                      to have all events go to some widget, effectively making it impossible to click on anything else.
                      However, it offers no control of what can is eaten. and if you fail to to ungrab as many times as you grab,
                      it will still be grapped. Also docs says:
                      "Warning: Bugs in mouse-grabbing applications very often lock the terminal. Use this function with extreme caution, and consider using the -nograb command line option while debugging."

                      Not the best solution, imho.

                      So another solution is to use an event filter.

                      // the filter class
                      #include <QEvent>
                      #include <QMouseEvent>
                      
                      class MouseEater : public QObject
                      {
                          Q_OBJECT
                          bool sweepmodeActive{0};
                      protected:
                          bool eventFilter(QObject *obj, QEvent *event)
                          {
                              if (event->type() == QEvent::MouseButtonPress || event->type() ==  QEvent::MouseButtonDblClick) {
                                  QMouseEvent *mp = static_cast<QMouseEvent *>(event);
                                  qDebug() << "MouseButtonPress detected" << sweepmodeActive;
                                  if ( mp->button() == Qt::LeftButton  ) {
                                      return sweepmodeActive; // say we handled it (true)
                                  }
                              }
                              return QObject::eventFilter(obj, event);// normal forward
                          }
                      public slots:
                          void SweepModeToggle( bool status)
                          {
                              sweepmodeActive = status;
                              qDebug() << "sweepmode" << status;
                          }
                      };
                      
                      // in main window 
                       MouseEater *me = new MouseEater();
                       connect(ui->pushButton, &QPushButton::toggled, me, &MouseEater::SweepModeToggle ); // to toogle sweep mode
                       ui->menubar->installEventFilter(me); // put filter ont he menubar
                      

                      the result is this.

                      alt text

                      O 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        @Oumayma

                        Hi
                        You can use
                        https://doc.qt.io/qt-5/qwidget.html#grabMouse
                        to have all events go to some widget, effectively making it impossible to click on anything else.
                        However, it offers no control of what can is eaten. and if you fail to to ungrab as many times as you grab,
                        it will still be grapped. Also docs says:
                        "Warning: Bugs in mouse-grabbing applications very often lock the terminal. Use this function with extreme caution, and consider using the -nograb command line option while debugging."

                        Not the best solution, imho.

                        So another solution is to use an event filter.

                        // the filter class
                        #include <QEvent>
                        #include <QMouseEvent>
                        
                        class MouseEater : public QObject
                        {
                            Q_OBJECT
                            bool sweepmodeActive{0};
                        protected:
                            bool eventFilter(QObject *obj, QEvent *event)
                            {
                                if (event->type() == QEvent::MouseButtonPress || event->type() ==  QEvent::MouseButtonDblClick) {
                                    QMouseEvent *mp = static_cast<QMouseEvent *>(event);
                                    qDebug() << "MouseButtonPress detected" << sweepmodeActive;
                                    if ( mp->button() == Qt::LeftButton  ) {
                                        return sweepmodeActive; // say we handled it (true)
                                    }
                                }
                                return QObject::eventFilter(obj, event);// normal forward
                            }
                        public slots:
                            void SweepModeToggle( bool status)
                            {
                                sweepmodeActive = status;
                                qDebug() << "sweepmode" << status;
                            }
                        };
                        
                        // in main window 
                         MouseEater *me = new MouseEater();
                         connect(ui->pushButton, &QPushButton::toggled, me, &MouseEater::SweepModeToggle ); // to toogle sweep mode
                         ui->menubar->installEventFilter(me); // put filter ont he menubar
                        

                        the result is this.

                        alt text

                        O Offline
                        O Offline
                        Oumayma
                        wrote on last edited by
                        #11

                        @mrjj
                        I have two buttons in the Sweep mode action, one of them activates the sweep mode where I install the filter so that the options are not displayed, that works well for me as you can see in the gif.

                        But when I click Direct mode where I uninstall the filter but it doesn't work for me and the menus are still blocked.

                        void MainWindow::on_actionModo_Barrido_2_triggered()
                        {
                        
                            modoBarrido = 0;
                            topFiller = new QWidget;
                            QVBoxLayout *box =  new QVBoxLayout;
                            QPushButton *mododirecto = new QPushButton(tr("ModoDirecto"));
                            QPushButton *modobarrido = new QPushButton(tr("ModoBarrido"));
                           // layout->setContentsMargins(0, 40, 40, 40);
                            box->addWidget(mododirecto);
                            box->addWidget(modobarrido);
                        
                            topFiller->setLayout(box);
                            topFiller->setGeometry(500,200,400,400);
                        
                            topFiller->setStyleSheet("background-color:rgb(23, 255, 240)");
                            topFiller->show();
                        
                            connect(mododirecto,SIGNAL(clicked()),this,SLOT(botondirecto()));
                            connect(modobarrido,SIGNAL(clicked()),this,SLOT(botonbarrido()));
                        
                        }
                        int MainWindow::botonbarrido()
                        {
                            main = new MainWindow();
                            modoBarrido=0;
                            if(modoBarrido==0){
                        
                                timeoutMenuPrincipal();
                                topFiller->close();
                                ui->menubar->installEventFilter(main);
                        
                            }
                        
                            return modoBarrido;
                        
                        }
                        
                        int MainWindow::botondirecto()
                        {
                            main = new MainWindow();
                            ui->menubar->removeEventFilter(main);
                            modoBarrido=1;
                            if(modoBarrido==1){
                                Highlight->~QRubberBand();
                                timer.stop();
                                timer2.stop();
                                timer.disconnect(&timer, SIGNAL(timeout()), this, SLOT(BarridoMenuPrincipal()));
                                timer2.disconnect(&timer2, SIGNAL(timeout()), this, SLOT(BarridoSubMenu()));
                                topFiller->close();
                        
                            }
                            return modoBarrido;
                        }
                        

                        screen-capture-5-h76w3wgv-zej7-mfzsncag-3q6r_5OH1CBCl_QL3U.gif

                        screen-capture-5-h76w3wgv-zej7-mfzsncag-3q6r_751zF4Yu_MWFp.gif

                        1 Reply Last reply
                        0
                        • mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by mrjj
                          #12

                          Hi
                          it seems to me that in

                          int MainWindow::botondirecto()
                          {
                              main = new MainWindow(); <<< make new window . its not the old one from botonbarrido()
                              ui->menubar->removeEventFilter(main); <<< remove the filter but did we ever put one On as main seems new to me
                          

                          So did i drink from the night pot or
                          did you mean to use removeEventFilter on the "main" we created in botonbarrido() ?

                          Also do notice if you are using my code for the filter, then you can
                          call
                          SweepModeToggle(true) to have it filter and SweepModeToggle(false) to allow all.
                          So you dont have to go and install and remove it.

                          1 Reply Last reply
                          1

                          • Login

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