Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Clicking two mouse buttons with QtTest

    General and Desktop
    3
    8
    231
    Loading More Posts
    • 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
      sandro4912 last edited by sandro4912

      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 ?

      Pablo J. Rogina 1 Reply Last reply Reply Quote 0
      • Pablo J. Rogina
        Pablo J. Rogina @sandro4912 last edited by

        @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 Reply Quote 1
        • S
          sandro4912 last edited by

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

          Pablo J. Rogina 1 Reply Last reply Reply Quote 0
          • Pablo J. Rogina
            Pablo J. Rogina @sandro4912 last edited by Pablo J. Rogina

            @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 Reply Quote 0
            • Kent-Dorfman
              Kent-Dorfman last edited by

              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 Reply Quote 0
              • S
                sandro4912 @Pablo J. Rogina last edited by

                @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

                Pablo J. Rogina 1 Reply Last reply Reply Quote 0
                • Pablo J. Rogina
                  Pablo J. Rogina @sandro4912 last edited by

                  @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 Reply Quote 1
                  • S
                    sandro4912 last edited by

                    @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 Reply Quote 0
                    • First post
                      Last post