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. how can draw a line that is created from path mouse but the lines be broken vertical line?
Forum Updated to NodeBB v4.3 + New Features

how can draw a line that is created from path mouse but the lines be broken vertical line?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.3k 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.
  • stackprogramerS Offline
    stackprogramerS Offline
    stackprogramer
    wrote on last edited by stackprogramer
    #1

    hi, i want to between two object in graphicview created a line,that they are broken vertical and horizontal line.i created lines between two objects.
    this is a part source example diagram scene:

    void Arrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
              QWidget *)
    {
        if (myStartItem->collidesWithItem(myEndItem))
            return;
    
        QPen myPen = pen();
        myPen.setColor(myColor);
        qreal arrowSize = 20;
        painter->setPen(myPen);
        painter->setBrush(myColor);
    //! [4] //! [5]
    
        QLineF centerLine(myStartItem->pos(), myEndItem->pos());
        QPolygonF endPolygon = myEndItem->polygon();
        QPointF p1 = endPolygon.first() + myEndItem->pos();
        QPointF p2;
        QPointF intersectPoint;
        QLineF polyLine;
        for (int i = 1; i < endPolygon.count(); ++i) {
        p2 = endPolygon.at(i) + myEndItem->pos();
        polyLine = QLineF(p1, p2);
        QLineF::IntersectType intersectType =
            polyLine.intersect(centerLine, &intersectPoint);
        if (intersectType == QLineF::BoundedIntersection)
            break;
            p1 = p2;
        }
    
        setLine(QLineF(intersectPoint, myStartItem->pos()));
    //! [5] //! [6]
    
        double angle = ::acos(line().dx() / line().length());
        if (line().dy() >= 0)
            angle = (Pi * 2) - angle;
    
            QPointF arrowP1 = line().p1() + QPointF(sin(angle + Pi / 3) * arrowSize,
                                            cos(angle + Pi / 3) * arrowSize);
            QPointF arrowP2 = line().p1() + QPointF(sin(angle + Pi - Pi / 3) * arrowSize,
                                            cos(angle + Pi - Pi / 3) * arrowSize);
    
            arrowHead.clear();
            arrowHead << line().p1() << arrowP1 << arrowP2;
    //! [6] //! [7]
            painter->drawLine(line());
            painter->drawPolygon(arrowHead);
            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);
        }
    
    

    see below image:

    http://www.mediafire.com/view/5apc35l064pd4kp/brokenline.png
    i want to it is same broken line same matlabsimulink...below image
    http://www.mediafire.com/view/z1axz5p7tmlqx8d/linebroken2(2).png#

    thanks for reply

    stackprogramerS 1 Reply Last reply
    0
    • stackprogramerS stackprogramer

      hi, i want to between two object in graphicview created a line,that they are broken vertical and horizontal line.i created lines between two objects.
      this is a part source example diagram scene:

      void Arrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
                QWidget *)
      {
          if (myStartItem->collidesWithItem(myEndItem))
              return;
      
          QPen myPen = pen();
          myPen.setColor(myColor);
          qreal arrowSize = 20;
          painter->setPen(myPen);
          painter->setBrush(myColor);
      //! [4] //! [5]
      
          QLineF centerLine(myStartItem->pos(), myEndItem->pos());
          QPolygonF endPolygon = myEndItem->polygon();
          QPointF p1 = endPolygon.first() + myEndItem->pos();
          QPointF p2;
          QPointF intersectPoint;
          QLineF polyLine;
          for (int i = 1; i < endPolygon.count(); ++i) {
          p2 = endPolygon.at(i) + myEndItem->pos();
          polyLine = QLineF(p1, p2);
          QLineF::IntersectType intersectType =
              polyLine.intersect(centerLine, &intersectPoint);
          if (intersectType == QLineF::BoundedIntersection)
              break;
              p1 = p2;
          }
      
          setLine(QLineF(intersectPoint, myStartItem->pos()));
      //! [5] //! [6]
      
          double angle = ::acos(line().dx() / line().length());
          if (line().dy() >= 0)
              angle = (Pi * 2) - angle;
      
              QPointF arrowP1 = line().p1() + QPointF(sin(angle + Pi / 3) * arrowSize,
                                              cos(angle + Pi / 3) * arrowSize);
              QPointF arrowP2 = line().p1() + QPointF(sin(angle + Pi - Pi / 3) * arrowSize,
                                              cos(angle + Pi - Pi / 3) * arrowSize);
      
              arrowHead.clear();
              arrowHead << line().p1() << arrowP1 << arrowP2;
      //! [6] //! [7]
              painter->drawLine(line());
              painter->drawPolygon(arrowHead);
              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);
          }
      
      

      see below image:

      http://www.mediafire.com/view/5apc35l064pd4kp/brokenline.png
      i want to it is same broken line same matlabsimulink...below image
      http://www.mediafire.com/view/z1axz5p7tmlqx8d/linebroken2(2).png#

      thanks for reply

      stackprogramerS Offline
      stackprogramerS Offline
      stackprogramer
      wrote on last edited by
      #2

      anyone doesn't any idea>please help me

      thanks for reply.

      1 Reply Last reply
      0
      • Ni.SumiN Offline
        Ni.SumiN Offline
        Ni.Sumi
        wrote on last edited by Ni.Sumi
        #3

        Hi @stackprogramer ,

        I have example code for drawing the Dash lines/ dash dot line/ dash dot sot lines. It is example, copied from internet and some of my code. Please download it here. . It draws the line basing on the mouse press, move and release. You can change the origin and last postion in this cide and can use as you wish. I did not set any toolbar menu to give the DashLine/DashDotLine/DashDotDotLine. I just hard coded it. Fin it in the scene.cpp at line "itemToDraw->setPen(QPen(Qt::black, 3, Qt::DashLine));" in void Scene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) function.

        Hopefully, it solves your problem.

        1 Reply Last reply
        0
        • stackprogramerS Offline
          stackprogramerS Offline
          stackprogramer
          wrote on last edited by
          #4

          thanks but i want to broken line ,direct line example i had.

          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