在QT实现画图功能中,如何选中已画出的图形
以下为实现画图的大部分代码,可正常画出矩形、椭圆和直线,求如何选中已画出的图形,并可以选中单个图形修改属性(如边框颜色,填充颜色,线宽等)和移动选中的图形
container.h
QVector<QRect> _line; //直线容器
QVector<QRect> _ellipse; //椭圆容器
QVector<QRect> _rects; //矩形容器
QPoint _begin; //开始坐标
int _lpress; //鼠标按下标志
int _drag; //拖拽标志
int _drawType; //当前绘画类型标志
container.cpp
_lpress = false; //鼠标按下标志
_drawType = 0; //当前绘画类型标志
_drag = 0; //拖拽标志
_width = 1; //线宽
_isfill = 0; //填充标志
_color = QColor(Qt::black); //当前画笔颜色
_fillcolor = QColor(Qt::white); //当前填充颜色
MainWindow.h
```c++
private:
container cont; //容器
protected:
void paintEvent(QPaintEvent*); //重载重绘函数
void mousePressEvent(QMouseEvent*); //重载鼠标按下函数
void mouseReleaseEvent(QMouseEvent*); //重载鼠标松开函数
void mouseMoveEvent(QMouseEvent*); //重载鼠标移动函数
public slots:
void Line(); //直线
void Rects(); //矩形
void Ellipses(); //椭圆
MainWindow.cpp
void MainWindow::paintEvent(QPaintEvent*) //重绘
{
QPicture pic;
QPainter p;
p.begin(&pic);
int ipen = 0, irect = 0, iellipse = 0, iline = 0; //工具索引
for (int c = 0; c < cont._shape.size(); ++c) //循环重绘容器内所有项
{
QPen pen(QColor(cont._colors.at(c))); //每次循环设置画笔
pen.setWidth(cont._widths.at(c)); //线宽
if (cont._isfills.at(c)) //填充
{
QBrush brush(QColor(cont._fillcolors.at(c)));
p.setBrush(brush);
}
else
{
p.setBrush(QBrush(Qt::NoBrush));
}
p.setPen(pen);
if (cont._shape.at(c) == 2) //矩形
{
p.drawRect(cont._rects.at(irect++));
}
else if (cont._shape.at(c) == 3) //椭圆
{
p.drawEllipse(cont._ellipse.at(iellipse++));
}
else if (cont._shape.at(c) == 4) //直线
{
p.drawLine(cont._line.at(iline).topLeft(), cont._line.at(iline).bottomRight());
iline++;
}
}
p.end();
p.begin(this);
p.drawPicture(0,0,pic);
}
void MainWindow::mousePressEvent(QMouseEvent* e) //鼠标按下
{
if (e->button() == Qt::LeftButton) //左键按下
{
if (cont._drawType == 2) //矩形
{
cont._lpress = true;
if (!cont._drag) //非拖拽
{
QRect rect;
cont._rects.append(rect);
QRect& lastRect = cont._rects.last();
lastRect.setTopLeft(e->pos());
cont._colors.append(cont._color);
cont._fillcolors.append(cont._fillcolor);
cont._isfills.append(cont._isfill);
cont._widths.append(cont._width);
cont._shape.append(2);
}
else if (cont._rects.last().contains(e->pos())) //拖拽,鼠标在图形内部
{
cont._begin = e->pos(); //记录起始的坐标
}
}
else if (cont._drawType == 3) //椭圆
{
cont._lpress = true;
if (!cont._drag)
{
QRect rect;
cont._ellipse.append(rect);
QRect& lastEllipse = cont._ellipse.last();
lastEllipse.setTopLeft(e->pos());
cont._colors.append(cont._color);
cont._fillcolors.append(cont._fillcolor);
cont._isfills.append(cont._isfill);
cont._widths.append(cont._width);
cont._shape.append(3);
}
else if (cont._ellipse.last().contains(e->pos()))
{
cont._begin = e->pos();
}
}
else if (cont._drawType == 4) //直线
{
cont._lpress = true;
QRect rect;
cont._line.append(rect);
QRect& lastLine = cont._line.last();
lastLine.setTopLeft(e->pos());
cont._colors.append(cont._color);
cont._fillcolors.append(Qt::black);
cont._isfills.append(0);
cont._widths.append(cont._width);
cont._shape.append(4);
}
}
}
void MainWindow::mouseMoveEvent(QMouseEvent* e) //鼠标移动
{
if (cont._drag && ((cont._drawType == 2 && cont._rects.last().contains(e->pos())) ||
(cont._drawType == 3 && cont._ellipse.last().contains(e->pos())) )) //拖拽中
{
setCursor(Qt::SizeAllCursor); //设置十字光标
}
else
{
setCursor(Qt::ArrowCursor); //恢复原始光标形状
cont._drag = 0;
}
if (cont._lpress)
{
if (cont._drawType == 2) //矩形
{
if (cont._drag == 0) //非拖拽
{
QRect& lastRect = cont._rects.last();
lastRect.setBottomRight(e->pos()); //更新右下角坐标
}
else //拖拽
{
QRect& lastRect = cont._rects.last();
if (lastRect.contains(e->pos())) //在矩形的内部
{
int dx = e->pos().x() - cont._begin.x(); //移动
int dy = e->pos().y() - cont._begin.y();
lastRect = lastRect.adjusted(dx, dy, dx, dy); //更新位置
cont._begin = e->pos(); //刷新拖拽点起始坐标
}
}
update();
}
else if (cont._drawType == 3) //椭圆
{
if (cont._drag == 0)
{
QRect& lastEllipse = cont._ellipse.last(); //拿到椭圆
lastEllipse.setBottomRight(e->pos()); //更新椭圆右下角坐标
}
else
{
QRect& lastEllipse = cont._ellipse.last();
if (lastEllipse.contains(e->pos()))
{
int dx = e->pos().x() - cont._begin.x(); //移动
int dy = e->pos().y() - cont._begin.y();
lastEllipse = lastEllipse.adjusted(dx, dy, dx, dy);
cont._begin = e->pos();
}
}
update();
}
else if (cont._drawType == 4) //直线
{
QRect& lastLine = cont._line.last();
lastLine.setBottomRight(e->pos());
update();
}
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent* e) //鼠标松开
{
if (cont._lpress)
{
if (cont._drawType == 2) //矩形
{
QRect& lastRect = cont._rects.last();
if (!cont._drag) //非拖拽
{
lastRect.setBottomRight(e->pos()); //更新右下角坐标
this->cursor().setPos(this->cursor().pos().x() - lastRect.width() / 2,
this->cursor().pos().y() - lastRect.height() / 2); //光标置图形中心
cont._drag = 1; //拖拽标志
}
cont._lpress = false; //松开标志
}
else if (cont._drawType == 3) //椭圆
{
QRect& lastEllipse = cont._ellipse.last();
if (!cont._drag)
{
lastEllipse.setBottomRight(e->pos());
this->cursor().setPos(this->cursor().pos().x() - lastEllipse.width() / 2,
this->cursor().pos().y() - lastEllipse.height() / 2);
cont._drag = 1;
}
cont._lpress = false;
}
else if (cont._drawType == 4) //直线
{
QRect& lastLine = cont._line.last();
lastLine.setBottomRight(e->pos());
cont._lpress = false;
}
}
}
void MainWindow::Rects()
{
cont._drawType = 2; //矩形
cont._drag = 0;
}
void MainWindow::Ellipses()
{
cont._drawType = 3; //椭圆
cont._drag = 0;
}
void MainWindow::Line()
{
cont._drawType = 4; //直线
}