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. Distinguish between one and two mouse buttons pressed.
QtWS25 Last Chance

Distinguish between one and two mouse buttons pressed.

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 333 Views
  • 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.
  • S Offline
    S Offline
    sandro4912
    wrote on last edited by
    #1

    I have the following code:

    void Cell::mousePressEvent(QMouseEvent *event)
    {
        if(event->buttons() & Qt::LeftButton &&
                event->buttons() & Qt::RightButton)
        {
            qDebug() << "both pressed";
            return;
        }
    
        //....
    
        if(event->buttons() & Qt::LeftButton &&
                !(event->buttons() & Qt::RightButton)) {
            qDebug() << "left pressed";
        }
        else if(event->buttons() & Qt::RightButton &&
                !(event->buttons() & Qt::LeftButton)) {
    
            qDebug() << "right pressed";
        }
    }
    

    I want to detect if both mouse buttons are pressed at the same time or only left or right is pressed.

    However when i press both i get

    "left pressed"
    "both pressed"
    

    or

    "right pressed"
    "both pressed"
    

    Do i have to delay the evaluation of right and left with some way so that only both can get detected?

    Also i want to check if both mouse buttons are released to trigger an action. This also does not seem to work well:

    void Cell::mouseReleaseEvent(QMouseEvent *event)
    {
        if(event->button() == Qt::LeftButton &&
                event->button() == Qt::RightButton)
        {
            qDebug() << "both released";
        }
    }
    
    VRoninV 1 Reply Last reply
    0
    • S sandro4912

      I have the following code:

      void Cell::mousePressEvent(QMouseEvent *event)
      {
          if(event->buttons() & Qt::LeftButton &&
                  event->buttons() & Qt::RightButton)
          {
              qDebug() << "both pressed";
              return;
          }
      
          //....
      
          if(event->buttons() & Qt::LeftButton &&
                  !(event->buttons() & Qt::RightButton)) {
              qDebug() << "left pressed";
          }
          else if(event->buttons() & Qt::RightButton &&
                  !(event->buttons() & Qt::LeftButton)) {
      
              qDebug() << "right pressed";
          }
      }
      

      I want to detect if both mouse buttons are pressed at the same time or only left or right is pressed.

      However when i press both i get

      "left pressed"
      "both pressed"
      

      or

      "right pressed"
      "both pressed"
      

      Do i have to delay the evaluation of right and left with some way so that only both can get detected?

      Also i want to check if both mouse buttons are released to trigger an action. This also does not seem to work well:

      void Cell::mouseReleaseEvent(QMouseEvent *event)
      {
          if(event->button() == Qt::LeftButton &&
                  event->button() == Qt::RightButton)
          {
              qDebug() << "both released";
          }
      }
      
      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      @sandro4912 said in Distinguish between one and two mouse buttons pressed.:

      Do i have to delay the evaluation of right and left with some way so that only both can get detected?

      Yes, QApplication::doubleClickInterval() holds the value of that delay in ms. create a QElapsedTimer, start it in the constructor then every mousePressEvent you can check:

      if( event->buttons() & (Qt::LeftButton | Qt::RightButton) && elapsedTimer.restart() < QApplication::doubleClickInterval()){
      if(event->buttons() & Qt::LeftButton && event->buttons() & Qt::RightButton)
      qDebug() << "both pressed";
      }
      

      Also i want to check if both mouse buttons are released to trigger an action.

      Depends what you mean by this, but if you mean released at the same time then the previous applies

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • S Offline
        S Offline
        sandro4912
        wrote on last edited by
        #3

        i tryed this:

        In the constructor:

        mElapsedTimer.start();
        
        void Cell::mousePressEvent(QMouseEvent *event)
        {
            if( event->buttons() & (Qt::LeftButton | Qt::RightButton) &&
                    mElapsedTimer.restart() < QApplication::doubleClickInterval()){
                if(event->buttons() & Qt::LeftButton &&
                        event->buttons() & Qt::RightButton)
                {
                    qDebug() << "both pressed";
                    return;
                }
        
            }
        
            if(event->buttons() & Qt::LeftButton &&
                    !(event->buttons() & Qt::RightButton)) {
                qDebug() << "left pressed";
            }
            else if(event->buttons() & Qt::RightButton &&
                    !(event->buttons() & Qt::LeftButton)) {
        
                qDebug() << "right pressed";
            }
        }
        

        Still it detects both pressed and left or right

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          That's correct, the idea is that if both buttons get clicked within a short timeframe the first if is triggered. What you want instead is probably something like this

          in header

          private:
          QElaplsedTimer mElapsedTimer;
          QTimer singleMouseTimerLeft;
          QTimer singleMouseTimerRight;
          

          in constructor:

          mElapsedTimer.start();
          for(QTimer* timer : {&singleMouseTimerRight,&singleMouseTimerLeft}){
          timer->setInterval(QApplication::doubleClickInterval());
          timer->setSingleShot(true);
          }
          connect(&singleMouseTimerRight,&QTimer::timeout,this,[]()->void{qDebug() << "right pressed, no double clicks"});
          connect(&singleMouseTimerRight,&QTimer::timeout,this,[]()->void{qDebug() << "left pressed, no double clicks"});
          

          void Cell::mousePressEvent(QMouseEvent *event)
          {
              if(!(event->buttons() & (Qt::LeftButton | Qt::RightButton)))
                  return; //some other button was pressed
              const auto elapsedTime = mElapsedTimer.restart();
              if(elapsedTime < QApplication::doubleClickInterval()){
                  if(event->buttons() & Qt::LeftButton && event->buttons() & Qt::RightButton)
                      qDebug() << "both pressed";
                  for(QTimer* timer : {&singleMouseTimerRight,&singleMouseTimerLeft})
                      timer->stop();
                  return;
              }
              if(event->buttons() & Qt::LeftButton)
                  singleMouseTimerLeft.start();
              else
                  singleMouseTimerRight.start();
          }
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          S 1 Reply Last reply
          2
          • VRoninV VRonin

            That's correct, the idea is that if both buttons get clicked within a short timeframe the first if is triggered. What you want instead is probably something like this

            in header

            private:
            QElaplsedTimer mElapsedTimer;
            QTimer singleMouseTimerLeft;
            QTimer singleMouseTimerRight;
            

            in constructor:

            mElapsedTimer.start();
            for(QTimer* timer : {&singleMouseTimerRight,&singleMouseTimerLeft}){
            timer->setInterval(QApplication::doubleClickInterval());
            timer->setSingleShot(true);
            }
            connect(&singleMouseTimerRight,&QTimer::timeout,this,[]()->void{qDebug() << "right pressed, no double clicks"});
            connect(&singleMouseTimerRight,&QTimer::timeout,this,[]()->void{qDebug() << "left pressed, no double clicks"});
            

            void Cell::mousePressEvent(QMouseEvent *event)
            {
                if(!(event->buttons() & (Qt::LeftButton | Qt::RightButton)))
                    return; //some other button was pressed
                const auto elapsedTime = mElapsedTimer.restart();
                if(elapsedTime < QApplication::doubleClickInterval()){
                    if(event->buttons() & Qt::LeftButton && event->buttons() & Qt::RightButton)
                        qDebug() << "both pressed";
                    for(QTimer* timer : {&singleMouseTimerRight,&singleMouseTimerLeft})
                        timer->stop();
                    return;
                }
                if(event->buttons() & Qt::LeftButton)
                    singleMouseTimerLeft.start();
                else
                    singleMouseTimerRight.start();
            }
            
            S Offline
            S Offline
            sandro4912
            wrote on last edited by
            #5

            @VRonin

            That works. So it means I evaluate right and left clicks in a slot called every time the right or left timer runs out?

            Then in mousePressEvent only both buttons pressed is detected.

            PS:

            You have typos in these lines:

            connect(&singleMouseTimerRight,&QTimer::timeout,this,[]()->void{qDebug() << "right pressed, no double clicks"});
            connect(&singleMouseTimerRight,&QTimer::timeout,this,[]()->void{qDebug() << "left pressed, no double clicks"});
            

            It should be:

            connect(&singleMouseTimerRight,&QTimer::timeout,this,[]()->void{qDebug() << "right pressed, no double clicks";});
            connect(&singleMouseTimerLeft,&QTimer::timeout,this,[]()->void{qDebug() << "left pressed, no double clicks";});
            
            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