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. I'm confused about QMouseEvent Arguments

I'm confused about QMouseEvent Arguments

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 1.0k Views 1 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.
  • SisqosS Offline
    SisqosS Offline
    Sisqos
    wrote on last edited by
    #1

    Hi, I'm Sisqos. I'm a novice at Qt.
    //====================================================================
    We knew:

    QMouseEvent(QEvent::Type type, 
                const QPointF &localPos, 
                const QPointF &windowPos, 
                const QPointF &screenPos, 
                Qt::MouseButton button, 
                Qt::MouseButtons buttons, *(the state of buttons)*
                Qt::KeyboardModifiers modifiers)
    

    Bitwise Operators:
    ~ >> It's a bitwise operator. It reverses the bit. 0 become 1; 1 become 0.
    & >> 0 &0 = 0; 0&1 = 0; 1&1 = 0.
    //====================================================================
    Question 1: What is "the state of buttons"? What is its the purpose? Why do we need "the state of buttons"?
    2814e9be-45a4-4f75-8987-8bca876cff67-image.png
    In this case, I used the code below to make a new mouseEvent. When I click button1 and send the new event to button2. The code works perfectly, but the confusing part is no matter which argument of "the state of buttons" used (Qt::NoButton, Qt::Buttons, Qt::LeftButton, etc). The state of buttons doesn't affect anything. Following that, I can't get any reason to use the argument of the state of buttons. Hope someone can explain it.

    void MainWindow::on_btn_1_clicked()
    {
        QMouseEvent * mouseEvent = new QMouseEvent(QEvent::MouseButtonRelease, QPointF(0,0), Qt::MiddleButton, Qt::NoButton,Qt::NoModifier);
        if(QApplication::sendEvent(button2, mouseEvent)){
            qDebug() << "Event accepted";
        }else{
            qDebug() <<"Event rejected";
        }
    }
    

    //====================================================================
    Question2: What does "event.buttons() & ~Qt.LeftButton" mean? Why do we need it?
    The code is taken from an online example. I don't understand why we need
    "event.buttons() & ~Qt.LeftButton"
    for the state of buttons. Hope someone can help. I really really want to know it.

    def middleMouseButtonRelease(self, event):
            fakeEvent = QMouseEvent(event.type(), event.localPos(), event.screenPos(),
                                    Qt.LeftButton, event.buttons() & ~Qt.LeftButton, event.modifiers())
            super().mouseReleaseEvent(fakeEvent)
    

    Have a good day
    Sisqos

    JonBJ 1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by Kent-Dorfman
      #2

      In simplest terms the "state" is the bitmask of buttons that are pressed at the time of the event. so given that, you tell me...what does state-mask & ~(specific button bit position) mean? If leftbutton has value of 4 then what is the bitwise "and" of the mask and ~4?

      If you meet the AI on the road, kill it.

      SisqosS 1 Reply Last reply
      1
      • SisqosS Sisqos

        Hi, I'm Sisqos. I'm a novice at Qt.
        //====================================================================
        We knew:

        QMouseEvent(QEvent::Type type, 
                    const QPointF &localPos, 
                    const QPointF &windowPos, 
                    const QPointF &screenPos, 
                    Qt::MouseButton button, 
                    Qt::MouseButtons buttons, *(the state of buttons)*
                    Qt::KeyboardModifiers modifiers)
        

        Bitwise Operators:
        ~ >> It's a bitwise operator. It reverses the bit. 0 become 1; 1 become 0.
        & >> 0 &0 = 0; 0&1 = 0; 1&1 = 0.
        //====================================================================
        Question 1: What is "the state of buttons"? What is its the purpose? Why do we need "the state of buttons"?
        2814e9be-45a4-4f75-8987-8bca876cff67-image.png
        In this case, I used the code below to make a new mouseEvent. When I click button1 and send the new event to button2. The code works perfectly, but the confusing part is no matter which argument of "the state of buttons" used (Qt::NoButton, Qt::Buttons, Qt::LeftButton, etc). The state of buttons doesn't affect anything. Following that, I can't get any reason to use the argument of the state of buttons. Hope someone can explain it.

        void MainWindow::on_btn_1_clicked()
        {
            QMouseEvent * mouseEvent = new QMouseEvent(QEvent::MouseButtonRelease, QPointF(0,0), Qt::MiddleButton, Qt::NoButton,Qt::NoModifier);
            if(QApplication::sendEvent(button2, mouseEvent)){
                qDebug() << "Event accepted";
            }else{
                qDebug() <<"Event rejected";
            }
        }
        

        //====================================================================
        Question2: What does "event.buttons() & ~Qt.LeftButton" mean? Why do we need it?
        The code is taken from an online example. I don't understand why we need
        "event.buttons() & ~Qt.LeftButton"
        for the state of buttons. Hope someone can help. I really really want to know it.

        def middleMouseButtonRelease(self, event):
                fakeEvent = QMouseEvent(event.type(), event.localPos(), event.screenPos(),
                                        Qt.LeftButton, event.buttons() & ~Qt.LeftButton, event.modifiers())
                super().mouseReleaseEvent(fakeEvent)
        

        Have a good day
        Sisqos

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @Sisqos
        The "state of the buttons" allows you to query which buttons are held/clicked. That is how, for instance, you can tell in an even whether the user has clicked the left button, right button, or both buttons together.

        event.buttons() & Qt.LeftButton is non-zero if the left button is marked as down in the state. It tells you nothing about other buttons, they may or may not be down, that information is masked out to 0.

        event.buttons() & ~Qt.LeftButton is non-zero if anything other than the left button is marked as down in the state. It tells you nothing about the left button, that may or may not be down, that information is masked out to 0.

        So this code creates a "fake event" from the genuine event passed in with whatever buttons were originally down except that it marks the left button bit as not-down. Why it does this I do not know!

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @JonB said in I'm confused about QMouseEvent Arguments:

          Why it does this I do not know!

          And it creates a memory leak (event is leaked)

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          JonBJ 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @JonB said in I'm confused about QMouseEvent Arguments:

            Why it does this I do not know!

            And it creates a memory leak (event is leaked)

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Christian-Ehrlicher
            Always interested in your comments :) How is event leaked here, this is Python, what would you like done with event to resolve?

            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @JonB said in I'm confused about QMouseEvent Arguments:

              this is Python

              MainWindow::on_btn_1_clicked() ? ;)

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              JonBJ 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                @JonB said in I'm confused about QMouseEvent Arguments:

                this is Python

                MainWindow::on_btn_1_clicked() ? ;)

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @Christian-Ehrlicher
                Ooohhh, I only noticed the pasted

                def middleMouseButtonRelease(self, event):
                    ...
                

                I had not clocked that the OP had translated that into C++!

                @Sisqos
                Where you now have

                QMouseEvent * mouseEvent = new QMouseEvent(...);
                

                You should either end that method with mouseEvent->deleteLater(), or change to making it a stack variable QMouseEvent mouseEvent(...).

                SisqosS 1 Reply Last reply
                0
                • Kent-DorfmanK Kent-Dorfman

                  In simplest terms the "state" is the bitmask of buttons that are pressed at the time of the event. so given that, you tell me...what does state-mask & ~(specific button bit position) mean? If leftbutton has value of 4 then what is the bitwise "and" of the mask and ~4?

                  SisqosS Offline
                  SisqosS Offline
                  Sisqos
                  wrote on last edited by Sisqos
                  #8

                  @Kent-Dorfman
                  After I compare these, I got the idea.
                  If the leftButton has 00001000. and ~4 = 11111011
                  Mask = 10000000
                  value = 11111011
                  AND = 10000000 (gets 1)

                  If the leftButton has 00001000.
                  Mask = 10000000
                  value = 00001000
                  AND = 00000000 (gets 0)

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Christian-Ehrlicher
                    Ooohhh, I only noticed the pasted

                    def middleMouseButtonRelease(self, event):
                        ...
                    

                    I had not clocked that the OP had translated that into C++!

                    @Sisqos
                    Where you now have

                    QMouseEvent * mouseEvent = new QMouseEvent(...);
                    

                    You should either end that method with mouseEvent->deleteLater(), or change to making it a stack variable QMouseEvent mouseEvent(...).

                    SisqosS Offline
                    SisqosS Offline
                    Sisqos
                    wrote on last edited by Sisqos
                    #9

                    @JonB
                    I will pay attention about
                    mouseEvent->deleteLater()
                    Thanks for your information.

                    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