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

Qt编写自定义控件10-云台仪表盘

1
回复
5537
查看
[复制链接]
累计签到:7 天
连续签到:1 天
来源: 2019-4-28 09:46:35 显示全部楼层 |阅读模式
前言
做过安防视频监控的同学都清楚,在视频监控系统软件上都可以看到一个云台控制区域,可以对球机进行下下左右等八个方位的运动控制,还可以进行复位,一般都是美工作图好,然后贴图的形式加入到软件中,好处是程序简单,界面美工,主要取决于美工的美图能力,缺点是对于各种分辨率的适应性稍微差点,需要不同的图片切图贴图,除非默认做好的是大图自适应看不出差别,可能大部分人所在的公司都是小公司,一般美工人员比较少甚至没有,都需要程序员一人负责,甚至一开始就要考虑到各种分辨率的应用场景以及后期可能的换肤换色等。
之前做过很多自定义控件,大部分都采用了qpainter的形式绘制,有个好处就是自适应任意分辨率,所以思考着这个云台控制仪表盘也采用纯painter绘制的形式,据说纯painter绘制还可以轻松移植到qml中,这又坚定了我用qpainter绘制的决心。所谓心中有坐标系,万物皆painter。
观察云台仪表盘下来,基本上就这几部分组成,圆形底盘,八个角,中间部分按钮,整个的控件的难点就在于八个角的定位,中间部分很好定位,而且八个角不是绝对的位置,都是相对于界面的宽高按照等比例自适应排列的。八个角的鼠标按下要做出对应的反应,发送出对应型号,网上大部分人都是切图或者放置label或者按钮来贴图实现,绑定事件过滤器过滤鼠标按下然后再发出信号。我这里为了提升逼格,直接采用位置坐标计算法。

实现的功能
* 1:可设置背景颜色
* 2:可设置基准颜色
* 3:可设置边框颜色
* 4:可设置文本颜色
* 5:可识别每个角度+中间 鼠标按下并发出信号
* 6:可设置八个角的图标和中间图标,随便换
* 7:内置4种云台风格 黑色+白色+蓝色+紫色
* 8:支持拓展鼠标进入离开时的切换
* 9:精准识别内圆区域鼠标按下,而不是圆的矩形区域
* 10:支持长按连续触发,支持设定延时间隔和执行间隔

效果图


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

  3. /**
  4. * 云台仪表盘控件 作者:feiyangqingyun(QQ:517216493) 2018-9-2
  5. * 1:可设置背景颜色
  6. * 2:可设置基准颜色
  7. * 3:可设置边框颜色
  8. * 4:可设置文本颜色
  9. * 5:可识别每个角度+中间 鼠标按下并发出信号
  10. * 6:可设置八个角的图标和中间图标,随便换
  11. * 7:内置4种云台风格 黑色+白色+蓝色+紫色
  12. * 8:支持拓展鼠标进入离开时的切换
  13. * 9:精准识别内圆区域鼠标按下,而不是圆的矩形区域
  14. * 10:支持长按连续触发,支持设定延时间隔和执行间隔
  15. */

  16. #include <QWidget>

  17. #ifdef quc
  18. #if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
  19. #include <QtDesigner/QDesignerExportWidget>
  20. #else
  21. #include <QtUiPlugin/QDesignerExportWidget>
  22. #endif

  23. class QDESIGNER_WIDGET_EXPORT GaugeCloud : public QWidget
  24. #else
  25. class GaugeCloud : public QWidget
  26. #endif

  27. {
  28.     Q_OBJECT
  29.     Q_ENUMS(CloudStyle)

  30.     Q_PROPERTY(QColor baseColor READ getBaseColor WRITE setBaseColor)
  31.     Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor)
  32.     Q_PROPERTY(QColor arcColor READ getArcColor WRITE setArcColor)
  33.     Q_PROPERTY(QColor borderColor READ getBorderColor WRITE setBorderColor)
  34.     Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor)
  35.     Q_PROPERTY(QColor enterColor READ getEnterColor WRITE setEnterColor)
  36.     Q_PROPERTY(QColor pressColor READ getPressColor WRITE setPressColor)

  37.     Q_PROPERTY(QString iconText READ getIconText WRITE setIconText)
  38.     Q_PROPERTY(QString centerText READ getCenterText WRITE setCenterText)
  39.     Q_PROPERTY(CloudStyle cloudStyle READ getCloudStyle WRITE setCloudStyle)

  40.     Q_PROPERTY(bool autoRepeat READ getAutoRepeat WRITE setAutoRepeat)
  41.     Q_PROPERTY(int autoRepeatDelay READ getAutoRepeatDelay WRITE setAutoRepeatDelay)
  42.     Q_PROPERTY(int autoRepeatInterval READ getAutoRepeatInterval WRITE setAutoRepeatInterval)

  43. public:
  44.     enum CloudStyle {
  45.         CloudStyle_Black = 0,   //黑色风格
  46.         CloudStyle_White = 1,   //白色风格
  47.         CloudStyle_Blue = 2,    //蓝色风格
  48.         CloudStyle_Purple = 3   //紫色风格
  49.     };

  50.     explicit GaugeCloud(QWidget *parent = 0);
  51.     ~GaugeCloud();

  52. protected:
  53.     void enterEvent(QEvent *);
  54.     void leaveEvent(QEvent *);
  55.     void mousePressEvent(QMouseEvent *);
  56.     void mouseReleaseEvent(QMouseEvent *);
  57.     void mouseMoveEvent(QMouseEvent *);
  58.     void paintEvent(QPaintEvent *);
  59.     void drawCircle(QPainter *painter, int radius, const QBrush &brush);
  60.     void drawArc(QPainter *painter);
  61.     void drawText(QPainter *painter);

  62. private:
  63.     QColor bgColor;                 //背景颜色
  64.     QColor baseColor;               //基准颜色
  65.     QColor arcColor;                //圆弧颜色
  66.     QColor borderColor;             //边框颜色
  67.     QColor textColor;               //文字颜色
  68.     QColor enterColor;              //悬停文字颜色
  69.     QColor pressColor;              //按下文字颜色

  70.     QString iconText;               //八个角图标
  71.     QString centerText;             //中间图标
  72.     CloudStyle cloudStyle;          //云台样式

  73.     bool autoRepeat;                //是否重复执行按下
  74.     int autoRepeatDelay;            //延时执行按下动作
  75.     int autoRepeatInterval;         //重复执行间隔

  76.     bool enter;                     //鼠标是否进入
  77.     bool pressed;                   //鼠标是否按下
  78.     bool inCenter;                  //是否在内圆中
  79.     QPoint lastPoint;               //鼠标按下处的坐标
  80.     QRectF centerRect;              //中间区域
  81.     QRectF leftRect;                //左侧图标区域
  82.     QRectF topRect;                 //上侧图标区域
  83.     QRectF rightRect;               //右侧图标区域
  84.     QRectF bottomRect;              //下侧图标区域
  85.     QRectF leftTopRect;             //左上角图标区域
  86.     QRectF rightTopRect;            //右上角图标区域
  87.     QRectF leftBottomRect;          //左下角图标区域
  88.     QRectF rightBottomRect;         //右下角图标区域

  89.     QFont iconFont;                 //图形字体
  90.     int position;                   //最后按下的位置
  91.     QTimer *timer;                  //定时器触发长按

  92. private slots:
  93.     void checkPressed();

  94. private:
  95.     double twoPtDistance(const QPointF &pt1, const QPointF &pt2);

  96. public:
  97.     QColor getBgColor()             const;
  98.     QColor getBaseColor()           const;
  99.     QColor getArcColor()            const;
  100.     QColor getBorderColor()         const;
  101.     QColor getTextColor()           const;
  102.     QColor getEnterColor()          const;
  103.     QColor getPressColor()          const;

  104.     QString getIconText()           const;
  105.     QString getCenterText()         const;
  106.     CloudStyle getCloudStyle()      const;

  107.     bool getAutoRepeat()            const;
  108.     int getAutoRepeatDelay()        const;
  109.     int getAutoRepeatInterval()     const;

  110.     QSize sizeHint()                const;
  111.     QSize minimumSizeHint()         const;

  112. public Q_SLOTS:
  113.     //设置背景颜色
  114.     void setBgColor(const QColor &bgColor);
  115.     //设置基准颜色
  116.     void setBaseColor(const QColor &baseColor);
  117.     //设置圆弧颜色
  118.     void setArcColor(const QColor &arcColor);
  119.     //设置边框颜色
  120.     void setBorderColor(const QColor &borderColor);
  121.     //设置文本颜色
  122.     void setTextColor(const QColor &textColor);
  123.     //设置悬停文本颜色
  124.     void setEnterColor(const QColor &enterColor);
  125.     //设置按下文本颜色
  126.     void setPressColor(const QColor &pressColor);

  127.     //设置八个角图标
  128.     void setIconText(const QString &iconText);
  129.     //设置中间图标
  130.     void setCenterText(const QString ¢erText);
  131.     //设置云台样式
  132.     void setCloudStyle(const CloudStyle &cloudStyle);

  133.     //设置是否启用长按重复执行
  134.     void setAutoRepeat(bool autoRepeat);
  135.     //设置延时执行长按时间
  136.     void setAutoRepeatDelay(int autoRepeatDelay);
  137.     //设置长按重复执行间隔
  138.     void setAutoRepeatInterval(int autoRepeatInterval);

  139. Q_SIGNALS:
  140.     //鼠标按下的区域,共9个,从0-8依次表示底部/左下角/左侧/左上角/顶部/右上角/右侧/右下角/中间
  141.     void mousePressed(int position);
  142.     //鼠标松开
  143.     void mouseReleased(int position);
  144. };

  145. #endif //GAUGECLOUD_H
复制代码
核心代码
  1. void GaugeCloud::paintEvent(QPaintEvent *)
  2. {
  3.     int width = this->width();
  4.     int height = this->height();
  5.     int side = qMin(width, height);

  6.     //以中心点为基准,分别计算八方位区域和中间区域
  7.     QPointF center = this->rect().center();
  8.     double centerSize = (double)side / ((double)100 / 30);
  9.     double iconSize = (double)side / ((double)100 / 10);
  10.     double offset1 = 3.6;
  11.     double offset2 = 2.65;

  12.     //计算当前按下点到中心点的距离,如果小于内圆的半径则认为在内圆中
  13.     double offset = twoPtDistance(lastPoint, this->rect().center());
  14.     inCenter = (offset <= centerSize / 2);

  15.     //中间区域
  16.     centerRect = QRectF(center.x() - centerSize / 2, center.y() - centerSize / 2, centerSize, centerSize);
  17.     //左侧图标区域
  18.     leftRect = QRectF(center.x() - iconSize * offset1, center.y() - iconSize / 2, iconSize, iconSize);
  19.     //上侧图标区域
  20.     topRect = QRectF(center.x() - iconSize / 2, center.y() - iconSize * offset1, iconSize, iconSize);
  21.     //右侧图标区域
  22.     rightRect = QRectF(center.x() + iconSize * (offset1 - 1), center.y() - iconSize / 2, iconSize, iconSize);
  23.     //下侧图标区域
  24.     bottomRect = QRectF(center.x() - iconSize / 2, center.y() + iconSize * (offset1 - 1), iconSize, iconSize);
  25.     //左上角图标区域
  26.     leftTopRect = QRectF(center.x() - iconSize * offset2, center.y() - iconSize * offset2, iconSize, iconSize);
  27.     //右上角图标区域
  28.     rightTopRect = QRectF(center.x() + iconSize * (offset2 - 1), center.y() - iconSize * offset2, iconSize, iconSize);
  29.     //左下角图标区域
  30.     leftBottomRect = QRectF(center.x() - iconSize * offset2, center.y() + iconSize * (offset2 - 1), iconSize, iconSize);
  31.     //右下角图标区域
  32.     rightBottomRect = QRectF(center.x() + iconSize * (offset2 - 1), center.y() + iconSize * (offset2 - 1), iconSize, iconSize);

  33.     //绘制准备工作,启用反锯齿,平移坐标轴中心,等比例缩放
  34.     QPainter painter(this);
  35.     painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
  36.     painter.translate(width / 2, height / 2);
  37.     painter.scale(side / 200.0, side / 200.0);

  38.     if (cloudStyle == CloudStyle_Black) {
  39.         //绘制外圆背景
  40.         drawCircle(&painter, 99, bgColor);
  41.         //绘制圆弧
  42.         drawArc(&painter);
  43.         //绘制中间圆盘背景
  44.         drawCircle(&painter, 83, baseColor);
  45.         //绘制内圆背景
  46.         drawCircle(&painter, 40, arcColor);
  47.         //绘制内圆边框
  48.         drawCircle(&painter, 33, borderColor);
  49.         //绘制内圆
  50.         drawCircle(&painter, 30, inCenter ? bgColor : baseColor);
  51.     } else if (cloudStyle == CloudStyle_White) {
  52.         //绘制外圆背景
  53.         drawCircle(&painter, 99, QColor(249, 249, 249));

  54.         //设置圆锥渐变
  55.         QConicalGradient gradient(0, 0, 100);
  56.         gradient.setColorAt(0, QColor(34, 163, 169));
  57.         gradient.setColorAt(0.4, QColor(240, 201, 136));
  58.         gradient.setColorAt(0.7, QColor(211, 77, 37));
  59.         gradient.setColorAt(1, QColor(34, 163, 169));

  60.         //绘制彩色外圆
  61.         drawCircle(&painter, 90, gradient);
  62.         //绘制中间圆盘背景
  63.         drawCircle(&painter, 83, QColor(245, 245, 245));
  64.         //绘制内圆背景
  65.         drawCircle(&painter, 33, QColor(208, 208, 208));
  66.         //绘制内圆边框
  67.         drawCircle(&painter, 32, QColor(208, 208, 208));
  68.         //绘制内圆
  69.         drawCircle(&painter, 30, inCenter ? QColor(255, 255, 255) : QColor(245, 245, 245));
  70.     } else if (cloudStyle == CloudStyle_Blue) {
  71.         //设置圆锥渐变
  72.         QConicalGradient gradient(0, 0, 100);
  73.         gradient.setColorAt(0, QColor(79, 163, 219));
  74.         gradient.setColorAt(0.4, QColor(227, 183, 106));
  75.         gradient.setColorAt(0.7, QColor(217, 178, 109));
  76.         gradient.setColorAt(1, QColor(79, 163, 219));

  77.         //绘制色彩外圆
  78.         drawCircle(&painter, 99, gradient);
  79.         //绘制中间圆盘背景
  80.         drawCircle(&painter, 91, QColor(31, 66, 98));
  81.         //绘制内圆背景
  82.         drawCircle(&painter, 33, QColor(23, 54, 81));
  83.         //绘制内圆边框
  84.         drawCircle(&painter, 30, QColor(150, 150, 150));
  85.         //绘制内圆
  86.         drawCircle(&painter, 30, inCenter ? QColor(35, 82, 133) : QColor(34, 73, 115));
  87.     } else if (cloudStyle == CloudStyle_Purple) {
  88.         //设置圆锥渐变
  89.         QConicalGradient gradient(0, 0, 100);
  90.         gradient.setColorAt(0, QColor(87, 87, 155));
  91.         gradient.setColorAt(0.4, QColor(129, 82, 130));
  92.         gradient.setColorAt(0.7, QColor(54, 89, 166));
  93.         gradient.setColorAt(1, QColor(87, 87, 155));

  94.         //绘制色彩外圆
  95.         drawCircle(&painter, 99, gradient);
  96.         //绘制中间圆盘背景
  97.         drawCircle(&painter, 91, QColor(55, 55, 92));
  98.         //绘制内圆背景
  99.         drawCircle(&painter, 33, QColor(49, 48, 82));
  100.         //绘制内圆边框
  101.         drawCircle(&painter, 30, QColor(82, 78, 131));
  102.         //绘制内圆
  103.         drawCircle(&painter, 30, inCenter ? QColor(85, 81, 137) : QColor(62, 59, 103));
  104.     }

  105.     //绘制八方位+中间图标
  106.     drawText(&painter);

  107. #if 0
  108.     //重置坐标系,并绘制八方位区域及中间区域,判断是否正确
  109.     painter.resetMatrix();
  110.     painter.resetTransform();
  111.     painter.setPen(Qt::white);
  112.     painter.drawRect(centerRect);
  113.     painter.drawRect(leftRect);
  114.     painter.drawRect(topRect);
  115.     painter.drawRect(rightRect);
  116.     painter.drawRect(bottomRect);
  117.     painter.drawRect(leftTopRect);
  118.     painter.drawRect(rightTopRect);
  119.     painter.drawRect(leftBottomRect);
  120.     painter.drawRect(rightBottomRect);
  121. #endif
  122. }

  123. void GaugeCloud::drawCircle(QPainter *painter, int radius, const QBrush &brush)
  124. {
  125.     painter->save();
  126.     painter->setPen(Qt::NoPen);
  127.     painter->setBrush(brush);

  128.     //绘制圆
  129.     painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
  130.     painter->restore();
  131. }

  132. void GaugeCloud::drawArc(QPainter *painter)
  133. {
  134.     int radius = 91;
  135.     painter->save();
  136.     painter->setBrush(Qt::NoBrush);

  137.     QPen pen;
  138.     pen.setWidthF(10);
  139.     pen.setColor(arcColor);
  140.     painter->setPen(pen);

  141.     QRectF rect = QRectF(-radius, -radius, radius * 2, radius * 2);
  142.     painter->drawArc(rect, 0 * 16, 360 * 16);

  143.     painter->restore();
  144. }

  145. void GaugeCloud::drawText(QPainter *painter)
  146. {
  147.     bool ok;
  148.     int radius = 100;
  149.     painter->save();

  150.     //判断当前按下坐标是否在中心区域,按下则文本不同颜色
  151.     if (inCenter) {
  152.         if (pressed) {
  153.             position = 8;
  154.             if (autoRepeat) {
  155.                 QTimer::singleShot(autoRepeatDelay, timer, SLOT(start()));
  156.             }

  157.             emit mousePressed(position);
  158.         }

  159.         painter->setPen(pressed ? pressColor : enterColor);
  160.     } else {
  161.         painter->setPen(textColor);
  162.     }

  163.     QFont font;
  164.     font.setPixelSize(30);
  165. #if (QT_VERSION >= QT_VERSION_CHECK(4,8,0))
  166.     font.setHintingPreference(QFont::PreferNoHinting);
  167. #endif
  168.     painter->setFont(font);

  169.     //绘制中间图标
  170.     QRectF centerRect(-radius, -radius, radius * 2, radius * 2);
  171.     QString centerText = this->centerText.replace("0x", "");
  172.     QChar centerChar = QChar(centerText.toInt(&ok, 16));
  173.     painter->drawText(centerRect, Qt::AlignCenter, centerChar);

  174.     //绘制八方位图标
  175.     radius = 70;
  176.     int offset = 15;
  177.     int steps = 8;
  178.     double angleStep = 360.0 / steps;

  179.     font.setPixelSize(28);
  180.     painter->setFont(font);

  181.     //从下侧图标开始绘制,顺时针旋转
  182.     QRect iconRect(-offset / 2, radius - offset, offset, offset);
  183.     QString iconText = this->iconText.replace("0x", "");
  184.     QChar iconChar = QChar(iconText.toInt(&ok, 16));
  185.     for (int i = 0; i < steps; i++) {
  186.         //判断鼠标按下的是哪个区域
  187.         bool contains = false;
  188.         if (bottomRect.contains(lastPoint) && i == 0) {
  189.             contains = true;
  190.         } else if (leftBottomRect.contains(lastPoint) && i == 1) {
  191.             contains = true;
  192.         } else if (leftRect.contains(lastPoint) && i == 2) {
  193.             contains = true;
  194.         } else if (leftTopRect.contains(lastPoint) && i == 3) {
  195.             contains = true;
  196.         } else if (topRect.contains(lastPoint) && i == 4) {
  197.             contains = true;
  198.         } else if (rightTopRect.contains(lastPoint) && i == 5) {
  199.             contains = true;
  200.         } else if (rightRect.contains(lastPoint) && i == 6) {
  201.             contains = true;
  202.         } else if (rightBottomRect.contains(lastPoint) && i == 7) {
  203.             contains = true;
  204.         }

  205.         if (contains) {
  206.             if (pressed) {
  207.                 position = i;
  208.                 if (autoRepeat) {
  209.                     QTimer::singleShot(autoRepeatDelay, timer, SLOT(start()));
  210.                 }

  211.                 emit mousePressed(position);
  212.             }

  213.             painter->setPen(pressed ? pressColor : enterColor);
  214.         } else {
  215.             painter->setPen(textColor);
  216.         }

  217.         painter->drawText(iconRect, Qt::AlignCenter, iconChar);
  218.         painter->rotate(angleStep);
  219.     }

  220.     painter->restore();
  221. }
复制代码
控件介绍

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使用(永久免费),无任何后门和限制,请放心使用。
- 目前已提供22个版本的dll,其中包括了qt5.12.3 msvc2017 32+64 mingw 32+64 的。
- 不定期增加控件和完善控件,不定期更新SDK,欢迎各位提出建议,谢谢!
- widget版本(QQ:517216493)qml版本(QQ:373955953)三峰驼(QQ:278969898)。



本帖子中包含更多资源

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

x
回复

使用道具 举报

累计签到:13 天
连续签到:1 天
2019-4-29 10:33:00 显示全部楼层
这些控件真的很漂亮啊
回复 支持 反对

使用道具 举报

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

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