开发者

Qt mouse click detection doesn't work all the time

开发者 https://www.devze.com 2023-01-11 13:02 出处:网络
Qt makes me question my sanity and existence. I don\'t know why code that works in one program that I wrote will not work in another program I wrote. The following code is identical in both programs.

Qt makes me question my sanity and existence. I don't know why code that works in one program that I wrote will not work in another program I wrote. The following code is identical in both programs. In P1 it works correctly by only allowing left clicks. In P2 it is exactly the same, except th开发者_C百科e left click code is does something different.

In P2 I have it checking the left click condition and do the code if it is true. Well, when I left or right click, it won't do the code. If I change the condition to check for a right click and return if true, then the left click works normally, but right click won't return. If I remove the conditions, both left and right click runs the code.

I am losing my mind because stupid stuff like this keeps happening all the time and I don't know why even though I do everything the same as the other programs that work (that I wrote).

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

P1 (this program works exactly how I want it to):

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 similar to P1, but doesn't work correctly):

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();
    }
}


From QMouseEvent::buttons() documentation:

For mouse release events this excludes the button that caused 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
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消