自学内容网 自学内容网

C++ QT 自绘呼吸灯

功能

  • 使用QLabel生成一个呼吸灯的效果,用于显示某个状态的变化
  • h
#ifndef CUELIGHTLABEL_H
#define CUELIGHTLABEL_H

#include <QLabel>
#include <QPropertyAnimation>

class CueLightLabel : public QLabel
{
    Q_OBJECT
    Q_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 enabled
    void 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);
}


原文地址:https://blog.csdn.net/weixin_49065061/article/details/145142555

免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!