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. Clicking two mouse buttons with QtTest
QtWS25 Last Chance

Clicking two mouse buttons with QtTest

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 771 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 24 Oct 2019, 18:21 last edited by sandro4912
    #1

    I want to test that this piece gets hit:

    if(elapsedTime < QApplication::doubleClickInterval() &&
               event->buttons().testFlag(Qt::LeftButton) &&
               event->buttons().testFlag(Qt::RightButton)) {
    
           if(!isPressed()) {
               pressIfReleased();
               mNeighboursPressed = true;
               emit pressNeighbours();
           }
           for(QTimer* timer : { &mSingleMouseTimerRight,
               &mSingleMouseTimerLeft }) {
               timer->stop();
           }
           return;
       }
    

    I tryed this but it is not detected as both pressed together:

       QTest::mousePress(&obj, Qt::LeftButton, Qt::NoModifier,
                         obj.rect().center());
       QTest::mousePress(&obj, Qt::RightButton, Qt::NoModifier,
                         obj.rect().center());
    

    So what can i do ?

    P 1 Reply Last reply 24 Oct 2019, 18:47
    0
    • S sandro4912
      24 Oct 2019, 18:21

      I want to test that this piece gets hit:

      if(elapsedTime < QApplication::doubleClickInterval() &&
                 event->buttons().testFlag(Qt::LeftButton) &&
                 event->buttons().testFlag(Qt::RightButton)) {
      
             if(!isPressed()) {
                 pressIfReleased();
                 mNeighboursPressed = true;
                 emit pressNeighbours();
             }
             for(QTimer* timer : { &mSingleMouseTimerRight,
                 &mSingleMouseTimerLeft }) {
                 timer->stop();
             }
             return;
         }
      

      I tryed this but it is not detected as both pressed together:

         QTest::mousePress(&obj, Qt::LeftButton, Qt::NoModifier,
                           obj.rect().center());
         QTest::mousePress(&obj, Qt::RightButton, Qt::NoModifier,
                           obj.rect().center());
      

      So what can i do ?

      P Offline
      P Offline
      Pablo J. Rogina
      wrote on 24 Oct 2019, 18:47 last edited by
      #2

      @sandro4912 said in Clicking two mouse buttons with QtTest:

      So what can i do ?

      What about QTest::mouseDClick(...)?

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • S Offline
        S Offline
        sandro4912
        wrote on 24 Oct 2019, 19:37 last edited by
        #3

        But that only clicks one button twice and not two buttons simultaneous?

        P 1 Reply Last reply 24 Oct 2019, 19:46
        0
        • S sandro4912
          24 Oct 2019, 19:37

          But that only clicks one button twice and not two buttons simultaneous?

          P Offline
          P Offline
          Pablo J. Rogina
          wrote on 24 Oct 2019, 19:46 last edited by Pablo J. Rogina
          #4

          @sandro4912 said in Clicking two mouse buttons with QtTest:

          that only clicks one button twice and not two buttons simultaneous?

          Yes, you're right. Sorry, I was misled by this QApplication::doubleClickInterval...

          Anyway, I guess (not tested) that you can do:

          QTest::mousePress(&obj, Qt::LeftButton | Qt::RightButton, Qt::NoModifier, obj.rect().center());
          

          I mean, given that QMouseEvent::buttons() returns

          the button state when the event was generated. The button state is a combination of Qt::LeftButton, Qt::RightButton, Qt::MidButton using the OR operator.

          you may be able to do the opposite, creating a mouse press event with the two buttons pressed.

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          S 1 Reply Last reply 25 Oct 2019, 14:52
          0
          • K Offline
            K Offline
            Kent-Dorfman
            wrote on 25 Oct 2019, 03:09 last edited by
            #5

            I'm not sure where this is defined, as it doesn't seem to be a registered framework virtual:

            QTest::mousePress(&obj, Qt::LeftButton, Qt::NoModifier,
                                 obj.rect().center());
            

            So try overriding the QWidget::mousePressEvent(QMouseEvent* e) virtual and check the state of the buttons as reported in e->buttons().

            1 Reply Last reply
            0
            • P Pablo J. Rogina
              24 Oct 2019, 19:46

              @sandro4912 said in Clicking two mouse buttons with QtTest:

              that only clicks one button twice and not two buttons simultaneous?

              Yes, you're right. Sorry, I was misled by this QApplication::doubleClickInterval...

              Anyway, I guess (not tested) that you can do:

              QTest::mousePress(&obj, Qt::LeftButton | Qt::RightButton, Qt::NoModifier, obj.rect().center());
              

              I mean, given that QMouseEvent::buttons() returns

              the button state when the event was generated. The button state is a combination of Qt::LeftButton, Qt::RightButton, Qt::MidButton using the OR operator.

              you may be able to do the opposite, creating a mouse press event with the two buttons pressed.

              S Offline
              S Offline
              sandro4912
              wrote on 25 Oct 2019, 14:52 last edited by
              #6

              @Pablo-J-Rogina said in Clicking two mouse buttons with QtTest:

              QTest::mousePress(&obj, Qt::LeftButton | Qt::RightButton, Qt::NoModifier, obj.rect().center());

              Unfortunately it does not compile. mousePress semms to take only button not buttons

              P 1 Reply Last reply 25 Oct 2019, 15:05
              0
              • S sandro4912
                25 Oct 2019, 14:52

                @Pablo-J-Rogina said in Clicking two mouse buttons with QtTest:

                QTest::mousePress(&obj, Qt::LeftButton | Qt::RightButton, Qt::NoModifier, obj.rect().center());

                Unfortunately it does not compile. mousePress semms to take only button not buttons

                P Offline
                P Offline
                Pablo J. Rogina
                wrote on 25 Oct 2019, 15:05 last edited by
                #7

                @sandro4912 said in Clicking two mouse buttons with QtTest:

                Unfortunately it does not compile

                I adviced my code was not tested :-)
                Anyway, could you please post the compile error?

                mousePress semms to take only button not buttons

                Yes, per method signature void QTest::mousePress(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = ..., QPoint pos = ..., int delay = ...)

                so the idea is to combine both Qt::LeftButton which is 1 and Qt::RightButton which is 2 into just one value with OR function -> 3.

                What if you try with this (again not tested):

                QTest::mousePress(&obj, 3, Qt::NoModifier, obj.rect().center());
                

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                1
                • S Offline
                  S Offline
                  sandro4912
                  wrote on 25 Oct 2019, 15:40 last edited by
                  #8

                  @Pablo-J-Rogina said in Clicking two mouse buttons with QtTest:

                  Qt::MouseButton button

                  That also does not work. I think you cannot combine it with or for Qt::MouseButton button only for Qt::MouseButtons buttons.

                  Anyway i realized I have a error in the code I want to test. The test code is okay. I don't really need to press two buttons exactly at the same time.

                  My code allows to have the buttons clicked with a short delay of 50ms. Then it still counts as clicked together.

                  Here just for curiosity the code to test:

                  The press event:

                      if(!(event->buttons().testFlag(Qt::LeftButton) ||
                           event->buttons().testFlag(Qt::RightButton))) {
                          return;
                      }
                  
                      if(event->buttons().testFlag(Qt::LeftButton)) {
                          mSingleMouseTimerLeft.start();
                      }
                      else if (event->buttons().testFlag(Qt::RightButton)){
                          mSingleMouseTimerRight.start();
                      }
                  
                      const auto elapsedTime = mElapsedTimer.restart();
                  
                      if(elapsedTime >= QApplication::doubleClickInterval()) {
                          return;
                      }
                  
                      if((mSingleMouseTimerLeft.isActive() &&
                          event->buttons().testFlag(Qt::RightButton)) ||
                          (mSingleMouseTimerRight.isActive() &&
                           event->buttons().testFlag(Qt::LeftButton))){
                  
                          if(!isPressed()) {
                              pressIfReleased();
                              mNeighboursPressed = true;
                              emit pressNeighbours();
                          }
                          for(QTimer* timer : { &mSingleMouseTimerRight,
                              &mSingleMouseTimerLeft }) {
                              timer->stop();
                          }
                      }
                  

                  In constructor:

                     constexpr auto intervall = 50;
                     for(QTimer* timer : {&mSingleMouseTimerRight, &mSingleMouseTimerLeft}){
                         timer->setInterval(intervall);
                         timer->setSingleShot(true);
                     }
                  
                     connect(&mSingleMouseTimerLeft, &QTimer::timeout,
                             this, &Cell::pressIfReleased);
                     connect(&mSingleMouseTimerRight, &QTimer::timeout,
                             this, &Cell::mark);
                  

                  So in Short if the user clicks Right and Left in less than 50ms after each other it counts as both buttons clicked together.

                  The test code stays the same as posted

                  Still i wonder why theres no method which Supports the ``Qt::MouseButtons buttons` to press 2 Buttons like you suggested simultaneously.

                  1 Reply Last reply
                  0

                  2/8

                  24 Oct 2019, 18:47

                  6 unread
                  • Login

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