Qt mouse click detection not working all the time

Qt makes me question my sanity and existence. I do not know why code that runs in one program that I wrote will not work in another program that I wrote. The following code is identical in both programs. In P1, it works correctly, allowing only left clicks. In P2, this is exactly the same, except that the left click code does something else.

In P2, I check the left click condition and execute the code if it is true. Well, when I left or right-clicked, it won’t do the code. If I change the condition to check the right click and return, if true, then the left click works fine, but the right click will not return. If I remove the conditions, both the left and right click run the code.

I lose my mind because such stupid things happen all the time, and I don’t know why, although I do everything the same way as other programs that work (what I wrote).

Edit: seems to ignore if-check in the mouseRelease function and works correctly for mousePress and mouseMove.

P1 (this program works exactly the way I want it):

void GLWidget::mousePressEvent(QMouseEvent *event)
{
    clickOn = event->pos();
    clickOff = event->pos();

    // right mouse button
    if (event->buttons() & Qt::RightButton){
        return;
    }

    // rest of left-click code here
}

/*************************************/

void GLWidget::mouseReleaseEvent(QMouseEvent *event)
{
    clickOff = event->pos();

    // right mouse button shouldn't do anything
    if (event->buttons() & Qt::RightButton)
        return;

    // rest of left click code here

}

/*************************************/

void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
    clickOff = event->pos();

    // do it only if left mouse button is down
    if (event->buttons() & Qt::LeftButton) {

        // left click code

        updateGL();

    } else if(event->buttons() & Qt::RightButton){

        // right mouse button code

    }
}

P2 (structured similarly to P1, but doesn’t work like that):

void GLWidget::mousePressEvent(QMouseEvent *event)
{
    clickOn = event->pos();
    clickOff = event->pos();

    // do it only if left mouse button is down
    if (event->buttons() & Qt::LeftButton) {
        // left click code
    }

}

void GLWidget::mouseReleaseEvent(QMouseEvent *event)
{
    clickOff = event->pos();

    // do it only if left mouse button is down
    if (event->buttons() & Qt::LeftButton) {
        // left click code
    }

}

void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
    clickOff = event->pos();
    clickDiff = clickOff - clickOn;

    // do it only if left mouse button is down
    if (event->buttons() & Qt::LeftButton) {
        // left click code
        updateGL();
    }
}
+3
source share
1 answer

From the QMouseEvent :: buttons () Documentation :

For mouse release events, this excludes the button that triggered the event.

So, the solution is to use QMouseEvent :: button () instead:

void GLWidget::mouseReleaseEvent(QMouseEvent *event)
{
    clickOff = event->pos();

    // do it only if left mouse button is down
    if (event->button() == Qt::LeftButton) {
        // left click code
    }
}
+3
source

Source: https://habr.com/ru/post/1758974/


All Articles