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?
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?
Define a constant for the Y cord. , for each mouse event entry , if y =! constant Y , Y = constant Y.
Can you give me example?
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.
chong_kimkeang (8th October 2012)
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 (8th October 2012)
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?
Bookmarks