PDA

View Full Version : How can I swipe one image to left or right on the same x and different y by mouse?



chong_kimkeang
5th October 2012, 11:21
Actually my image can be moved but I want it to move on only an axis(x) that mean the image can not swipe to top or bottom but can be swiped to right or left only. Can everyone give me code or idea?

AtlasS
5th October 2012, 15:10
Define a constant for the Y cord. , for each mouse event entry , if y =! constant Y , Y = constant Y.

chong_kimkeang
6th October 2012, 09:25
Can you give me example?

AtlasS
7th October 2012, 15:31
I'm not yet familiar with the drag/drop mouse events, only with the clicking, but with each mouse press/movement the event returns the (x,y) cord. of the cursor on the scene.

So, the code might be something like this :

qreal fixed_Y = the value you want for the Y at all times

QPoint *toMove = new QPoint(0,fixed_Y);

toMove->setX ( event->scenePos().x() ) ; //Event function will return an integer, this integer would be casted to qreal then stored in toMove
toMove->setY( fixedY) ;

yourItem ->setPos( *toMove) ;


now for each time the mouse moves/getting pressed, it will retrive the click on the X axis only, while the Y (defined as a constant & never to change) is fixed at all times.

ChrisW67
7th October 2012, 22:52
Assuming you are using the QWidget::mousePressEvent(), mouseMoveEvent(), and mouseReleaseEvent() then:

When the mouse button is pressed capture the mouse position (QPoint or QPointF) into a member variable
During mouse moves while dragging take the current mouse position and insert the y (for horizontal drag) or x (for vertical) of the original click point. Use this hybrid point for any drawing/scene changes you trigger here.


BTW: No need to allocate QPoint/QPointF objects on the heap like AtlasS has done. These are value objects and copied on use.

chong_kimkeang
9th October 2012, 04:10
Now I am using Scene for setFlag() to move the image but now I don't want to use it. Do you have other ways to do with this?