Random MousePressOffset



  • Hello,

    I recently run into a problem using QMouseEvent - Press, Release and Move.

    In the end I was able to fix it, but I'm puzzled, why this was a problem in the first place. Maybe someone can enlighten me on what is going on.

    Ok, the MousePress Origin was saved like this:

    mousePressEvent(QMouseEvent *event){
       pointPressed = event->pos();
    }
    

    In mouse move I had the following:

    mouseMoveEvent(QMouseEvent *event){
       int distY = pointPressed.y() - event->y();
       this->move(this->x(),0-distY);
    }
    

    For fast mousemoves, everything works fine, for slow movement of the mouse however, the widgets gets 'stuttery'. It jumps back and forth.

    Turns out, pointPressed actually chages its value by 1, even so mousebutton is not pressed again. Found with

    qDebug() << pointPressed; //In mouseMoveEvent
    

    fixed it with the following:

    //MousePressEvent
    pointPressed = mapToGlobal(event->pos());
    
    //mouseMoveEvent
    QPoint eveGl = mapToGlobal(event->pos());
    int distY = pointPressed.y() - eveGl.y();
    

    By the way, pointPressed = mapToGlobal(event->pos()); has to happen in MousePressEvent . If I do both maptoGlobals in the mouseMoveEvent I have the same behaviour as if I used local coordinates.

    If any one could explain that to me, I would be very grateful.



  • Turns out @kragnfrol answered my question. Probably without knowladge of this topics existence x)

    from the definition of QPoint QMouseEvent::pos() const

    Those who are able to read are at an advantage indeed...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.