Files
QT_wuhan_YT/widgets/ledindicator.h
T
2026-09-10 14:10:40 +08:00

41 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#ifndef LEDINDICATOR_H
#define LEDINDICATOR_H
#include <QWidget>
#include <QTimer>
// ===================== 指示灯(自绘圆形渐变 + 外发光) =====================
// 状态:灰=未运行/关闭/正常,绿=运行/开启/到位,红=故障/液位低,
// 黄=检测中;红闪/黄闪按 500ms 周期闪烁(透明度 40%↔100%)。
class LedIndicator : public QWidget
{
Q_OBJECT
public:
enum State { Off, Green, Red, RedBlink, Yellow, YellowBlink };
explicit LedIndicator(const QString& text, QWidget* parent = nullptr);
void setState(State s);
void setText(const QString& t);
void setTextPixelSize(int px) { m_textPx = px; updateGeometry(); update(); } // 文字字号
void setRadius(int r) { m_radius = r; updateGeometry(); update(); } // 圆形指示点半径
State state() const { return m_state; }
QSize sizeHint() const override; // 宽度按文字实际宽度计算,避免文字被裁
protected:
void paintEvent(QPaintEvent*) override;
private:
int textLeft() const { return m_radius * 2 + 18; } // 文字起点:圆点区 + 左侧 10px 留白
State m_state = Off;
QString m_text;
QTimer* m_blink = nullptr;
bool m_on = true;
int m_radius = 16; // 圆形指示点 32×32
int m_textPx = 24; // 文字字号
};
#endif // LEDINDICATOR_H