Chinaunix首页 | 论坛 | 博客
  • 博客访问: 286816
  • 博文数量: 46
  • 博客积分: 6495
  • 博客等级: 大校
  • 技术积分: 483
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-09 16:36
文章分类

全部博文(46)

文章存档

2013年(1)

2012年(1)

2011年(6)

2010年(14)

2009年(1)

2008年(1)

2007年(22)

分类: C/C++

2013-03-21 10:31:51

最近由于要做一个桌面软件,使用Qt技术,需要用到折线。
在Qt原有的库中没有发现现成的折线方法(或许是我笨,没有找到)。于是,自定义了一套折线的类。主要是利用了三段线来组织折线。

见图:





废话不多说,见代码。

代码中用到了一些软件中的其他类,不影响阅读,仅供参考。
polyline.h
 
 
#ifndef POLYLINE_H
#define POLYLINE_H

#include 

#include "diagramitem.h"
#include "public.h"

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


class Polyline
{
public:
 ~Polyline();
 Polyline(DiagramItem *startItem, DiagramItem *endItem,
 QGraphicsItem *parent = 0, QGraphicsScene *scene = 0);
 vector<QGraphicsLineItem*> vectorLines;

private:
 
 void updatePosition();
 struct polyPoints myPoints;
 void getPoint();
 void getNewPoint();
 DiagramItem *myStartItem;
 DiagramItem *myEndItem;
};

#endif // POLYLINE_H

 
 
 
polyline.cpp
 
 
#include "polyline.h"
#include 


Polyline::~Polyline()
{
 QGraphicsLineItem *item;
 vector<QGraphicsLineItem*>::iterator lineItem;
 for(lineItem = vectorLines.begin(); lineItem != vectorLines.end(); lineItem++) {
 item = (QGraphicsLineItem*)*lineItem;
 delete item;
 }

 vectorLines.clear();
}


Polyline::Polyline(DiagramItem *startItem, DiagramItem *endItem, QGraphicsItem *parent, QGraphicsScene *scene)
{
 myStartItem = startItem;
 myEndItem = endItem;

 getPoint();

 QGraphicsLineItem *item = new QGraphicsLineItem(myPoints.top_x, myPoints.top_y,
 myPoints.top_x, myPoints.mid_y,
 parent, scene);
 item->setFlag(QGraphicsItem::ItemIsSelectable, true);
 item->setPen(QPen(Qt::black, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
 item->setZValue(-1);
 vectorLines.push_back(item);

 item = new QGraphicsLineItem(myPoints.top_x, myPoints.mid_y,
 myPoints.buttom_x, myPoints.mid_y,
 parent, scene);
 item->setFlag(QGraphicsItem::ItemIsSelectable, true);
 item->setPen(QPen(Qt::black, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
 item->setZValue(-1);
 vectorLines.push_back(item);

 item = new QGraphicsLineItem(myPoints.buttom_x, myPoints.mid_y,
 myPoints.buttom_x, myPoints.buttom_y,
 parent, scene);
 item->setFlag(QGraphicsItem::ItemIsSelectable, true);
 item->setPen(QPen(Qt::black, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
 item->setZValue(-1);
 vectorLines.push_back(item);
}

void Polyline::updatePosition()
{
 getNewPoint();

 QLineF line(myPoints.top_x, myPoints.top_y,
 myPoints.top_x, myPoints.mid_y);
 vectorLines.at(0)->setLine(line);

 QLineF line1(myPoints.top_x, myPoints.mid_y,
 myPoints.buttom_x, myPoints.mid_y);
 vectorLines.at(1)->setLine(line1);

 QLineF line2(myPoints.buttom_x, myPoints.mid_y,
 myPoints.buttom_x, myPoints.buttom_y);
 vectorLines.at(2)->setLine(line2);
}


void Polyline::getPoint()
{
 float w ;
 float h ;
 w = myStartItem->pixmap().width();
 h = myStartItem->pixmap().height();

 myPoints.top_x = myStartItem->point()->rx() + w / 2;
 myPoints.top_y = myStartItem->point()->ry() + h / 2;
 myPoints.buttom_x = myEndItem->point()->rx() + w / 2;
 myPoints.buttom_y = myEndItem->point()->ry() + h / 2;

 myPoints.mid_x = (myPoints.buttom_x - myPoints.top_x) / 2 + myPoints.top_x;
 myPoints.mid_y = (myPoints.buttom_y - myPoints.top_y) / 2 + myPoints.top_y;
}

void Polyline::getNewPoint()
{
 float w ;
 float h ;
 w = myStartItem->pixmap().width();
 h = myStartItem->pixmap().height();

 QPointF point1 = this->vectorLines.at(0)->mapFromItem(myStartItem, 0, 0);
 QPointF point2 = this->vectorLines.at(0)->mapFromItem(myEndItem, 0, 0);

 myPoints.top_x = point1.x() + w / 2;
 myPoints.top_y = point1.y() + h / 2;;
 myPoints.buttom_x = point2.x() + w / 2;
 myPoints.buttom_y = point2.y() + h / 2;;

 myPoints.mid_x = (myPoints.buttom_x - myPoints.top_x) / 2 + myPoints.top_x;
 myPoints.mid_y = (myPoints.buttom_y - myPoints.top_y) / 2 + myPoints.top_y;
}

备注:系统CU博客系统提供的代码插入标签太难用了,效果很难看。
阅读(5843) | 评论(0) | 转发(0) |
0

上一篇:Qt同一个signal多次激发单个slots的执行顺序

下一篇:没有了

给主人留下些什么吧!~~