找回密码
 立即注册
收起左侧

Qt编写自定义控件7-自定义可拖动多边形

0
回复
3976
查看
[复制链接]
累计签到:7 天
连续签到:1 天
来源: 2019-4-24 20:58:12 显示全部楼层 |阅读模式
前言
自定义可拖动多边形控件,原创作者是赵彦博(QQ:408815041 zyb920@hotmail.com),创作之初主要是为了能够在视频区域内用户自定义可拖动的多个区域,即可用来作为警戒区域,也可用来其他的处理,拿到对应的多边形坐标集合,本控件的主要难点是如何计算一个点在一个多边形区域内,何时完成一个多边形区域,支持多个多边形。

实现的功能
* 1:自定义随意绘制多边形
* 2:产生闭合形状后可单击选中移动整个多边形
* 3:可拉动某个点
* 4:支持多个多边形
* 5:鼠标右键退出绘制
* 6:可设置各种颜色

效果图

头文件代码
  1. #ifndef CUSTOMGRAPHICS_H
  2. #define CUSTOMGRAPHICS_H

  3. /**
  4. * 自定义多边形控件 作者:赵彦博(QQ:408815041 zyb920@hotmail.com) 2019-3-28
  5. * 1:自定义随意绘制多边形
  6. * 2:产生闭合形状后可单击选中移动整个多边形
  7. * 3:可拉动某个点
  8. * 4:支持多个多边形
  9. * 5:鼠标右键退出绘制
  10. * 6:可设置各种颜色
  11. */

  12. #include <QWidget>

  13. #ifdef quc
  14. #if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
  15. #include <QtDesigner/QDesignerExportWidget>
  16. #else
  17. #include <QtUiPlugin/QDesignerExportWidget>
  18. #endif

  19. class QDESIGNER_WIDGET_EXPORT CustomGraphics : public QWidget
  20. #else
  21. class CustomGraphics : public QWidget
  22. #endif

  23. {
  24.     Q_OBJECT
  25.     Q_PROPERTY(bool selectDotVisible READ getSelectDotVisible WRITE setSelectDotVisible)
  26.     Q_PROPERTY(int dotRadius READ getDotRadius WRITE setDotRadius)
  27.     Q_PROPERTY(int lineWidth READ getLineWidth WRITE setLineWidth)

  28.     Q_PROPERTY(QColor dotColor READ getDotColor WRITE setDotColor)
  29.     Q_PROPERTY(QColor lineColor READ getLineColor WRITE setLineColor)
  30.     Q_PROPERTY(QColor polygonColor READ getPolygonColor WRITE setPolygonColor)
  31.     Q_PROPERTY(QColor selectColor READ getSelectColor WRITE setSelectColor)

  32. public:
  33.     typedef struct {
  34.         QVector<QPoint> pos;
  35.         bool selected;
  36.     } Polygon;

  37.     explicit CustomGraphics(QWidget *parent = 0);

  38. protected:
  39.     void mousePressEvent(QMouseEvent *e);
  40.     void mouseMoveEvent(QMouseEvent *e);
  41.     void mouseReleaseEvent(QMouseEvent *e);
  42.     void paintEvent(QPaintEvent *);
  43.     void drawPolygon(QPainter *p, const Polygon &v);
  44.     void drawLines(QPainter *p, const QList<QPoint> &list, bool isFirst = true);

  45. private:
  46.     bool selectDotVisible;      //选中点可见
  47.     int dotRadius;              //点的半径
  48.     int lineWidth;              //线条宽度

  49.     QColor dotColor;            //点的颜色
  50.     QColor lineColor;           //线条颜色
  51.     QColor polygonColor;        //多边形颜色
  52.     QColor selectColor;         //选中颜色

  53.     QPoint tempPoint;           //临时点
  54.     QList<QPoint> tempPoints;   //点集合
  55.     QList<Polygon> tempPolygons;//多边形集合

  56.     bool pressed;               //鼠标是否按下
  57.     QPoint lastPoint;           //鼠标按下处的坐标
  58.     QPoint ellipsePos;          //保存按下点的坐标
  59.     int selectedEllipseIndex;   //选中点的index
  60.     Polygon pressedPolygon;     //保存按下时多边形的原始坐标
  61.     int selectedIndex;          //选中多边形的index

  62. private:
  63.     //计算两点间的距离
  64.     double length(const QPoint &p1, const QPoint &p2);
  65.     //检测是否选中多边形
  66.     bool checkPoint(const QVector<QPoint> &points, int x, int y);

  67. public:
  68.     bool getSelectDotVisible()  const;
  69.     int getDotRadius()          const;
  70.     int getLineWidth()          const;

  71.     QColor getDotColor()        const;
  72.     QColor getLineColor()       const;
  73.     QColor getPolygonColor()    const;
  74.     QColor getSelectColor()     const;

  75.     QSize sizeHint()            const;
  76.     QSize minimumSizeHint()     const;

  77. public Q_SLOTS:
  78.     void setSelectDotVisible(bool selectDotVisible);
  79.     void setDotRadius(int dotRadius);
  80.     void setLineWidth(int lineWidth);

  81.     void setDotColor(const QColor &dotColor);
  82.     void setLineColor(const QColor &lineColor);
  83.     void setPolygonColor(const QColor &polygonColor);
  84.     void setSelectColor(const QColor &selectColor);

  85.     //清除临时绘制的
  86.     void clearTemp();
  87.     //清除所有
  88.     void clearAll();
  89. };

  90. #endif // CUSTOMGRAPHICS_H
复制代码
核心代码
  1. void CustomGraphics::mousePressEvent(QMouseEvent *e)
  2. {
  3.     QPoint p = e->pos();
  4.     pressed = true;
  5.     lastPoint = this->mapToGlobal(p);

  6.     //连线模式下不选中
  7.     if(tempPoints.isEmpty()) {
  8.         //如果选中了,检测是否点到点上
  9.         bool selectedPot = false;
  10.         selectedEllipseIndex = -1;
  11.         if (selectedIndex != -1) {
  12.             for(int i = tempPolygons.at(selectedIndex).pos.size() - 1; i >= 0; --i) {
  13.                 if(length(p, tempPolygons.at(selectedIndex).pos[i]) <= 36) {
  14.                     selectedPot = true;
  15.                     selectedEllipseIndex = i;
  16.                     ellipsePos = tempPolygons.at(selectedIndex).pos[i];
  17.                     break;
  18.                 }
  19.             }
  20.         }

  21.         //当前选中了点则不用重绘
  22.         if(selectedPot) {
  23.             return;
  24.         }

  25.         //判断是否选中一个
  26.         selectedIndex = -1;
  27.         for(int i = tempPolygons.size() - 1; i >= 0; --i) {
  28.             tempPolygons[i].selected = checkPoint(tempPolygons.at(i).pos, p.x(), p.y());
  29.             if(tempPolygons.at(i).selected) {
  30.                 //防止重叠部分
  31.                 if(selectedIndex == -1) {
  32.                     selectedIndex = i;
  33.                     pressedPolygon = tempPolygons.at(i);
  34.                 } else {
  35.                     tempPolygons[i].selected = false;
  36.                 }
  37.             }
  38.         }

  39.         this->update();
  40.     }
  41. }

  42. void CustomGraphics::mouseMoveEvent(QMouseEvent *e)
  43. {
  44.     tempPoint = e->pos();
  45.     if(pressed && selectedIndex != -1) {
  46.         //整体偏移坐标
  47.         QPoint delta = this->mapToGlobal(tempPoint) - lastPoint;
  48.         int len = tempPolygons.at(selectedIndex).pos.size();

  49.         if(selectedEllipseIndex != -1) { //移动点
  50.             tempPolygons[selectedIndex].pos[selectedEllipseIndex] = ellipsePos + delta;
  51.         } else if(selectedIndex != -1) { //移动面
  52.             for(int i = 0; i < len; ++i) {
  53.                 tempPolygons[selectedIndex].pos[i] = pressedPolygon.pos.at(i) + delta;
  54.             }
  55.         }
  56.     }

  57.     this->update();
  58. }

  59. void CustomGraphics::mouseReleaseEvent(QMouseEvent *e)
  60. {
  61.     //鼠标右键清空临时的
  62.     if (e->button() == Qt::RightButton) {
  63.         clearTemp();
  64.         return;
  65.     }

  66.     //检测再次点击与最后个点 - 还没写
  67.     pressed = false;
  68.     if(selectedIndex != -1) {
  69.         return;
  70.     }

  71.     QPoint point = e->pos();
  72.     if(tempPoints.count() > 0) {
  73.         qreal len = (qPow(tempPoints.first().x() - point.x() , 2.0) + qPow(tempPoints.first().y() - point.y() , 2.0) );
  74.         if(len < 100) {
  75.             //完成一个多边形
  76.             if(tempPoints.size() >= 3) {
  77.                 Polygon pol;
  78.                 pol.pos = tempPoints.toVector();
  79.                 pol.selected = false;
  80.                 tempPolygons.append(pol);
  81.             }

  82.             tempPoints.clear();
  83.             this->update();
  84.             return;
  85.         }
  86.     }

  87.     tempPoints.append(point);
  88.     this->update();
  89. }

  90. void CustomGraphics::paintEvent(QPaintEvent *)
  91. {
  92.     QPainter painter(this);
  93.     painter.setRenderHints(QPainter::Antialiasing, true);

  94.     //绘制多边形
  95.     foreach(const Polygon &p, tempPolygons) {
  96.         drawPolygon(&painter, p);
  97.     }

  98.     //绘制点集合
  99.     drawLines(&painter, tempPoints, false);
  100. }

  101. void CustomGraphics::drawPolygon(QPainter *p, const Polygon &v)
  102. {
  103.     p->save();

  104.     //绘制多边形
  105.     p->setPen(QPen(lineColor, lineWidth));
  106.     v.selected ? p->setBrush(selectColor) : p->setBrush(polygonColor);
  107.     p->drawPolygon(v.pos.data(), v.pos.size());

  108.     //绘制圆点
  109.     if(selectDotVisible && v.selected) {
  110.         p->setPen(Qt::NoPen);
  111.         p->setBrush(dotColor);
  112.         foreach(const QPoint &point, v.pos) {
  113.             p->drawEllipse(point, dotRadius, dotRadius);
  114.         }
  115.     }

  116.     p->restore();
  117. }

  118. void CustomGraphics::drawLines(QPainter *p, const QList<QPoint> &list, bool isFirst)
  119. {
  120.     p->save();

  121.     int count = list.count();
  122.     if (count > 0) {
  123.         //绘制点集合
  124.         p->setPen(Qt::NoPen);
  125.         p->setBrush(dotColor);
  126.         for(int i = 0; i < count; ++i) {
  127.             p->drawEllipse(list.at(i), dotRadius, dotRadius);
  128.         }

  129.         //绘制线条集合
  130.         p->setPen(QPen(lineColor, lineWidth));
  131.         p->setBrush(Qt::NoBrush);
  132.         for(int i = 0; i < count - 1; ++i) {
  133.             p->drawLine(list.at(i), list.at(i + 1));
  134.         }

  135.         //绘制最后一条线条
  136.         p->drawLine(list.last(), isFirst ? list.first() : tempPoint);
  137.     }

  138.     p->restore();
  139. }

  140. double CustomGraphics::length(const QPoint &p1, const QPoint &p2)
  141. {
  142.     //平方和
  143.     return qPow(p1.x() - p2.x(), 2.0) + qPow(p1.y() - p2.y(), 2.0);
  144. }

  145. bool CustomGraphics::checkPoint(const QVector<QPoint> &points, int testx, int testy)
  146. {
  147.     //最少保证3个点
  148.     const int count = points.size();
  149.     if(count < 3) {
  150.         return false;
  151.     }

  152.     QList<int> vertx, verty;
  153.     for(int i = 0; i < count; ++i) {
  154.         vertx << points.at(i).x();
  155.         verty << points.at(i).y();
  156.     }

  157.     //核心算法,计算坐标是否在多边形内部
  158.     int i = 0, j, c = 0;
  159.     for (i = 0, j = count - 1; i < count; j = i++) {
  160.         bool b1 = (verty.at(i) > testy) != (verty.at(j) > testy);
  161.         bool b2 = (testx < (vertx.at(j) - vertx.at(i)) * (testy - verty.at(i)) / (verty.at(j) - verty.at(i)) + vertx.at(i));
  162.         if (b1 && b2) {
  163.             c = !c;
  164.         }
  165.     }

  166.     return c;
  167. }
复制代码



1. 超过140个精美控件,涵盖了各种仪表盘、进度条、进度球、指南针、曲线图、标尺、温度计、导航条、导航栏,flatui、高亮按钮、滑动选择器、农历等。远超qwt集成的控件数量。
2. 每个类都可以独立成一个单独的控件,零耦合,每个控件一个头文件和一个实现文件,不依赖其他文件,方便单个控件以源码形式集成到项目中,较少代码量。qwt的控件类环环相扣,高度耦合,想要使用其中一个控件,必须包含所有的代码。
3. 全部纯Qt编写,QWidget+QPainter绘制,支持Qt4.6到Qt5.12的任何Qt版本,支持mingw、msvc、gcc等编译器,不乱码,可直接集成到Qt  Creator中,和自带的控件一样使用,大部分效果只要设置几个属性即可,极为方便。
4. 每个控件都有一个对应的单独的包含该控件源码的DEMO,方便参考使用。同时还提供一个所有控件使用的集成的DEMO。
5. 每个控件的源代码都有详细中文注释,都按照统一设计规范编写,方便学习自定义控件的编写。
6. 每个控件默认配色和demo对应的配色都非常精美。
7. 超过120个可见控件,6个不可见控件。
8. 部分控件提供多种样式风格选择,多种指示器样式选择。
9. 所有控件自适应窗体拉伸变化。
10.  集成自定义控件属性设计器,支持拖曳设计,所见即所得,支持导入导出xml格式。
11. 自带activex控件demo,所有控件可以直接运行在ie浏览器中。
12. 集成fontawesome图形字体+阿里巴巴iconfont收藏的几百个图形字体,享受图形字体带来的乐趣。
13. 所有控件最后生成一个dll动态库文件,可以直接集成到qtcreator中拖曳设计使用。

SDK下载
- SDK下载链接:https://pan.baidu.com/s/1tD9v1YPfE2fgYoK6lqUr1Q  提取码:lyhk
- 自定义控件+属性设计器欣赏:https://pan.baidu.com/s/1l6L3rKSiLu_uYi7lnL3ibQ  提取码:tmvl
- 下载链接中包含了各个版本的动态库文件,所有控件的头文件,使用demo。
- 自定义控件插件开放动态库dll使用(永久免费),无任何后门和限制,请放心使用。
- 不定期增加控件和完善控件,不定期更新SDK,欢迎各位提出建议,谢谢!
- widget版本(QQ:517216493)qml版本(QQ:373955953)三峰驼(QQ:278969898)。






本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了