Move square with mouse only in vector X or Y

2

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

    
asked by anonymous 17.10.2014 / 21:49

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, follow the logic in "agnostic" code:

When you click the mouse:

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

While dragging (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;
}

The only care is to update the source of the object only after the user releases the mouse.

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

    
17.10.2014 / 21:53