QGraphicsItem Moves Faster than Mouse



  • I have created a QGraphicsItem that can be moved to a different position within a QGraphicsScene by using the mouse. The position of the QGraphicsItem is stored in an separate Model object (I am trying to properly separate Model and View as the Views (incl any QGraphicsItems can be destroyed while the Model objects should remain). I enabled the option of moving the QGraphicsItem by using:

    setFlags(ItemIsMovable | ItemSendsScenePositionChanges);
    

    Since I have added the QGraphicsItem::ItemSendsScenePositionChanges flag and reimplemented the itemChange() handler with the following code

    QVariant StateDiagram::itemChange(GraphicsItemChange Change, const QVariant &Value) {
    
        if (Change == ItemPositionHasChanged) {
            QPointF Position = Value.toPointF();
            ModelObject->setPosition(Position);
        }
    
        return QGraphicsItem::itemChange(Change, Value);
    }
    

    the QGrahicsItem moves with exponential position differences compared to the mouse movements. Anybody an idea what I am doing wrongly?

    Just in case it is relevant: the boundingRect() of my QGraphicsItem depends on the Position stored in the ModelObject.



  • @ModelTech said:

    itemChange

    From the documentation:

    Item Coordinates
    
    Items live in their own local coordinate system. Their coordinates are usually centered around its center point (0, 0), and this is also the center for all transformations. 
    

    and

    At item's position is the coordinate of the item's center point in its parent's coordinate system;
    

    I'm not sure, but wouldn't this mean that the pos() of an item is it's center, while the position of the boundingRect() is it's top left?



  • Well, it took a very long time before I figured it out, but indeed it had to do with different coordinate systems. This is what I suspected, but I wasn't sure at all what was wrong. The problem was that the boundingRect should not depend on the stored position. I should use setPos to retrieve the stored position instead of moving the boundingRect to that position. Your hint @Jan-Willem gave me the last push in the right direction. A big thank you!



  • You're welcome.
    Great you've got it working.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.