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. itemChange not called
Qt 6.11 is out! See what's new in the release blog

itemChange not called

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 633 Views 2 Watching
  • 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.
  • B Offline
    B Offline
    Bert59
    wrote on last edited by
    #1

    Hi,

    I have a rectangle I would like to keep inside the plot area as shown below.
    629e1878-a383-4cbe-bb17-b9edcd1dc3ad-image.png
    To do so I have reimplemented QGraphicsRectItem as below:

    CallOut::CallOut(QRectF rect, QRectF plotArea) :
        QGraphicsRectItem(rect),
        m_plotArea(plotArea)
    {
    }
    
    QVariant CallOut::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
    {
        if (change == ItemPositionChange) {
            // value is the new position.
            QPointF newPos = value.toPointF();
    
            if (!m_plotArea.contains(newPos)) {
               newPos.setX(qMin(m_plotArea.right(), qMax(newPos.x(), m_plotArea.left())));
                newPos.setY(qMin(m_plotArea.bottom(), qMax(newPos.y(), m_plotArea.top())));
                return newPos;
            }
            qDebug() << "ItemPositionChange";
        }
        qDebug() << "itemChange";
    
        return QGraphicsItem::itemChange(change, value);
    }
    

    Then I implement my rectangle as follows:

    testRect = new CallOut(QRectF(0, 0, 100, 100), plotArea);
    testRect->setFlag(QGraphicsItem::ItemIsMovable);
    scene->addItem(testRect);
    

    The issue is that itemChange is never called when I move the rectangle?

    What did I made wrong?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi,
      To avoid massive change notifications, there are not all reported as default.
      So i think you need
      testRect->setFlag(QGraphicsItem::ItemSendsScenePositionChanges);

      B 1 Reply Last reply
      2
      • mrjjM mrjj

        Hi,
        To avoid massive change notifications, there are not all reported as default.
        So i think you need
        testRect->setFlag(QGraphicsItem::ItemSendsScenePositionChanges);

        B Offline
        B Offline
        Bert59
        wrote on last edited by
        #3

        @mrjj
        Thank you, I forgot this important "detail".

        1 Reply Last reply
        0

        • Login

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