qt 带箭头的直线 (类似viso)
阅读原文时间:2023年07月12日阅读:3

2020.02.27

想要代码,留邮箱吧。或者到https://download.csdn.net/download/XuePiaoFei1/12195991下载,或者https://files-cdn.cnblogs.com/files/warmlight/NewProject.rar

近来Qt开发时可能遇到这样的需求:两个(或多个)矩形,要用直线将它们连接起来,之后还要把它们保存到xml中,并且能够还原。

类似于下图:

首先想到的就是Qt自带的demo:diagramscene。因为demo中有箭头方向,开始节点和结束节点,连坐标都已经有了,保存还原都可以完成。但是,demo的直线不是竖直或水平的,所以肯定要对demo进行修改。(扯淡了,demo肯定不会符合个人的需求的,无论如何都要修改的。)

刚用Qt不久,网络上也搜不到类似的问题,也许是自己找不到,只能寄希望于Qt的demo,限制了自己的思想。可是目前没有更好的办法。

我的工程有几个要点,要能够保存和还原,箭头的首尾节点肯定要确定,或者说首尾节点的坐标一定要知道。

demo中的基本元素够了,我想只对arrow类作修改,希望能达到目标。

我先把我改造后的arrow类贴出来,然后再试图分析一下。

头文件:

/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef ARROW_H
#define ARROW_H

#include

#include "diagramitem.h"

QT_BEGIN_NAMESPACE
class QGraphicsPolygonItem;
class QGraphicsLineItem;
class QGraphicsScene;
class QRectF;
class QGraphicsSceneMouseEvent;
class QPainterPath;
QT_END_NAMESPACE

enum LineType {
lineType1 = , //横竖
lineType2, //竖横
lineType3, //横竖横
lineType4 //竖横竖
};

//! [0]
class Arrow : public QGraphicsLineItem
{
public:
enum { Type = UserType + };

 Arrow(DiagramItem \*startItem, DiagramItem \*endItem,  
   QGraphicsItem \*parent = );  
 ~Arrow() {m\_bDeleteFlag = true;}

 int type() const override { return Type; }  
 QRectF boundingRect() const override;  
 QPainterPath shape() const override;  
 void setColor(const QColor &color) { myColor = color; }  
 DiagramItem \*startItem() const { return myStartItem; }  
 DiagramItem \*endItem() const { return myEndItem; }

 void setItemId(QString startItemId ,QString startEndId);  
 void updatePosition();

 QString getStartId(){return startId;}  
 QString getEndId(){return endId;}

 void setStartItem(DiagramItem \*startItem){ myStartItem = startItem; }  
 void setEndItem(DiagramItem \*endItem){ myEndItem = endItem; }

 void setArrowFlag(bool IsArrow){bisArrow = IsArrow;}  
 bool getArrowFlag(){return bisArrow;}  
 bool getDeleteFlage() {return m\_bDeleteFlag;}  
 void DrawArrow(QPainter \*painter, QPointF startPt, QPointF endPt, bool bArrow);  
 void ThreeLine(QPainter \*painter, QPointF startPt, QPointF endPt);//三段线  
 void TwoLine(QPainter \*painter, QPointF startPt, QPointF endPt); //两段线  
 //void SetLineStyle(bool bVhvLine);//线段的折线风格  
 //void SetLineType(int nLineType);//线段的数量  
 void SetLineType(LineType lineType);  
 int GetLineType() {return m\_lineType;}  
 void DrawLine(QPainter \*painter, QPointF startPt, QPointF endPt);  

protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = ) override;

private:
DiagramItem *myStartItem;
DiagramItem *myEndItem;
QColor myColor;
QPolygonF arrowHead;
QString startId;
QString endId;
bool bisArrow;
bool m_bDeleteFlag;//
LineType m_lineType;
//bool m_bVhvLine;//true三段线,竖横竖; false三段线 横竖横
//bool m_bVhLine;// true两段线 竖横; false 两段线 横竖
//int m_nLineType;//2-2段线,3-3段线
};

inline void Arrow::setItemId(QString startItemId ,QString startEndId){
startId = startItemId;
endId = startEndId;
}
//! [0]

#endif // ARROW_H

cpp文件:

/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "arrow.h"
#include
#include
#include
#include

//! [0]
Arrow::Arrow(DiagramItem *startItem, DiagramItem *endItem, QGraphicsItem *parent)
: QGraphicsLineItem(parent)
{
m_bDeleteFlag = false;//20191121 sdl
myStartItem = startItem;
myEndItem = endItem;
setFlag(QGraphicsItem::ItemIsSelectable, true);
myColor = Qt::black;
setPen(QPen(myColor, , Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
bisArrow = true;
if(myStartItem){startId = myStartItem->getItemId();}
if(myEndItem){endId = myEndItem->getItemId();}
m_lineType = LineType::lineType1;
}
//! [0]

//! [1]
QRectF Arrow::boundingRect() const
{
qreal extra = (pen().width() + ) / 2.0;

 return QRectF(line().p1(), QSizeF(line().p2().x() - line().p1().x(),  
                                   line().p2().y() - line().p1().y()))  
     .normalized()  
     .adjusted(-extra, -extra, extra, extra);  

}
//! [1]

//! [2]
QPainterPath Arrow::shape() const
{
QPainterPath path = QGraphicsLineItem::shape();
path.addPolygon(arrowHead);
return path;
}
//! [2]

//! [3]
void Arrow::updatePosition()
{
QPointF endPt = mapFromItem(myEndItem, , );
QLineF line(mapFromItem(myStartItem, , ), endPt);
setLine(line);
}

//! [3]

//! [4]
void Arrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
QWidget *)
{
if(myStartItem == NULL || myEndItem == NULL) return;
if(myStartItem->GetDeleteFlag() || myEndItem->GetDeleteFlag()) return;
if (myStartItem->collidesWithItem(myEndItem))
return;

 QPointF start = myStartItem->pos();  
 QPointF end = myEndItem->pos();  
 if(qFuzzyCompare(start.x(), end.x())){ //如果在同一竖直或水平线时,直接连接两节点中心点  
     DrawArrow(painter, start, end, true);  
     return;  
 }  
 if (qFuzzyCompare(start.y(), end.y())) {  
     DrawArrow(painter, start, end, true);  
     return;  
 }

 DrawLine(painter, start, end);  

// if(m_nLineType == 2){
// TwoLine(painter, start, end);
// }
// else if (m_nLineType == 3) {
// ThreeLine(painter, start, end);
// }
}
//! [7]

void Arrow::DrawArrow(QPainter *painter, QPointF startPt, QPointF endPt, bool bArrow)
{
if(endPt == QPointF(, )){
return;
}
QPen myPen = pen();
myPen.setColor(myColor);
qreal arrowSize = ;
painter->setPen(myPen);
painter->setBrush(myColor);
//! [4] //! [5]

 QLineF centerLine(startPt, endPt);

 QPolygonF endPolygon;  
 endPolygon = myEndItem->polygon();  
 QPointF p1 = endPolygon.first() + endPt;  
 QPointF p2;  
 QPointF intersectPoint;  
 QLineF polyLine;  
 for (int i = ; i < endPolygon.count(); ++i) {  
     p2 = endPolygon.at(i) + endPt;  
     polyLine = QLineF(p1, p2);  
     QLineF::IntersectType intersectType =  
         polyLine.intersect(centerLine, &intersectPoint);  
     if (intersectType == QLineF::BoundedIntersection)  
         break;  
     p1 = p2;  
 }

 setLine(QLineF(endPt, startPt/\*myStartItem->pos()\*/));

//! [5] //! [6]
if (bArrow){
setLine(QLineF(intersectPoint, startPt/*myStartItem->pos()*/));//如果是箭头所在的线
}
double angle = std::atan2(-line().dy(), line().dx());

 QPointF arrowP1 = line().p1() + QPointF(sin(angle + M\_PI / ) \* arrowSize,  
                                 cos(angle + M\_PI / ) \* arrowSize);  
 QPointF arrowP2 = line().p1() + QPointF(sin(angle + M\_PI - M\_PI / ) \* arrowSize,  
                                 cos(angle + M\_PI - M\_PI / ) \* arrowSize);

 arrowHead.clear();  
 arrowHead << line().p1() << arrowP1 << arrowP2;  

//! [6] //! [7]
painter->drawLine(line());
//是否画箭头
if(bArrow){
painter->drawPolygon(arrowHead);
}
if (isSelected()) {
painter->setPen(QPen(myColor, , Qt::DashLine));
QLineF myLine = line();
myLine.translate(, 4.0);
painter->drawLine(myLine);
myLine.translate(,-8.0);
painter->drawLine(myLine);
}
}

void Arrow::ThreeLine(QPainter *painter, QPointF start, QPointF end)
{
if(m_lineType == LineType::lineType4){
qreal midY = (start.y() + end.y()) / ;
QPointF startPt = myStartItem->pos();
QPointF endPt = myEndItem->pos();

     endPt.setX(startPt.x());  
     endPt.setY(midY);  
     DrawArrow(painter, startPt, endPt, false);

     startPt = endPt;  
     endPt.setX(myEndItem->pos().x());  
     endPt.setY(startPt.y());  
     DrawArrow(painter, startPt, endPt, false);

     startPt = endPt;  
     endPt = myEndItem->pos();

     DrawArrow(painter, startPt, endPt, true);  
 }  
 else if(m\_lineType == LineType::lineType3){  
     qreal midX = (start.x() + end.x()) / ;  
     QPointF startPt = myStartItem->pos();  
     QPointF endPt = myEndItem->pos();

     endPt.setX(midX);  
     endPt.setY(startPt.y());  
     DrawArrow(painter, startPt, endPt, false);

     startPt = endPt;  
     endPt.setX(startPt.x());  
     endPt.setY(myEndItem->pos().y());  
     DrawArrow(painter, startPt, endPt, false);

     startPt = endPt;  
     endPt = myEndItem->pos();  
     DrawArrow(painter, startPt, endPt, true);  
 }  

}

void Arrow::TwoLine(QPainter *painter, QPointF startPt, QPointF endPt)
{
if(m_lineType == LineType::lineType2){
endPt.setX(startPt.x());
endPt.setY(endPt.y());
DrawArrow(painter, startPt, endPt, false);

     startPt = endPt;  
     endPt = myEndItem->pos();  
     DrawArrow(painter, startPt, endPt, true);  
 }  
 else if(m\_lineType == LineType::lineType1){  
     endPt.setY(startPt.y());  
     DrawArrow(painter, startPt, endPt, false);

     startPt = endPt;  
     endPt = myEndItem->pos();  
     DrawArrow(painter, startPt, endPt, true);  
 }  

}

//void Arrow::SetLineStyle(bool bVhvLine)
//{
// m_bVhvLine = bVhvLine;
//}

void Arrow::SetLineType(LineType lineType)
{
if(lineType > lineType4 || lineType < lineType1)
m_lineType = lineType1;
else{
m_lineType = lineType;
}
}

void Arrow::DrawLine(QPainter *painter, QPointF start, QPointF end)
{
if(m_lineType == LineType::lineType4){
qreal midY = (start.y() + end.y()) / ;
QPointF startPt = myStartItem->pos();
QPointF endPt = myEndItem->pos();

     endPt.setX(startPt.x());  
     endPt.setY(midY);  
     DrawArrow(painter, startPt, endPt, false);

     startPt = endPt;  
     endPt.setX(myEndItem->pos().x());  
     endPt.setY(startPt.y());  
     DrawArrow(painter, startPt, endPt, false);

     startPt = endPt;  
     endPt = myEndItem->pos();

     DrawArrow(painter, startPt, endPt, true);  
 }  
 else if(m\_lineType == LineType::lineType3){  
     qreal midX = (start.x() + end.x()) / ;  
     QPointF startPt = myStartItem->pos();  
     QPointF endPt = myEndItem->pos();

     endPt.setX(midX);  
     endPt.setY(startPt.y());  
     DrawArrow(painter, startPt, endPt, false);

     startPt = endPt;  
     endPt.setX(startPt.x());  
     endPt.setY(myEndItem->pos().y());  
     DrawArrow(painter, startPt, endPt, false);

     startPt = endPt;  
     endPt = myEndItem->pos();  
     DrawArrow(painter, startPt, endPt, true);  
 }  
 else if(m\_lineType == LineType::lineType2){  
     end.setX(start.x());  
     end.setY(end.y());  
     DrawArrow(painter, start, end, false);

     start = end;  
     end = myEndItem->pos();  
     DrawArrow(painter, start, end, true);  
 }  
 else if(m\_lineType == LineType::lineType1){  
     end.setY(start.y());  
     DrawArrow(painter, start, end, false);

     start = end;  
     end = myEndItem->pos();  
     DrawArrow(painter, start, end, true);  
 }  
 else {//默认是linetype1  
     m\_lineType = LineType::lineType1;  
     end.setY(start.y());  
     DrawArrow(painter, start, end, false);

     start = end;  
     end = myEndItem->pos();  
     DrawArrow(painter, start, end, true);  
 }  

}

这个类中,有一些代码冗余。

我在这里分了四类画线方式,即头文件中的LineType。在横竖横和竖横竖的类型中,中间线段取的是中点画线。箭头首尾的节点都有了,保存也可以的。也可以根据个人情况修改箭头类。

操作:编辑图形时,基本图形时拖拽到场景中的。连线不是拖拽,是单击选中一种类型,在场景中连接两个基本图形即可。选中一个画线方式,再次拖拽基本图形或右键鼠标后,需要再次选择画线方式,才能再次画线。右键鼠标会出现右键菜单,打开、保存、删除。保存,保存场景中图形为xml;打开,打开保存的xml文件;删除,可删除基本图形或者箭头。注意:删除基本图形时,如果基本图形有连线,连线也会被删除。

还有一些功能待完善,如修改基本图形的名称等。

结果图:

稍后把代码传到csdn。如果博客园能上传代码就好了。

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章