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

  • 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))
        QPen myPen = pen();
        qreal arrowSize = 20;
    //! [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 = + myEndItem->pos();
        polyLine = QLineF(p1, p2);
        QLineF::IntersectType intersectType =
            polyLine.intersect(centerLine, &intersectPoint);
        if (intersectType == QLineF::BoundedIntersection)
            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 << line().p1() << arrowP1 << arrowP2;
    //! [6] //! [7]
            if (isSelected()) {
                painter->setPen(QPen(myColor, 1, Qt::DashLine));
            QLineF myLine = line();
            myLine.translate(0, 4.0);

    see below image:
    i want to it is same broken line same matlabsimulink...below image

    thanks for reply

  • anyone doesn't any idea>please help me

    thanks for reply.

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

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

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.