Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsItem Moves Faster than Mouse
Forum Updated to NodeBB v4.3 + New Features

QGraphicsItem Moves Faster than Mouse

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 869 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ModelTechM Offline
    ModelTechM Offline
    ModelTech
    wrote on last edited by ModelTech
    #1

    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.

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jan-Willem
      wrote on last edited by
      #2

      @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?

      1 Reply Last reply
      0
      • ModelTechM Offline
        ModelTechM Offline
        ModelTech
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        1
        • J Offline
          J Offline
          Jan-Willem
          wrote on last edited by
          #4

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

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved