-1

I don't have any code to show because this was over a year ago. I used a timer I think. It did not work very professionally. How would you do it so that it is a smooth operator?

I am already able to draw the grid efficiently (ie. in view only). I just need the snapping algorithm.

MathCrackExchange
  • 595
  • 1
  • 6
  • 25
  • 2
    Many different things can be snapped to a grid, in many different ways. What exact behavior are you trying to implement? Please describe the interaction from a user's perspective, in detail (edit the question then flag this comment as obsolete). – Kuba hasn't forgotten Monica Apr 25 '17 at 15:58

2 Answers2

1

Here is my mouseReleaseEvent for QGraphicsItem's derivative class. Grid has a step equal to 5:

void RadBox::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    double xx=x();
    double yy=y();
    if((int)xx / 5 != xx/5.) xx=5.0*round(xx/5.);
    if((int)yy / 5 != yy/5.) yy=5.0*round(yy/5.);
    setPos(xx,yy);
    QGraphicsItem::mouseReleaseEvent(event);
    emit moveBox(id,scenePos().x(),scenePos().y()); // you don't need this line, it's specific to my program
}
Michael
  • 5,095
  • 2
  • 13
  • 35
1

Re-implement QGraphicsItem::itemChange() to react upon the QGraphicsItem::ItemPositionChange change. Don't forget to set the QGraphicsItem::ItemSendsGeometryChanges flag.

Kamajii
  • 1,826
  • 9
  • 21