• 1795阅读
  • 0回复

Qt编写自定义控件28-颜色滑块面板 [复制链接]

上一主题 下一主题
离线liudianwu
 

只看楼主 倒序阅读 楼主  发表于: 2019-07-14

一、前言
相比于上一个颜色按钮面板,此控件就要难很多,颜色值有三种表示形式,除了程序员最常用的RGB以外,还有HSB和CMY方式。
RGB色彩模式是工业界的一种颜色标准,是通过对红(R)、绿(G)、蓝(B)三个颜色通道的变化以及它们相互之间的叠加来得到各式各样的颜色的,RGB即是代表红、绿、蓝三个通道的颜色,这个标准几乎包括了人类视力所能感知的所有颜色,是目前运用最广的颜色系统之一。也是程序员最喜欢最常用的颜色表示方法。
HSB又称HSV,表示一种颜色模式:在HSB模式中,H(hues)表示色相,S(saturation)表示饱和度,B(brightness)表示亮度HSB模式对应的媒介是人眼。HSB模式中S和B呈现的数值越高,饱和度明度越高,页面色彩强烈艳丽,对视觉刺激是迅速的,醒目的效果,但不益于长时间的观看。
CMY是青(Cyan)、洋红或品红(Magenta)和黄(Yellow)三种颜色的简写,是相减混色模式,用这种方法产生的颜色之所以称为相减色,乃是因为它减少了为视觉系统识别颜色所需要的反射光。
由于本控件用于灯光舞台效果的控制控件,可能用户不一定相关使用RGB颜色,也可能用到HSB或者CMY,所以在提供颜色选择的时候,三种都要提供,一种处于选中调节模式的情况下,另外两种要跟随变化,这个是难点,要不断计算当前的颜色值换算成其他颜色值,一开始采用了各种公式换算,后面发现原来QColor内部就封装了,我擦,比如QColor::fromHsv,QColor::fromRgb等,真的是非常强大,可以不用管具体的换算细节。

二、实现的功能
* 1:可设置滑块条之间的间隔
* 2:可设置滑块组之间的间隔
* 3:可设置背景颜色

三、效果图



四、头文件代码
  1. #ifndef COLORPANELFADER_H
  2. #define COLORPANELFADER_H
  3. /**
  4. * 颜色滑块面板 作者:feiyangqingyun(QQ:517216493) 2017-11-17
  5. * 1:可设置滑块条之间的间隔
  6. * 2:可设置滑块组之间的间隔
  7. * 3:可设置背景颜色
  8. */
  9. #include <QWidget>
  10. class QHBoxLayout;
  11. class QSpacerItem;
  12. class ColorPanelBar;
  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 ColorPanelFader : public QWidget
  20. #else
  21. class ColorPanelFader : public QWidget
  22. #endif
  23. {
  24.     Q_OBJECT
  25.     Q_PROPERTY(int barSpace READ getBarSpace WRITE setBarSpace)
  26.     Q_PROPERTY(int groupSpace READ getGroupSpace WRITE setGroupSpace)
  27.     Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor)
  28. public:
  29.     explicit ColorPanelFader(QWidget *parent = 0);
  30. protected:
  31.     bool eventFilter(QObject *watched, QEvent *event);
  32.     void paintEvent(QPaintEvent *);
  33. private:
  34.     QHBoxLayout *layout;
  35.     QSpacerItem *spacer1;
  36.     QSpacerItem *spacer2;
  37.     QList<ColorPanelBar *> items;
  38.     int barSpace;               //柱状条间隔
  39.     int groupSpace;             //分组间隔
  40.     QColor bgColor;             //背景颜色
  41. private slots:
  42.     void colorChanged(const QColor &color, double value, double percent);
  43. public:
  44.     int getBarSpace()           const;
  45.     int getGroupSpace()         const;
  46.     QColor getBgColor()         const;
  47.     QSize sizeHint()            const;
  48.     QSize minimumSizeHint()     const;
  49. public:
  50.     //设置柱状条间隔
  51.     void setBarSpace(int barSpace);
  52.     //设置分组间隔
  53.     void setGroupSpace(int groupSpace);
  54.     //设置背景颜色
  55.     void setBgColor(const QColor &bgColor);
  56. Q_SIGNALS:
  57.     void colorChanged(const QColor &color, double hue, double sat, double bright);
  58. };
  59. #endif // COLORPANELFADER_H

五、核心代码
  1. bool ColorPanelFader::eventFilter(QObject *watched, QEvent *event)
  2. {
  3.     if (event->type() == QEvent::MouseButtonPress) {
  4.         ColorPanelBar *item = (ColorPanelBar *)watched;
  5.         int index = items.indexOf(item);
  6.         if (index >= 6) {
  7.             items.at(0)->setEnabled(false);
  8.             items.at(1)->setEnabled(false);
  9.             items.at(2)->setEnabled(false);
  10.             items.at(3)->setEnabled(false);
  11.             items.at(4)->setEnabled(false);
  12.             items.at(5)->setEnabled(false);
  13.         } else if (index >= 3) {
  14.             items.at(0)->setEnabled(false);
  15.             items.at(1)->setEnabled(false);
  16.             items.at(2)->setEnabled(false);
  17.             items.at(6)->setEnabled(false);
  18.             items.at(7)->setEnabled(false);
  19.             items.at(8)->setEnabled(false);
  20.         } else if (index >= 0) {
  21.             items.at(3)->setEnabled(false);
  22.             items.at(4)->setEnabled(false);
  23.             items.at(5)->setEnabled(false);
  24.             items.at(6)->setEnabled(false);
  25.             items.at(7)->setEnabled(false);
  26.             items.at(8)->setEnabled(false);
  27.         }
  28.     } else if (event->type() == QEvent::MouseButtonRelease) {
  29.         ColorPanelBar *item = (ColorPanelBar *)watched;
  30.         int index = items.indexOf(item);
  31.         if (index >= 6) {
  32.             items.at(0)->setEnabled(true);
  33.             items.at(1)->setEnabled(true);
  34.             items.at(2)->setEnabled(true);
  35.             items.at(3)->setEnabled(true);
  36.             items.at(4)->setEnabled(true);
  37.             items.at(5)->setEnabled(true);
  38.         } else if (index >= 3) {
  39.             items.at(0)->setEnabled(true);
  40.             items.at(1)->setEnabled(true);
  41.             items.at(2)->setEnabled(true);
  42.             items.at(6)->setEnabled(true);
  43.             items.at(7)->setEnabled(true);
  44.             items.at(8)->setEnabled(true);
  45.         } else if (index >= 0) {
  46.             items.at(3)->setEnabled(true);
  47.             items.at(4)->setEnabled(true);
  48.             items.at(5)->setEnabled(true);
  49.             items.at(6)->setEnabled(true);
  50.             items.at(7)->setEnabled(true);
  51.             items.at(8)->setEnabled(true);
  52.         }
  53.     }
  54.     return QWidget::eventFilter(watched, event);
  55. }
  56. void ColorPanelFader::paintEvent(QPaintEvent *)
  57. {
  58.     QPainter painter(this);
  59.     painter.fillRect(rect(), bgColor);
  60. }
  61. void ColorPanelFader::colorChanged(const QColor &color, double value, double percent)
  62. {
  63.     ColorPanelBar *item = (ColorPanelBar *)sender();
  64.     int index = items.indexOf(item);
  65.     if (index == 0) {
  66.         //获取当前HSB处的颜色值
  67.         items.at(1)->setTopColor(color);
  68.         items.at(2)->setTopColor(color);
  69.         items.at(1)->setBorderColor(color);
  70.         items.at(2)->setBorderColor(color);
  71.     } else if (index == 1) {
  72.         items.at(2)->setTopColor(color);
  73.         items.at(2)->setBorderColor(color);
  74.     } else if (index == 2) {
  75.         items.at(1)->setTopColor(color);
  76.         items.at(1)->setBorderColor(color);
  77.     } else if (index == 3) {
  78.         items.at(6)->setPercent(100 - percent);
  79.     } else if (index == 4) {
  80.         items.at(7)->setPercent(100 - percent);
  81.     } else if (index == 5) {
  82.         items.at(8)->setPercent(100 - percent);
  83.     } else if (index == 6) {
  84.         items.at(3)->setPercent(100 - percent);
  85.     } else if (index == 7) {
  86.         items.at(4)->setPercent(100 - percent);
  87.     } else if (index == 8) {
  88.         items.at(5)->setPercent(100 - percent);
  89.     }
  90.     //如果是HSB变化则CMY和RGB变化
  91.     if (index < 3) {
  92.         double hue = items.at(0)->getPercent() / 100;
  93.         double sat = items.at(1)->getPercent() / 100;
  94.         double bright = items.at(2)->getPercent() / 100;
  95.         //组合HSB当前值,然后转为CMY和RGB计算百分比进行设置
  96.         QColor color = QColor::fromHsvF(hue, sat, bright);
  97.         double percentRed = color.redF() * 100;
  98.         double percentGreen = color.greenF() * 100;
  99.         double percentBlue = color.blueF() * 100;
  100.         items.at(3)->setPercent(100 - percentRed);
  101.         items.at(4)->setPercent(100 - percentGreen);
  102.         items.at(5)->setPercent(100 - percentBlue);
  103.         items.at(6)->setPercent(percentRed);
  104.         items.at(7)->setPercent(percentGreen);
  105.         items.at(8)->setPercent(percentBlue);
  106.     }
  107.     //根据百分比获取颜色值
  108.     double red = items.at(6)->getPercent() / 100;
  109.     double green = items.at(7)->getPercent() / 100;
  110.     double blue = items.at(8)->getPercent() / 100;
  111.     QColor currentColor = QColor::fromRgbF(red, green, blue);
  112.     emit colorChanged(currentColor, items.at(0)->getValue(), items.at(1)->getPercent(), items.at(2)->getPercent());
  113.     //如果是CMY或者RGB变化则HSB变化
  114.     if (index >= 3) {
  115.         //hue活出现负数=白色,要矫正
  116.         double percentHue = currentColor.hueF() * 100;
  117.         if (percentHue < 0) {
  118.             percentHue = 0;
  119.         }
  120.         double percentSat = currentColor.saturationF() * 100;
  121.         double percentBright = currentColor.lightnessF() * 100;
  122.         //计算当前值所占百分比
  123.         items.at(0)->setPercent(percentHue);
  124.         items.at(1)->setPercent(percentSat);
  125.         items.at(2)->setPercent(percentBright);
  126.         items.at(1)->setTopColor(currentColor);
  127.         items.at(2)->setTopColor(currentColor);
  128.         items.at(1)->setBorderColor(currentColor);
  129.         items.at(2)->setBorderColor(currentColor);
  130.     }
  131. }

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

七、SDK下载
- SDK下载链接:https://pan.baidu.com/s/1A5Gd77kExm8Co5ckT51vvQ 提取码:877p
- 下载链接中包含了各个版本的动态库文件,所有控件的头文件,使用demo,自定义控件+属性设计器。
- 自定义控件插件开放动态库dll使用(永久免费),无任何后门和限制,请放心使用。
- 目前已提供26个版本的dll,其中包括了qt5.12.3 msvc2017 32+64 mingw 32+64 的。
- 不定期增加控件和完善控件,不定期更新SDK,欢迎各位提出建议,谢谢!
- widget版本(QQ:517216493)qml版本(QQ:373955953)三峰驼(QQ:278969898)。
- 涛哥的知乎专栏 Qt进阶之路 https://zhuanlan.zhihu.com/TaoQt
- 欢迎关注微信公众号【高效程序员】,C++/Python、学习方法、写作技巧、热门技术、职场发展等内容,干货多多,福利多多!
  
欢迎关注微信公众号:Qt实战 (各种开源作品、经验整理、项目实战技巧,专注Qt/C++软件开发,视频监控、物联网、工业控制、嵌入式软件、国产化系统应用软件开发)QQ:517216493  WX:feiyangqingyun  QQ群:751439350
快速回复
限100 字节
 
上一个 下一个