标签:
#include<vector>
#include<shape.h>//后面会讲到
* 添加成员函数
```cpp
protected:
void paintEvent(QPaintEvent *event);//绘图事件,用update()调用
void mousePressEvent(QMouseEvent *event);//鼠标按下
void mouseMoveEvent(QMouseEvent *event);//鼠标移动
void mouseReleaseEvent(QMouseEvent *event);//鼠标释放
class Shape
{
public:
enum drawMode//绘图模式
{
LINE,
RECT,
ELLIPSE,
POLYGON,
FREEHAND,
DELETE,
DEFAULT
};
Shape();
virtual ~Shape();//虚函数,防止内存泄漏,具体见后面
void setStart(QPoint start)
{
this->start=start;
}
void setEnd(QPoint end)
{
this->end=end;
}
QPoint getStart()
{
return start;
}
QPoint getEnd()
{
return end;
}
void virtual paint(QPainter &painter)=0;//需要在子类中重写的函数设置为虚函数
protected:
QPoint start,end;
};
```cpp
//shape.cpp
#include "shape.h"
Shape::Shape()
{
}
2.1创建子类line、Rectangle、Ellipse、Polygon、FreeHand——主要是重写paint函数
* **rectangle**
```cpp
void Rectangle::paint(QPainter &painter)
{
painter.drawRect(start.x(),start.y(),end.x()-start.x(),end.y()-start.y());
}
void Ellipse::paint(QPainter &painter)
{
painter.drawEllipse(start.x(),start.y(),end.x()-start.x(),end.y()-start.y());
}
以上这些都很简单
class FreeHand:public Shape
{
public:
FreeHand();
FreeHand(const QVector
void paint(QPainter &painter);
protected:
QVector
};
```cpp
//构造函数和绘制函数的实现
FreeHand::FreeHand(const QVector<QPoint> &point_array)
{
for(size_t i=0;i<point_array.size();i++)
{
//free_point_array[i]=point_array[i];
free_point_array.push_back(point_array[i]);
}
}
void FreeHand::paint(QPainter &painter)
{
for(size_t i=0;i<free_point_array.size()-1;i++)
{
painter.drawLine(free_point_array[i],free_point_array[i+1]);
}
}
**以上两个是比较难的,需要考虑一下**
**3整体逻辑——对成员函数的实现**
**3.1给按钮添加槽函数**
(这里我是采用Qt Creator 添加的,这不是我们讲的重点)
```cpp
void MainWindow::on_actionLine_triggered()
{
mode=Shape::drawMode::LINE;
}
void MainWindow::on_actionRectangle_triggered()
{
mode=Shape::drawMode::RECT;
}
void MainWindow::on_actionEllipse_triggered()
{
mode=Shape::drawMode::ELLIPSE;
}
void MainWindow::on_actionPolygon_triggered()
{
mode=Shape::drawMode::POLYGON;
}
void MainWindow::on_actionFreehand_triggered()
{
mode=Shape::drawMode::FREEHAND;
}
void MainWindow::on_actionDelete_triggered()
{
mode=Shape::drawMode::DELETE;
if(!shapeList.isEmpty())
{
shapeList.pop_back();//最后一个图形对象出栈
mode=Shape::DEFAULT;//设置模式为默认模式
update();
}
}
逻辑就是点击不同的按钮进入不同的绘制模式
3.2给鼠标按键添加监听
```cpp
//鼠标按下
void MainWindow::mousePressEvent(QMouseEvent *event)
{
switch (event->button()) {
case Qt::LeftButton ://在绘制直线、矩形、多边形、椭圆、曲线时有效,开始绘制
switch (mode) {
case Shape::DEFAULT:
break;
case Shape::DELETE:
//do nothing
break;
case Shape::LINE:
shape=new line;
break;
case Shape::RECT:
shape=new Rectangle;
break;
case Shape::ELLIPSE:
shape=new Ellipse;
break;
case Shape::FREEHAND:
shape=new FreeHand;
break;
case Shape::POLYGON:
shape=new Polygon;
break;
default:
break;
}
break;
case Qt::RightButton: //仅在绘制多边形时有效,结束绘制
if(mode==Shape::POLYGON)
{
isDone=true;
if(polygon_point_array.size()>0)
{
shape=new Polygon(polygon_point_array);
shapeList.push_back(shape);
polygon_point_array.clear();
update();
shape=NULL;
}
}
break;
default:
break;
}
if(shape!=NULL){//刚开始绘制
if(mode==Shape::POLYGON)//设置转折点
{
isDone=false;
polygon_point_array.push_back(event->pos());//添加进锚点
freePoint=event->pos();
update();
}else if(mode==Shape::FREEHAND)
{
isDone=false;
freehand_point_array.push_back(event->pos());//添加进锚点
}
else {
isDone=false;
shape->setStart(event->pos());
shape->setEnd(event->pos());
shapeList.push_back(shape);//将图形对象入栈
}
}
}
```cpp
//鼠标移动
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
if(shape&&!isDone)
{
if(mode==Shape::FREEHAND)
{
freehand_point_array.push_back(event->pos());//添加曲线点序列
update();
}else
{
shape->setEnd(event->pos());
freePoint=event->pos();
update();
}
}
}
//鼠标释放
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)//仅在左键释放时有效,结束绘制直线、矩形、椭圆、曲线
{
switch (mode) {
case Shape::POLYGON:
break;
case Shape::FREEHAND:
isDone=true;
shape=new FreeHand(freehand_point_array);
freehand_point_array.clear();
shapeList.push_back(shape);
update();
shape=NULL;
break;
default:
isDone=true;
update();
shape=NULL;
break;
}
}
}
以上
标签:
原文地址:http://www.cnblogs.com/LingjieLi-vera/p/d0b86630f939da87ff0e04249df9bf87.html