欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > C++ QT 自绘呼吸灯

C++ QT 自绘呼吸灯

2025/1/18 22:28:48 来源:https://blog.csdn.net/weixin_49065061/article/details/145142555  浏览:    关键词:C++ QT 自绘呼吸灯

功能

  • 使用QLabel生成一个呼吸灯的效果,用于显示某个状态的变化
  • h
#ifndef CUELIGHTLABEL_H
#define CUELIGHTLABEL_H#include <QLabel>
#include <QPropertyAnimation>class CueLightLabel : public QLabel
{Q_OBJECTQ_PROPERTY(QColor color READ getColor WRITE setColor)public:enum CueLightStatus{Online = 0, // 在线Offline,    // 离线Dropped,    // 掉线Warning,    // 报警Grayedg,    // 置灰};explicit CueLightLabel(QWidget *parent = nullptr);~CueLightLabel();/// @brief 设置通信状态void setCommunicationStatus(bool status);/// @brief 设置状态void setStatus(CueLightStatus status);/// @brief 设置闪烁效果/// @param enabledvoid setBreathingEffectEnabled(bool enabled);QColor getColor() const { return m_color; }void setColor(QColor color) { m_color = color; }protected:void paintEvent(QPaintEvent *event) override;private:CueLightStatus mCurrentStatus;QPropertyAnimation *mAnimation = nullptr;QColor m_color;
};#endif
  • cpp
#include "cuelightlabel.h"
#include <QPainter>
#include <QPen>
#include <QBrush>
#include <QToolTip>
#include <QObject>CueLightLabel::CueLightLabel(QWidget *parent) : QLabel(parent), mCurrentStatus(Offline)
{setStyleSheet("QToolTip { color: #ffffff; background-color: #081640; }");
}CueLightLabel::~CueLightLabel()
{delete mAnimation;
}void CueLightLabel::setCommunicationStatus(bool status)
{if (status){setStatus(CueLightLabel::Offline);setBreathingEffectEnabled(true);setToolTip("通信失败");}else{setStatus(CueLightLabel::Online);setBreathingEffectEnabled(false);setToolTip("通信成功");}
}void CueLightLabel::setStatus(CueLightStatus status)
{if (mCurrentStatus != status){mCurrentStatus = status;update();}
}void CueLightLabel::setBreathingEffectEnabled(bool enabled)
{if (enabled){if (!mAnimation){mAnimation = new QPropertyAnimation(this, "color");mAnimation->setDuration(1000);mAnimation->setStartValue(QColor(Qt::transparent));mAnimation->setEndValue(QColor(Qt::black));mAnimation->setLoopCount(-1);mAnimation->setEasingCurve(QEasingCurve::InOutSine);connect(mAnimation, &QPropertyAnimation::valueChanged, this, QOverload<>::of(&QLabel::update));}mAnimation->start();}else{if (mAnimation){mAnimation->stop();}}
}void CueLightLabel::paintEvent(QPaintEvent *event)
{QLabel::paintEvent(event);QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing);QColor baseColor;switch (mCurrentStatus){case Online:baseColor = Qt::green;break;case Offline:baseColor = Qt::red;break;case Dropped:baseColor = Qt::gray;break;case Warning:baseColor = Qt::yellow;break;case Grayedg:baseColor = Qt::white;break;}QColor color = baseColor;if (mAnimation && mAnimation->state() == QAbstractAnimation::Running){QColor fadeColor = qvariant_cast<QColor>(mAnimation->currentValue());color.setAlpha(fadeColor.alpha());}painter.setPen(Qt::NoPen);painter.setBrush(color);int side = qMin(width(), height()) / 2;QRect circleRect((width() - side) / 2, (height() - side) / 2, side, side);painter.drawEllipse(circleRect);
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com