Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

I'm confused about QMouseEvent Arguments



  • 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



  • 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?



  • @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!


  • Lifetime Qt Champion

    @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)



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


  • Lifetime Qt Champion

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

    this is Python

    MainWindow::on_btn_1_clicked() ? ;)



  • @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(...).



  • @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)



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


Log in to reply