当前位置:   article > 正文

Qt编写自定义控件:彩色渐变圆角按钮之一_qt 漂亮按钮

qt 漂亮按钮

代码:

  1. #ifndef COLORGRADIENTROUNDEDBUTTON_H
  2. #define COLORGRADIENTROUNDEDBUTTON_H
  3. #include <QAbstractButton>
  4. struct doubleColor
  5. {
  6. doubleColor(QColor frist = Qt::red,QColor second = Qt::blue)
  7. :fristColor(frist),secondColor(second)
  8. {}
  9. QColor fristColor;
  10. QColor secondColor;
  11. bool operator !=(const doubleColor & c)
  12. {
  13. return (this->fristColor != c.fristColor) || (this->secondColor != c.secondColor);
  14. }
  15. };
  16. Q_DECLARE_METATYPE(doubleColor)
  17. class ColorGradientRoundedButton : public QAbstractButton
  18. {
  19. Q_OBJECT
  20. Q_PROPERTY(doubleColor currentColor MEMBER currentColor)
  21. public:
  22. ColorGradientRoundedButton(QWidget *parent = nullptr);
  23. ~ColorGradientRoundedButton()override;
  24. protected:
  25. void paintEvent(QPaintEvent *event)override;
  26. void enterEvent(QEnterEvent *event)override;
  27. void leaveEvent(QEvent *event)override;
  28. private:
  29. void onValueChanged(const QVariant &value);
  30. doubleColor currentColor;
  31. doubleColor startColor;
  32. doubleColor endColor;
  33. class QPropertyAnimation * animation{nullptr};
  34. };
  35. #endif // COLORGRADIENTROUNDEDBUTTON_H
  1. #include "colorgradientroundedbutton.h"
  2. #include <QPainter>
  3. #include <QPaintEvent>
  4. #include <QGraphicsDropShadowEffect>
  5. #include <QPropertyAnimation>
  6. #include <QDebug>
  7. #include <QPainterPath>
  8. #include <QRandomGenerator>
  9. QVariant myColorInterpolator(const doubleColor &start, const doubleColor &end, qreal progress)
  10. {
  11. auto fr = start.fristColor.red() + ((end.fristColor.red() - start.fristColor.red()) * progress);
  12. auto fg = start.fristColor.green() + ((end.fristColor.green() - start.fristColor.green()) * progress);
  13. auto fb = start.fristColor.blue() + ((end.fristColor.blue() - start.fristColor.blue()) * progress);
  14. auto sr = start.secondColor.red() + ((end.secondColor.red() - start.secondColor.red()) * progress);
  15. auto sg = start.secondColor.green() + ((end.secondColor.green() - start.secondColor.green()) * progress);
  16. auto sb = start.secondColor.blue() + ((end.secondColor.blue() - start.secondColor.blue()) * progress);
  17. return QVariant::fromValue(doubleColor(QColor(fr,fg,fb),QColor(sr,sg,sb)));
  18. }
  19. QColor getRandomColor()
  20. {
  21. return QColor(QRandomGenerator::global()->bounded(255),
  22. QRandomGenerator::global()->bounded(255),
  23. QRandomGenerator::global()->bounded(255));
  24. }
  25. ColorGradientRoundedButton::ColorGradientRoundedButton(QWidget *parent)
  26. : QAbstractButton(parent)
  27. {
  28. qRegisterAnimationInterpolator<doubleColor>(myColorInterpolator);
  29. startColor = doubleColor(getRandomColor(),getRandomColor());
  30. endColor = doubleColor(getRandomColor(),getRandomColor());
  31. currentColor = startColor;
  32. this->setMinimumSize(180,50);
  33. setMouseTracking(true);
  34. QGraphicsDropShadowEffect * effect = new QGraphicsDropShadowEffect(this);
  35. setGraphicsEffect(effect);
  36. effect->setOffset(0,0);
  37. effect->setBlurRadius(25);
  38. effect->setColor(Qt::black);
  39. animation = new QPropertyAnimation(this, "currentColor");
  40. animation->setDuration(400);
  41. connect(animation,&QPropertyAnimation::valueChanged,this,&ColorGradientRoundedButton::onValueChanged);
  42. }
  43. ColorGradientRoundedButton::~ColorGradientRoundedButton()
  44. {
  45. }
  46. void ColorGradientRoundedButton::onValueChanged(const QVariant &value)
  47. {
  48. update();
  49. }
  50. void ColorGradientRoundedButton::paintEvent(QPaintEvent *event)
  51. {
  52. QPainter painter(this);
  53. painter.setRenderHint(QPainter::Antialiasing,true);
  54. auto rect = event->rect();
  55. QPainterPath path;
  56. path.addRoundedRect(rect,25,25);
  57. painter.setClipPath(path);
  58. painter.drawRect(rect);
  59. QLinearGradient linearGradient(rect.topLeft(),rect.topRight());
  60. linearGradient.setColorAt(0,currentColor.fristColor);
  61. linearGradient.setColorAt(1,currentColor.secondColor);
  62. painter.fillRect(rect,linearGradient);
  63. auto font = painter.font();
  64. font.setBold(true);
  65. font.setPixelSize(20);
  66. painter.setFont(font);
  67. painter.setPen(Qt::white);
  68. painter.drawText(rect,Qt::AlignCenter,text());
  69. }
  70. void ColorGradientRoundedButton::enterEvent(QEnterEvent *event)
  71. {
  72. if(animation->state() == QAbstractAnimation::Running)
  73. {
  74. animation->stop();
  75. }
  76. animation->setStartValue(QVariant::fromValue(currentColor));
  77. animation->setEndValue(QVariant::fromValue(endColor));
  78. animation->start();
  79. QWidget::enterEvent(event);
  80. }
  81. void ColorGradientRoundedButton::leaveEvent(QEvent *event)
  82. {
  83. if(animation->state() == QAbstractAnimation::Running)
  84. {
  85. animation->stop();
  86. }
  87. animation->setStartValue(QVariant::fromValue(currentColor));
  88. animation->setEndValue(QVariant::fromValue(startColor));
  89. animation->start();
  90. QWidget::leaveEvent(event);
  91. }

使用示例:

  1. QWidget w;
  2. w.setPalette(Qt::white);
  3. auto vb = new QVBoxLayout;
  4. vb->setSpacing(15);
  5. for(int i = 0;i < 8;++i)
  6. {
  7. auto btn = new ColorGradientRoundedButton;
  8. btn->setText(QString("按钮%1").arg(i));
  9. vb->addWidget(btn);
  10. }
  11. w.setLayout(vb);
  12. w.show();

效果:

相关博文:Qt动画框架:QVariantAnimation

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/299596
推荐阅读
相关标签
  

闽ICP备14008679号