Important: Please read the Qt Code of Conduct -

Setting Position of custom Graphics Item not working as expected

  • 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,

  • Lifetime Qt Champion


    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.

  • @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 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);
        /* Set colour of the state */
            backgoundColor = QColor(229,231,233);
        QColor statecolor(backgoundColor);
        QBrush statebrush(statecolor);
        /* Draw state item */
        /* 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->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! :(

  • Lifetime Qt Champion

    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.

  • @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()?

Log in to reply