场景类
cgraphicsscene.h
#ifndef CGRAPHICSSCENE_H
#define CGRAPHICSSCENE_H
#include <QGraphicsScene>
#include <QGraphicsPathItem>
#include <QList>
#include "cgraphicsview.h"
class CGraphicsScene : public QGraphicsScene
{
Q_OBJECT
public:
enum Type
{
T_None,
T_Point,
T_Line,
T_Circle,
T_Ellipse,
T_Rect,
T_RoundRect
};
enum State
{
S_None,
S_Begin,
S_Moving,
S_End,
};
CGraphicsScene(QObject *parent = 0);
public slots:
void OnItemCreate(QPointF beginPos,QPointF endPos,int type);
private:
QList<QGraphicsPathItem*> m_pathList;
};
#endif // CGRAPHICSSCENE_H
cgraphicsscene.cpp
#include "cgraphicsscene.h"
CGraphicsScene::CGraphicsScene(QObject *parent)
: QGraphicsScene(parent)
{
setBackgroundBrush(Qt::gray);
}
void CGraphicsScene::OnItemCreate(QPointF beginPos, QPointF endPos, int type)
{
QGraphicsPathItem *item = new QGraphicsPathItem;
item->setPen(QPen(Qt::blue,2));
int w = endPos.x() - beginPos.x();
int h = endPos.y() - beginPos.y();
switch (type)
{
case T_Point:
break;
case T_Line:
{
QPainterPath path;
path.moveTo(beginPos);
path.lineTo(endPos);
item->setPath(path);
}
break;
case T_Circle:
{
QPainterPath path;
path.addEllipse(beginPos.x(),beginPos.y(),w,h);
item->setPath(path);
}
break;
case T_Ellipse:
{
QPainterPath path;
path.addEllipse(beginPos.x(),beginPos.y(),w,h);
item