QT实现4G信号图标显示

        显示信号强度的图标,模拟类似手机的信号强度图标的绘制,比较简单,欢迎参考。

一、简述

        使用Qt实现4G信号图标。

二、效果 

三、核心代码  
 1、头文件
#ifndef SIGNALWIDGET_H
#define SIGNALWIDGET_H

#include <QWidget>

class SignalWidget : public QWidget
{
    Q_OBJECT
public:
    explicit SignalWidget(QWidget *parent = nullptr);
    ~SignalWidget();

    //信号强度
    enum SignalStrength{
        Zero,
        One,
        Two,
        Three,
        Four
    };

    //设置信号强度
    void setSignalStrength(SignalStrength newSignalStrength);

protected:
    void paintEvent(QPaintEvent *event) override;

private:
    SignalStrength m_signalStrength;

};

#endif // SIGNALWIDGET_H
2、cpp代码
#include "signalwidget.h"
#include <QPainter>

SignalWidget::SignalWidget(QWidget *parent) : QWidget(parent)
{
    m_signalStrength = SignalStrength::Zero;
}

SignalWidget::~SignalWidget()
{

}

void SignalWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing);

    float hUnit = this->height() / 5.0;
    int h1 = hUnit * 1;
    int h2 = hUnit * 2;
    int h3 = hUnit * 3;
    int h4 = hUnit * 4;

    //widget 宽度分为七份,四个信号柱及三个空白区域各占一份
    float wUnit = this->width() / 7.0;
    int x1 = 1;
    int x2 = wUnit * 2+1;
    int x3 = wUnit * 4;
    int x4 = wUnit * 6 - 1;

    QColor havaSignal = QColor(20, 18, 19);
    QColor noSignal = Qt::lightGray;

    QColor colorOne, colorTwo, colorThree, colorFour;
    colorOne = noSignal;
    colorTwo = noSignal;
    colorThree = noSignal;
    colorFour = noSignal;

    switch(m_signalStrength)
    {
    case SignalStrength::Four:
        colorFour = havaSignal;
    case SignalStrength::Three:
        colorThree = havaSignal;
    case SignalStrength::Two:
        colorTwo = havaSignal;
    case SignalStrength::One:
        colorOne = havaSignal;
    default: break;
    };

    p.setPen(colorFour);
    p.setBrush(colorFour);
    p.drawRect(x4, this->height() - h4, wUnit, h4);

    p.setPen(colorThree);
    p.setBrush(colorThree);
    p.drawRect(x3, this->height() - h3, wUnit, h3);

    p.setPen(colorTwo);
    p.setBrush(colorTwo);
    p.drawRect(x2, this->height() - h2, wUnit, h2);

    p.setPen(colorOne);
    p.setBrush(colorOne);
    p.drawRect(x1, this->height() - h1, wUnit, h1);

    QFont font;
    font.setPixelSize(this->height() / 2.0);
    font.setBold(true);
    p.setFont(font);
    QString text = "4G";
    p.drawText(0, this->height() - h2-5,text);
    if(m_signalStrength == SignalStrength::Zero)
    {
        QFontMetrics metrics(font);
        int w = metrics.width(text);
        int h = metrics.height();

        p.drawLine(x1, this->height() - h,w,1);
        p.drawLine(x1, 1,w,this->height() - h);
    }
}


void SignalWidget::setSignalStrength(SignalStrength newSignalStrength)
{
    m_signalStrength = newSignalStrength;
    this->update();
}
 3、示例代码
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值