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. Weird behaviour when dragging QGraphicsItem
Forum Updated to NodeBB v4.3 + New Features

Weird behaviour when dragging QGraphicsItem

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 791 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.
  • MatheusIHM Offline
    MatheusIHM Offline
    MatheusIH
    wrote on last edited by MatheusIH
    #1

    Here's before I drag:
    0_1527790290357_pic1.png

    Here's after I drag it:
    0_1527790307920_pic2.png

    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:

    //rectItem
    customRect::customRect(const QRectF &rect) : QGraphicsRectItem(rect){
        setFlag(QGraphicsItem::ItemIsMovable);
        setFlag(QGraphicsItem::ItemSendsScenePositionChanges);
    }
    
    void customRect::addLine(myArrow *line) {
        arrows.append(line);
        this->line = line;
    }
    
    QVariant customRect::itemChange(GraphicsItemChange change, const QVariant &value)
    {
        if (change == ItemPositionChange && scene()) {
            QPointF newPos = value.toPointF();
            moveLineToCenter(newPos);
        }
        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);
            arrow->updatePosition(p1,p2);
            update();
        }
    }
    
    //arrowItem
    myArrow::myArrow(customRect *rect){
        myrect = rect;
        QPointF p = rect->boundingRect().topLeft();
        p.setY(p.y() - 200);
        setLine( QLineF( p , rect->boundingRect().topLeft() ));
        setFlag(QGraphicsLineItem::ItemIsSelectable);
    }
    
    void myArrow::updatePosition(QPointF p1, QPointF p2)
    {
        QLineF line(p2,p1);
        setLine(line);
    }
    
    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.clear();
        arrowHead << line().p1() << arrowP1 << arrowP2;
    
        painter->drawLine(line());
        painter->drawPolygon(arrowHead);
    }
    

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

    (Editted myArrow paint event)

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

      Hi,

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

      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
      • MatheusIHM Offline
        MatheusIHM Offline
        MatheusIH
        wrote on last edited by
        #3

        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.

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

          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.

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

          MatheusIHM 1 Reply Last reply
          0
          • SGaistS SGaist

            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.

            MatheusIHM Offline
            MatheusIHM Offline
            MatheusIH
            wrote on last edited by
            #5

            @SGaist I'm trying to replicate this example with only 1 object (which I thought would be easier, since I'm still learning Qt) : http://doc.qt.io/qt-5/qtwidgets-graphicsview-diagramscene-arrow-cpp.html

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

            painter->drawPolygon(arrowHead)
            

            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 (?).

            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