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. restraining QGraphicsPolygonItem on a QGraphicsScene
Forum Updated to NodeBB v4.3 + New Features

restraining QGraphicsPolygonItem on a QGraphicsScene

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.2k Views 1 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.
  • C Offline
    C Offline
    CraigBakalian
    wrote on last edited by CraigBakalian
    #1

    Hi,

    I am making a tangram puzzle with my son. Tangram puzzle pieces can rotate. Easily done in qt as is moving the pieces around. However, restraining the pieces in the scene is freaking me out. The puzzle piece stays within the bounds of the scene fine if the piece is not rotated. However, when the piece is rotated odd things happen; half the piece goes out of bounds, or the whole piece cannot approach the edge by odd distances. I am freaking out about the math. I have used qDebug to look at the positions, and the piece should behave properly, but it isn't... what am I missing. Here is the itemChanged override->

    QVariant PuzzlePiece::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
    {
        if (change == ItemPositionChange && scene()) {
                QPointF newPos = value.toPointF();
                QRectF rect = scene()->sceneRect();
                QPolygonF poly = mapToScene(tangramShape);
    
                rect.setWidth(rect.width() - poly.boundingRect().width());
                rect.setHeight(rect.height() - poly.boundingRect().height());
    
                if (!rect.contains(newPos))
                {
                    qDebug() << newPos << rect << poly.boundingRect();
                    newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left())));
                    newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top())));
                    qDebug() << newPos;
                    return newPos;
                }
            }
        return QGraphicsItem::itemChange(change, value);
    }
    

    Is the mapToScene messing things up? Any help would be appreciated.

    Craig Bakalian

    1 Reply Last reply
    0
    • E Offline
      E Offline
      egan
      wrote on last edited by
      #2

      setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);

      1 Reply Last reply
      0
      • C Offline
        C Offline
        CraigBakalian
        wrote on last edited by
        #3

        setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);

        Is not changing the behavior. I think I may need to just implement a mouse move function.

        E 1 Reply Last reply
        0
        • C CraigBakalian

          setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);

          Is not changing the behavior. I think I may need to just implement a mouse move function.

          E Offline
          E Offline
          egan
          wrote on last edited by
          #4

          @CraigBakalian
          setFlag(QGraphicsItem::ItemIsMovable, true);
          setFlag(QGraphicsItem::ItemIsSelectable, true);
          setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);

          1 Reply Last reply
          0
          • C Offline
            C Offline
            CraigBakalian
            wrote on last edited by
            #5

            all of those flags are set. I still have unusual bounds checking after an object is rotated, even if i get the boundingRect of a polygon. I cannot figure it out.

            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