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

Qt编写自定义控件27-颜色按钮面板

0
回复
5644
查看
[复制链接]
累计签到:7 天
连续签到:1 天
来源: 2019-7-14 17:37:54 显示全部楼层 |阅读模式
一、前言
颜色按钮面板主要用在提供一个颜色按钮面板,用户单击某个按钮,然后拿到对应的颜色值,用户可以预先设定常用的颜色集合,传入到控件中,自动生成面板颜色集合按钮,每当滑过按钮的时候,按钮边缘高亮提示当前所在颜色的按钮,当选中某个按钮时,右侧颜色条显示当前选中的颜色,此控件功能极其简单,直接采用动态生成按钮的方式,设置按钮的样式表来设置对应的颜色和高亮边框等,单击按钮发出颜色改变信号即可,对外提供该信号就行,非常适合初学者学习。

二、实现的功能
* 1:可设置颜色集合
* 2:可设置按钮圆角角度
* 3:可设置列数
* 4:可设置按钮边框宽度和边框颜色

三、效果图


四、头文件代码
  1. #ifndef COLORPANELBTN_H
  2. #define COLORPANELBTN_H

  3. /**
  4. * 颜色按钮面板 作者:feiyangqingyun(QQ:517216493) 2017-11-17
  5. * 1:可设置颜色集合
  6. * 2:可设置按钮圆角角度
  7. * 3:可设置列数
  8. * 4:可设置按钮边框宽度和边框颜色
  9. */

  10. #include <QWidget>

  11. class QGridLayout;
  12. class QPushButton;

  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 ColorPanelBtn : public QWidget
  20. #else
  21. class ColorPanelBtn : public QWidget
  22. #endif

  23. {
  24.     Q_OBJECT
  25.     Q_PROPERTY(int space READ getSpace WRITE setSpace)
  26.     Q_PROPERTY(int columnCount READ getColumnCount WRITE setColumnCount)
  27.     Q_PROPERTY(int borderRadius READ getBorderRadius WRITE setBorderRadius)
  28.     Q_PROPERTY(int borderWidth READ getBorderWidth WRITE setBorderWidth)
  29.     Q_PROPERTY(QColor borderColor READ getBorderColor WRITE setBorderColor)

  30. public:
  31.     explicit ColorPanelBtn(QWidget *parent = 0);

  32. private:
  33.     QGridLayout *gridLayout;
  34.     QList<QPushButton *> btns;
  35.     QStringList colors;

  36.     int space;                  //按钮之间的间隔
  37.     int columnCount;            //按钮列数
  38.     int borderRadius;           //边框圆角
  39.     int borderWidth;            //边框宽度
  40.     QColor borderColor;         //边框颜色

  41. private slots:
  42.     void initStyle();
  43.     void initBtn();
  44.     void btnClicked();

  45. public:
  46.     QStringList getColors()     const;

  47.     int getSpace()              const;
  48.     int getColumnCount()        const;
  49.     int getBorderRadius()       const;
  50.     int getBorderWidth()        const;
  51.     QColor getBorderColor()     const;

  52.     QSize sizeHint()            const;
  53.     QSize minimumSizeHint()     const;

  54. public Q_SLOTS:
  55.     //设置颜色集合
  56.     void setColors(const QStringList &colors);

  57.     //设置按钮间隔
  58.     void setSpace(int space);

  59.     //设置列数
  60.     void setColumnCount(int columnCount);

  61.     //设置圆角角度
  62.     void setBorderRadius(int borderRadius);

  63.     //设置边框宽度
  64.     void setBorderWidth(int borderWidth);

  65.     //设置边框颜色
  66.     void setBorderColor(const QColor &borderColor);

  67. Q_SIGNALS:
  68.     void colorChanged(const QColor &color);

  69. };

  70. #endif // COLORPANELBTN_H
复制代码


五、完整代码
  1. #pragma execution_character_set("utf-8")

  2. #include "colorpanelbtn.h"
  3. #include "qlayout.h"
  4. #include "qpushbutton.h"
  5. #include "qdebug.h"

  6. ColorPanelBtn::ColorPanelBtn(QWidget *parent) : QWidget(parent)
  7. {
  8.     colors << "#FEFEFE" << "#EEEEEF" << "#DCDDDD" << "#C9CACA" << "#B6B6B7" << "#A1A1A1" << "#8B8B8C" << "#757475" << "#5F5D5D" << "#474443" << "#303030";
  9.     colors << "#00A2E9" << "#009B4C" << "#FFF000" << "#E62129" << "#E40082" << "#B04B87" << "#F08519" << "#F4B3B3" << "#897870" << "#D2CDE6" << "#A79CCB";
  10.     colors << "#758FC8" << "#7C6FB0" << "#9288B1" << "#566892" << "#5E5872" << "#7789A4" << "#008FD7" << "#A0D9F6" << "#B8CEDA" << "#98AAB4" << "#75838A";
  11.     colors << "#50585D" << "#5B7877" << "#4B8D7F" << "#769C9B" << "#5BA997" << "#5FA776" << "#62C3D0" << "#56AAB7" << "#B9CCBC" << "#D5EAD8" << "#A6D4AE";
  12.     colors << "#99A99C" << "#9AA780" << "#BCC774" << "#BBC99A" << "#ACCE22" << "#D9E483" << "#5F5C50" << "#8B8979" << "#B6B49E" << "#B6B281" << "#DED572";
  13.     colors << "#FFF582" << "#FFF9B1" << "#FFFCDB" << "#B39B77" << "#D59961" << "#DAB96B" << "#EF8641" << "#F6AE45" << "#F5B06E" << "#FDD100" << "#FBD7A3";
  14.     colors << "#89765B" << "#AC6249" << "#D0753B" << "#EF8762" << "#F5B193" << "#FADAC9" << "#AF8283" << "#CF7771" << "#FF696B" << "#CF788A" << "#E61D4C";
  15.     colors << "#EF8781" << "#E95A6F" << "#D49D9E" << "#876474" << "#AC6484" << "#F4B5D0" << "#D49EB6" << "#B39FA8" << "#D8C0CB" << "#B3719D" << "#CA5599";
  16.     colors << "#CD81B3" << "#B593B3" << "#D0A9CD" << "#745E73" << "#977B95" << "#A878B1" << "#A72185" << "#934787" << "#804E9A" << "#7B5882" << "#714588";

  17.     space = 2;
  18.     columnCount = 11;
  19.     borderRadius = 0;
  20.     borderWidth = 2;
  21.     borderColor = QColor("#C0392B");

  22.     gridLayout = new QGridLayout;
  23.     gridLayout->setSpacing(space);
  24.     gridLayout->setMargin(0);
  25.     this->setLayout(gridLayout);
  26.     this->initStyle();
  27.     this->initBtn();   
  28. }

  29. void ColorPanelBtn::initStyle()
  30. {
  31.     QString qss = QString("QPushButton{border:none;border-radius:%1px;}"
  32.                           "QPushButton:hover{border:%2px solid %3;}")
  33.                   .arg(borderRadius).arg(borderWidth).arg(borderColor.name());
  34.     this->setStyleSheet(qss);
  35. }

  36. void ColorPanelBtn::initBtn()
  37. {
  38.     qDeleteAll(btns);
  39.     btns.clear();

  40.     int count = colors.count();
  41.     int row = 0;
  42.     int column = 0;
  43.     int index = 0;
  44.     for (int i = 0; i < count; i++) {
  45.         QPushButton *btn = new QPushButton;
  46.         connect(btn, SIGNAL(pressed()), this, SLOT(btnClicked()));
  47.         btn->setObjectName("btn" + colors.at(i));
  48.         btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  49.         btn->setStyleSheet(QString("QPushButton{background:%1;}").arg(colors.at(i)));

  50.         gridLayout->addWidget(btn, row, column);
  51.         column++;
  52.         index++;

  53.         if (index % columnCount == 0) {
  54.             row++;
  55.             column = 0;
  56.         }

  57.         btns.append(btn);
  58.     }
  59. }

  60. void ColorPanelBtn::btnClicked()
  61. {
  62.     QPushButton *btn = (QPushButton *)sender();
  63.     QString objName = btn->objectName();
  64.     emit colorChanged(QColor(objName.right(7)));
  65. }

  66. QStringList ColorPanelBtn::getColors() const
  67. {
  68.     return this->colors;
  69. }

  70. int ColorPanelBtn::getSpace() const
  71. {
  72.     return this->space;
  73. }

  74. int ColorPanelBtn::getColumnCount() const
  75. {
  76.     return this->columnCount;
  77. }

  78. int ColorPanelBtn::getBorderRadius() const
  79. {
  80.     return this->borderRadius;
  81. }

  82. int ColorPanelBtn::getBorderWidth() const
  83. {
  84.     return this->borderWidth;
  85. }

  86. QColor ColorPanelBtn::getBorderColor() const
  87. {
  88.     return this->borderColor;
  89. }

  90. QSize ColorPanelBtn::sizeHint() const
  91. {
  92.     return QSize(400, 300);
  93. }

  94. QSize ColorPanelBtn::minimumSizeHint() const
  95. {
  96.     return QSize(40, 30);
  97. }

  98. void ColorPanelBtn::setColors(const QStringList &colors)
  99. {
  100.     if (this->colors != colors) {
  101.         this->colors = colors;
  102.         this->initBtn();
  103.     }
  104. }

  105. void ColorPanelBtn::setSpace(int space)
  106. {
  107.     if (this->space != space) {
  108.         this->space = space;
  109.         this->gridLayout->setSpacing(space);
  110.     }
  111. }

  112. void ColorPanelBtn::setColumnCount(int columnCount)
  113. {
  114.     if (this->columnCount != columnCount) {
  115.         this->columnCount = columnCount;
  116.         this->initBtn();
  117.     }
  118. }

  119. void ColorPanelBtn::setBorderRadius(int borderRadius)
  120. {
  121.     if (this->borderRadius != borderRadius) {
  122.         this->borderRadius = borderRadius;
  123.         this->initStyle();
  124.     }
  125. }

  126. void ColorPanelBtn::setBorderWidth(int borderWidth)
  127. {
  128.     if (this->borderWidth != borderWidth) {
  129.         this->borderWidth = borderWidth;
  130.         this->initStyle();
  131.     }
  132. }

  133. void ColorPanelBtn::setBorderColor(const QColor &borderColor)
  134. {
  135.     if (this->borderColor != borderColor) {
  136.         this->borderColor = borderColor;
  137.         this->initStyle();
  138.     }
  139. }
复制代码


六、控件介绍
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、学习方法、写作技巧、热门技术、职场发展等内容,干货多多,福利多多!




本帖子中包含更多资源

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

x
回复

使用道具 举报

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

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