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. Problem with QGraphicsLineItem, it keeps "flashing"

Problem with QGraphicsLineItem, it keeps "flashing"

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

    Hi,
    In my program i can connect lines between objects(QGraphicItem) in a diagramscene
    First i drew the lines between the objects position(object->pos()), but i wanted to connect the lines in the center of the object. So the program draws a line using the position (object->pos()+p), P is a QPointF.

    But now i have a problem, when i move the objects and the line keeps updating, it flashes. Like it follows the position
    object->pos()+p but sometimes for some miliseconds it comeback to the object->pos()

    I don't know if i'm being clear...
    https://www.youtube.com/watch?v=MLcGfTGzFOw
    here is a video that i recorded with my cellphone

    Diagramscene.cpp

    void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
    {
             ...            
    case InsertLine:
                line = new QGraphicsLineItem(QLineF(mouseEvent->scenePos(),
                                            mouseEvent->scenePos()));
                line->setPen(QPen(myLineColor, 2));
                addItem(line);
                break;
             ...
    }
    void DiagramScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
    {
        if (myMode == InsertLine && line != 0) {
            QLineF newLine(line->line().p1(), mouseEvent->scenePos());
            line->setLine(newLine);
        } else if (myMode == MoveItem) {
            QGraphicsScene::mouseMoveEvent(mouseEvent);
        }
    }
    

    Arrow.cpp(that's the class that handles the line function)

    void Arrow::updatePosition()
    {
        QLineF line(mapFromItem(myStartItem,p1), mapFromItem(myEndItem,p2));//p1 and p2 here
        setLine(line);
        update();
    }
    
    void Arrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
              QWidget *)
    {
        if (myStartItem->collidesWithItem(myEndItem))
            return;
    
        QPen myPen = pen();
        myPen.setColor(myColor);
        painter->setPen(myPen);
        painter->setBrush(myColor);
        QPointF start(myStartItem->pos()+p1);
        QPointF end(myEndItem->pos()+p2);//trid to add the points here, but nothing happens
    
        
        QLineF centerLine(myStartItem->pos(), myEndItem->pos());
    
        painter->drawLine(line());
        setLine(QLineF(start+p1,end+p2));
        if (isSelected()) {
            painter->setPen(QPen(myColor, 1, Qt::DashLine));
            QLineF myLine = line();
            myLine.translate(0, 4.0);
            painter->drawLine(myLine);
            myLine.translate(0,-8.0);
            painter->drawLine(myLine);
        }
    }
    

    i don't have any ideia how to fix it, can somebody help me please?

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

      Hi,

      Are you sure you are not replacing the line on each move ?

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

        how can i be certain?that's everything related to moving

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

          You can add a qDebug statement so you know where it's going.

          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
          • F Offline
            F Offline
            frnklu20
            wrote on last edited by frnklu20
            #5

            I don't know why but if i use just

            setLine(QLineF(myStartItem->pos(),myEndItem->pos()));
            

            instead of

            setLine(QLineF(myStartItem->pos()+p,myEndItem->pos()+p));
            //p is abitrary point
            

            it works just fine, doesn't flashes

            and it makes no sense to me

            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