Important: Please read the Qt Code of Conduct -

Weird behaviour when dragging QGraphicsItem

  • Here's before I drag:

    Here's after I drag it:

    If I alt tab the blurry shadow line disappears, what I can't figure out is why it appears in the first place.

    Here's the code I'm using for my objects:

    customRect::customRect(const QRectF &rect) : QGraphicsRectItem(rect){
    void customRect::addLine(myArrow *line) {
        this->line = line;
    QVariant customRect::itemChange(GraphicsItemChange change, const QVariant &value)
        if (change == ItemPositionChange && scene()) {
            QPointF newPos = value.toPointF();
        return QGraphicsItem::itemChange(change, value);
    void customRect::moveLineToCenter(QPointF newPos) {
        int xOffset = rect().x();
        int yOffset = rect().y();
        QPointF newCenterPos = QPointF(newPos.x() + xOffset, newPos.y() + yOffset);
        // Move the required point of the line to the center of the elipse
        QPointF p1 = newCenterPos;
        p1.setY( newCenterPos.y() - 200 );
        QPointF p2 = newCenterPos;
        foreach (myArrow *arrow, arrows) {
            p1.setX(p1.x() + 25);
            p2.setX(p2.x() + 25);
    myArrow::myArrow(customRect *rect){
        myrect = rect;
        QPointF p = rect->boundingRect().topLeft();
        p.setY(p.y() - 200);
        setLine( QLineF( p , rect->boundingRect().topLeft() ));
    void myArrow::updatePosition(QPointF p1, QPointF p2)
        QLineF line(p2,p1);
    void myArrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
        qreal arrowSize = 20;
        double angle = std::atan2(-line().dy(), line().dx());
        QPointF arrowP1 = line().p1() + QPointF(sin(angle + M_PI / 3) * arrowSize,
                                        cos(angle + M_PI / 3) * arrowSize);
        QPointF arrowP2 = line().p1() + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,
                                        cos(angle + M_PI - M_PI / 3) * arrowSize);
        arrowHead << line().p1() << arrowP1 << arrowP2;

    I really have no clue, please help.
    Thanks in advance!

    (Editted myArrow paint event)

  • Lifetime Qt Champion


    Isn't that your arrow being painted as you are moving your item ?

  • Hi, thanks for the response!

    I'm guessing yes, so it's a problem in my paint event being called too many times? I'm not seeing the problem or a way out of it, here.

  • Lifetime Qt Champion

    Why are you calling update from within paint ?

    Then are you sure that you are painting the same thing each time ?

    Take a look at the notes in the paint method documentation.

  • @SGaist I'm trying to replicate this example with only 1 object (which I thought would be easier, since I'm still learning Qt) :

    The update inside paint event was misplaced but it was not causing the problem. If I take out this line:


    It works fine but I lose the head of my arrow. What really annoys me is that when I alt tab and tab back again to my program the blurry lines disappears, which makes me think its not a problem within my code (?).

Log in to reply