Move square with mouse only on vector X or Y

Asked

Viewed 113 times

2

I’m learning about the QgraphicsScene, QgraphicsItem I am trying to move two squares with the mouse in the window, but moving only in vector X or only in vector Y, but that they move only in one vector at a time as if they were tied to move only horizontally or vertically, as I drag the mouse.

  • 2

    How exactly are you making the move? Show relevant part of your code.

1 answer

3

If you can, post a current code with traditional drag in all directions, which shows how to apply this logic when you have a little time.


For now, it follows the logic in code "agnostic":

By clicking on the mouse:

xOrigemObjeto = xAtualObjeto;
yOrigemObjeto = yAtualObjeto;
xOrigemMouse = xMouse;
yOrigemMouse = yMouse;

During drag (in the mouse movement Signal):

deltaX = xOrigemMouse - xMouse;
deltaY = yOrigemMouse - yMouse;

if( abs( deltaX ) > abs( deltaY ) ) {
    xAtualObjeto = xOrigemObjeto + deltaX;
    yAtualObjeto = yOrigemObjeto;
} else {
    xAtualObjeto = xOrigemObjeto;
    yAtualObjeto = yOrigemObjeto + deltaY;
}

Only care is to update the object source only after the user drops the mouse.

Basically what we are doing here is to consider whether the mouse walked more on X or Y, and change only one of the positions, "locking" the other.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.