Results 1 to 1 of 1

Thread: QGraphicScene item moving itself

  1. #1
    Join Date
    May 2015
    Posts
    22
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicScene item moving itself

    Hello all,

    I'm trying to create a 'matrix' of items in a scene, so when the user moves an item and drops, I use the code below to calculate the x index and y index and place the item in the right position. This works fine; however, after I place the item in the new position, when I try to move the item again, the item first returns to the first position and then starts to move. I think the problem is that when I call QGraphicsItem::setPos(), although the item is in the correct visible position, seems that the first drag and drop movement wasn't 'recorded' by Qt. I'm attaching some code to see if helps to understand my problem

    Qt Code:
    1. bool MyClass::eventFilter(QObject * object, QEvent * event){
    2. switch ((int)event->type()){
    3. case QEvent::GraphicsSceneMouseRelease:
    4. QGraphicsItem *item = scene->itemAt(me->scenePos(),QTransform());
    5. QPointF newPos = ItemNewPosition(item->scenePos());
    6. item->setPos(newPos);
    7. item->update();
    8. this->updateMicroFocus();
    9. this->update();
    10. this->updateGeometry();
    11. return true;
    12. break;
    13. }
    14. return QObject::eventFilter(object, event);
    15. }
    16.  
    17. QPointF MyClass::ItemNewPosition(QPointF nodePosition)
    18. {
    19. QPointF newPos;
    20. int x = nodePosition.x() / 10;
    21. int y = nodePosition.y() / 50;
    22. newPos.setX( 10 * x);
    23. newPos.setY( 50 * y);
    24. return newPos;
    25. }
    To copy to clipboard, switch view to plain text mode 


    Added after 29 minutes:


    Got it, I have to return false in my event!
    Last edited by thibs; 11th March 2017 at 03:22.

Similar Threads

  1. Replies: 4
    Last Post: 8th April 2011, 20:59
  2. Moving parent item
    By barisatamer in forum Qt Programming
    Replies: 1
    Last Post: 6th February 2011, 09:49
  3. How to orientate a moving item?
    By lengshuang in forum Qt Programming
    Replies: 2
    Last Post: 13th April 2009, 08:25
  4. moving one graphical item over other item...
    By salmanmanekia in forum Qt Programming
    Replies: 36
    Last Post: 3rd July 2008, 07:59
  5. Moving an item in QGraphicsScene
    By prosass in forum Newbie
    Replies: 4
    Last Post: 28th March 2007, 14:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.