• 392阅读
  • 0回复

Qt编写自定义控件27-颜色按钮面板 [复制链接]

上一主题 下一主题
离线liudianwu
 


一、前言
颜色按钮面板主要用在提供一个颜色按钮面板,用户单击某个按钮,然后拿到对应的颜色值,用户可以预先设定常用的颜色集合,传入到控件中,自动生成面板颜色集合按钮,每当滑过按钮的时候,按钮边缘高亮提示当前所在颜色的按钮,当选中某个按钮时,右侧颜色条显示当前选中的颜色,此控件功能极其简单,直接采用动态生成按钮的方式,设置按钮的样式表来设置对应的颜色和高亮边框等,单击按钮发出颜色改变信号即可,对外提供该信号就行,非常适合初学者学习。

二、实现的功能
* 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. }

专业各种自定义控件编写+UI定制+输入法定制+视频监控+工业控制+仪器仪表+嵌入式linux+各种串口网络通信,童叟无欺,量大从优,欢迎咨询购买定制!你正好需要,我正好专业!QQ:517216493 微信:feiyangqingyun Email:feiyangqingyun@163.com
群号:853086607(Qt交流大会,雨田哥群,不定期上传作品,解答作品中相关问题!) 312125701(QtQML多多指教群) 46679801(Qt开发技术交流群-5000人大群)
快速回复
限100 字节
 
上一个 下一个