求教绘图工具中几个功能的Qt实现

代码至上 2013-12-11 06:37:19
以下为win7自带的画图软件


求教如何实现红框框标注的功能:复制黏贴剪切 选择 填充颜色 添加文本
讲一下实现思路和所用类以及函数就好(如果能写一点例子代码更好)


自己的思路:
对于选择 还真一点思路都没有,应该要用到鼠标事件把?
复制黏贴剪切好像有个类QClipboard,六个函数setText(),setImage()和setPixmap(),text(),image()和pixmap()。
对于填充颜色,好像需要保存已画过的图形的坐标?比如矩形则要保存四个点?然后判断鼠标是否在坐标围成的曲线内,然后调用void QPainter::drawPath ( const QPainterPath & path )
void QPainter::fillPath ( const QPainterPath & path, const QBrush & brush ) 这两个函数么

添加文本 QLineEdit? 鼠标事件,点击,得到点击位置坐标,然后用move函数将editLine移到该坐标,再show出?
...全文
373 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
LinWM_csdn 2015-10-22
  • 打赏
  • 举报
回复
楼主,解决了吗?跟我说下思路吧?
代码至上 2013-12-16
  • 打赏
  • 举报
回复
没人答,是给分太低了么。。。
代码至上 2013-12-12
  • 打赏
  • 举报
回复
引用 1 楼 ybjx111 的回复:
选择的话应该是一个类似于截屏的过程,不过截是画布的屏。当拖动选择的区域的时候可以把选择的画布区域图像截取出来保存起来,并把画布相应的区域画成白色。
你可以看看QMimeData这个类,这个类是在鼠标拖动的时候使用的,它可以设置鼠标拖动的时候鼠标的样式,比如你拖动一个文件时看到的你个小加号,你可以把刚刚截取的那个图片设置到这个类实例中去,qt设计器中的控件拖动就是使用这个方法实现的。
当释放鼠标的时候把图片再画一下就可以了。

对于填充色可能不是使用记录绘制的点就可以的,因为填充颜色是把颜色一样的连续地方都填充的,所以应该是计算出的区域,最后的绘制应该是使用fillPath



对选择复制黏贴剪切,我写了以下代码,不过只实现了选择,复制剪切黏贴没反应。。麻烦帮忙看下哪错了,谢谢。

void PaintArea::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.scale(scale,scale);

if(isDrawing)
painter.drawImage(0,0,tempImage);
if(penStyle==Qt::DotLine)
painter.drawImage(0,0,tempImage);
else
painter.drawImage(0,0,image);
}
void PaintArea::mousePressEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{


lastPoint=event->pos();
isDrawing=true;


}
}
void PaintArea::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons()&Qt::LeftButton)
{

endPoint=event->pos();
if(curShape==None)
{
isDrawing=false;
paint(image);
}
else
{
tempImage=image;
paint(tempImage);
}
}
}

void PaintArea::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{



endPoint=event->pos();
isDrawing=false;
if(penStyle==Qt::DotLine)
{
tempImage=image;
paint(tempImage);
}
else
paint(image);
}
}
void PaintArea::paint(QImage&theImage)
{
QPainter pp(&theImage);
QPen pen=QPen();
pen.setColor(penColor);
pen.setStyle(penStyle);
pen.setWidth(penWidth);
if(penStyle!=Qt::DotLine)
brushColor=curBrushColor;
QBrush brush=QBrush(brushColor);
pp.setPen(pen);
pp.setBrush(brush);
int x,y,w,h;
x=lastPoint.x()/scale;
y=lastPoint.y()/scale;
w=endPoint.x()/scale-x;
h=endPoint.y()/scale-y;
switch(curShape)
{
case None:
{
pp.drawLine(lastPoint/scale,endPoint/scale);
lastPoint=endPoint;
break;
}
case Line:
{
pp.drawLine(lastPoint/scale,endPoint/scale);
break;
}
case Rectangle:
{
pp.drawRect(x,y,w,h);
break;
}
case Ellipse:
{
pp.drawEllipse(x,y,w,h);
}
}

update();
modified=true;
}
void PaintArea::setChooseCursor()
{
setCursor(QCursor(Qt::CrossCursor));
setPenStyle(Qt::DotLine);
curShape=Rectangle;
curBrushColor=brushColor;
brushColor=Qt::transparent;
}
void PaintArea::copy()
{

int x,y,w,h;
x=lastPoint.x()/scale;
y=lastPoint.y()/scale;
w=endPoint.x()/scale-x;
h=endPoint.y()/scale-y;
QImage copyImage=image.copy(x,y,w,h);
cb->setImage(copyImage);
}
void PaintArea::paste()
{
QImage pasteImage=cb->image();
QPainter painter(&pasteImage);
painter.drawImage(0,0,image);

}
void PaintArea::cut()
{
int x,y,w,h;
x=lastPoint.x()/scale;
y=lastPoint.y()/scale;
w=endPoint.x()/scale-x;
h=endPoint.y()/scale-y;
QImage copyImage=image.copy(x,y,w,h);
cb->setImage(copyImage);



QColor curColor =penColor;
penColor=Qt::white;
penStyle=Qt::SolidLine;
brushColor=Qt::white;



paint(image);


penStyle=Qt::DotLine;
penColor=curColor;
brushColor=curBrushColor;

}



填充颜色涉及到算法?递归?遍历?感觉好像很麻烦的样子。。。是不是从鼠标点击处向周围搜索,碰到不一样的颜色,停止搜索,返回点坐标,然后所有点坐标连起来形成path?
ybjx111 2013-12-12
  • 打赏
  • 举报
回复
选择的话应该是一个类似于截屏的过程,不过截是画布的屏。当拖动选择的区域的时候可以把选择的画布区域图像截取出来保存起来,并把画布相应的区域画成白色。 你可以看看QMimeData这个类,这个类是在鼠标拖动的时候使用的,它可以设置鼠标拖动的时候鼠标的样式,比如你拖动一个文件时看到的你个小加号,你可以把刚刚截取的那个图片设置到这个类实例中去,qt设计器中的控件拖动就是使用这个方法实现的。 当释放鼠标的时候把图片再画一下就可以了。 对于填充色可能不是使用记录绘制的点就可以的,因为填充颜色是把颜色一样的连续地方都填充的,所以应该是计算出的区域,最后的绘制应该是使用fillPath

16,203

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧