#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();
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
SignalWidget.rar (7个子文件)
SignalWidget
mainwindow.h 407B
signalwidget.h 551B
mainwindow.cpp 571B
SignalWidget.pro 700B
main.cpp 172B
mainwindow.ui 2KB
signalwidget.cpp 2KB
共 7 条
- 1
资源评论
秀木易风
- 粉丝: 1654
- 资源: 34
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功