c/c++语言开发共享Qt实现画笔功能

用qt实现在窗口上画线,类似于画笔功能。头文件#ifndef mypaint_h__#define mypaint_h__#include <qtwidgets/qwidget>class

用qt实现在窗口上画线,类似于画笔功能。

头文件

#ifndef mypaint_h__  #define mypaint_h__    #include <qtwidgets/qwidget>    class mypaint :public qwidget  {      q_object  public:      mypaint(qwidget *parent = nullptr);        void mousepressevent(qmouseevent *event);      void mousemoveevent(qmouseevent *event);      void mousereleaseevent(qmouseevent *event);      void paintevent(qpaintevent *event);    private:      std::vector<std::vector<qpoint>> _lines;    };    #endif // mypaint_h__

实现

#include "mypaint.h"  #include <qpainter>  #include <qmouseevent>      mypaint::mypaint(qwidget *parent):qwidget(parent)  {    }    void mypaint::mousepressevent(qmouseevent *event) //鼠标按下的时候新增加一条线,并记录起点  {      std::vector<qpoint> line;        line.push_back(event->pos());      _lines.push_back(line);  }    void mypaint::mousemoveevent(qmouseevent *event)//鼠标移动的时候新增加点  {      auto &line = _lines[_lines.size() - 1];      line.push_back(event->pos());        update(); //更新,重新绘制窗口,自动调用paintevent  }    void mypaint::mousereleaseevent(qmouseevent *event)  {      auto &line = _lines[_lines.size() - 1];      line.push_back(event->pos());  }    void mypaint::paintevent(qpaintevent *event) //绘制所有的线  {      //qpainter painter(this);      //painter.setpen(qpen(qt::red, 3, qt::dashline));      //painter.setbrush(qt::blue);      //painter.drawrect(qrect(10, 10, 100, 30));      //painter.drawellipse(130, 10, 50, 50);      //painter.drawline(qline(200, 10, 300, 20));      //painter.drawtext(qpoint(10, 70), "hello" );      qpainter painter(this);      painter.setpen(qpen(qt::green, 3));      for (const auto &line : _lines)      {          for (int i=0;i<line.size()-1;i++)          {              painter.drawline(line.at(i), line.at(i + 1));          }      }  }

效果

Qt实现画笔功能

以上就是c/c++开发分享Qt实现画笔功能的全部内容,希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。

需要了解更多c/c++开发分享Qt实现画笔功能,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/1237245.html

(0)
上一篇 2022年9月7日
下一篇 2022年9月7日

精彩推荐