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. Setting Position of custom Graphics Item not working as expected
QtWS25 Last Chance

Setting Position of custom Graphics Item not working as expected

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 958 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.
  • faiszalkhanF Offline
    faiszalkhanF Offline
    faiszalkhan
    wrote on last edited by
    #1

    Hi, I have a custom graphics Item. When the user clicks a point on the qgraphicsscene, I do the painting of the device using the point clicked. This works as intended.
    However, I have not yet set the position of the Item using setPos(), hence when I get the position of the Item using getPos() after drawing I get the value as 0,0 as I have not set the position.
    The problem is after drawing the Item if I set the position of the Item using setPos() using the legitimate value, I get an offset while drawing i.e. the Item is drawn away from where I have clicked. This offset decreases when I move near Top left.

    Note: I am setting the position value as ScenePos() taken from the scene as required.

    Please Help,
    Thanks.

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

      Hi,

      You should show the code you are using for the painting and handling of the position of your item otherwise it's pretty much Crystal Ball Debugging.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • faiszalkhanF Offline
        faiszalkhanF Offline
        faiszalkhan
        wrote on last edited by
        #3

        @SGaist Well I think I need an outside view on it then.

        This is where the State Item is created.

        /* Get the calculated position based on the mouse click */
         QPointF calPos((mouseEvent->scenePos().x() - (DEFAULT_WIDTH/2)),
                       (mouseEvent->scenePos().y() - (DEFAULT_HEIGHT/2)));
        
        
         /* Create new state at the calculated position */
         srstate = new StateItem(calPos);
        

        This is where is get the position from the canvas and set the position of the stateItem

        /* Get the position from the caller */
            drawPos = new QPointF(pos.x(), pos.y());
            
            /* Set the position of the stateItem */
            this->setPos(*drawPos);
        
        

        This is how the painting of the Item is done,

        void StateItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
        {
            /* Set the pen */
            QPen blackpen(Qt::black);
            blackpen.setWidth(1);
            painter->setPen(blackpen);
        
            /* Set colour of the state */
            if(!backgoundColor.isValid())
                backgoundColor = QColor(229,231,233);
            QColor statecolor(backgoundColor);
            QBrush statebrush(statecolor);
            painter->setBrush(statebrush);
        
            /* Draw state item */
            painter->drawRoundedRect(drawPos->x(),drawPos->y(),stateItemWidth,stateItemHeight,stateItemXRadius,stateItemYRadius);
        
            /* Draw line to complete state item */
            QPoint p(drawPos->x(),(drawPos->y() + STATEITEM_NAMECHANNEL_WIDTH));
            QPoint q((drawPos->x() + stateItemWidth), (drawPos->y() + STATEITEM_NAMECHANNEL_WIDTH));
            QLine stateLine(p,q);
            painter->drawLine(stateLine);
            painter->drawText(QRect(drawPos->x()+5, drawPos->y()+5, stateItemWidth, 10), Qt::AlignCenter, stateItemName);
            painter->drawText(QRect(drawPos->x()+5, drawPos->y()+35, stateItemWidth, stateItemHeight-35),  stateItemAction);
        }
        
        

        Thank you! :(

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          The way you calculate calPos looks a bit fishy, QGraphicsView provides the various map functions that should give you the right position on the scene.

          On a side note, why did you made drawPos a pointer ? There's no need for that and you are creating a leak the way you handle it.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • faiszalkhanF Offline
            faiszalkhanF Offline
            faiszalkhan
            wrote on last edited by
            #5

            @SGaist, calPos is calculated to get the top left position of the Item to be drawn so that when the user clicks on the scene to draw, the item should be drawn with the clicked position as the centre. The problem is only when I do a explicit setPos I get the offset in the position of the item drawn.

            My only intention to do setPos is to have a legitimate value of the pos of the item on the scene at anytime by calling item->pos(). Is there an alternative to setPos() and pos()?

            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