#pragma execution_character_set("utf-8")
#include "lcdnumber.h"
#include "qpainter.h"
#include "qtimer.h"
#include "qdebug.h"
LcdNumber::LcdNumber(QWidget *parent) : QWidget(parent)
{
number = 0;
space = 20;
bgColorStart = QColor(100, 100, 100);
bgColorEnd = QColor(60, 60, 60);
numberColorStart = QColor(100, 184, 255);
numberColorEnd = QColor(79, 148, 205);
}
void LcdNumber::paintEvent(QPaintEvent *)
{
//绘制准备工作,启用反锯齿
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
//绘制背景
drawBg(&painter);
//绘制数字
drawNumber(&painter);
}
void LcdNumber::drawBg(QPainter *painter)
{
painter->save();
painter->setPen(Qt::NoPen);
QLinearGradient bgGradient(QPointF(0, 0), QPointF(0, height()));
bgGradient.setColorAt(0.0, bgColorStart);
bgGradient.setColorAt(1.0, bgColorEnd);
painter->setBrush(bgGradient);
painter->drawRect(rect());
painter->restore();
}
void LcdNumber::drawNumber(QPainter *painter)
{
painter->save();
painter->setPen(Qt::NoPen);
//找出六个对应点
QPointF topLeft(space, space);
QPointF topRight(width() - space, space);
QPointF midLeft(space, height() / 2);
QPointF midRight(width() - space, height() / 2);
QPointF bottomLeft(space, height() - space);
QPointF bottomRight(width() - space, height() - space);
//形状宽度及凸起部分高度
double recWidth = width() / 10;
double recHeight = height() / 30;
double recSpace = width() / 30;
//逐个将每个形状的点集合添加,每个数码管都有7个形状,每个形状有4-6个点
//左侧上部分形状坐标集合
QVector<QPointF> topLeftRectVec;
topLeftRectVec.append(QPointF(topLeft.x(), topLeft.y()));
topLeftRectVec.append(QPointF(topLeft.x() + recWidth, topLeft.y() + recHeight * 2));
topLeftRectVec.append(QPointF(midLeft.x() + recWidth, midLeft.y() - recHeight - recSpace));
topLeftRectVec.append(QPointF(midLeft.x() + recWidth / 2, midLeft.y() - recSpace));
topLeftRectVec.append(QPointF(midLeft.x(), midLeft.y() - recHeight - recSpace));
//顶部形状坐标集合
QVector<QPointF> topRectVec;
topRectVec.append(QPointF(topLeft.x() + recSpace, topLeft.y()));
topRectVec.append(QPointF(topLeft.x() + recWidth + recSpace, topLeft.y() + recHeight * 2));
topRectVec.append(QPointF(topRight.x() - recWidth - recSpace, topRight.y() + recHeight * 2));
topRectVec.append(QPointF(topRight.x() - recSpace, topRight.y()));
//右侧上部分形状坐标集合
QVector<QPointF> topRightRectVec;
topRightRectVec.append(QPointF(topRight.x() - recWidth, topRight.y() + recHeight * 2));
topRightRectVec.append(QPointF(topRight.x(), topRight.y()));
topRightRectVec.append(QPointF(midRight.x(), midRight.y() - recHeight - recSpace));
topRightRectVec.append(QPointF(midRight.x() - recWidth / 2, midRight.y() - recSpace));
topRightRectVec.append(QPointF(midRight.x() - recWidth, midRight.y() - recHeight - recSpace));
//中间部分形状坐标集合
QVector<QPointF> midRectVec;
midRectVec.append(QPointF(midLeft.x() + recWidth / 2, midLeft.y()));
midRectVec.append(QPointF(midLeft.x() + recWidth, midLeft.y() - recHeight));
midRectVec.append(QPointF(midRight.x() - recWidth, midRight.y() - recHeight));
midRectVec.append(QPointF(midRight.x() - recWidth / 2, midRight.y()));
midRectVec.append(QPointF(midRight.x() - recWidth, midRight.y() + recHeight));
midRectVec.append(QPointF(midLeft.x() + recWidth, midRight.y() + recHeight));
//左侧下部分形状坐标集合
QVector<QPointF> bottomLeftRectVec;
bottomLeftRectVec.append(QPointF(midLeft.x(), midLeft.y() + recHeight + recSpace));
bottomLeftRectVec.append(QPointF(midLeft.x() + recWidth / 2, midLeft.y() + recSpace));
bottomLeftRectVec.append(QPointF(midLeft.x() + recWidth, midLeft.y() + recHeight + recSpace));
bottomLeftRectVec.append(QPointF(bottomLeft.x() + recWidth, bottomLeft.y() - recHeight * 2));
bottomLeftRectVec.append(QPointF(bottomLeft.x(), bottomLeft.y()));
//底部形状坐标集合
QVector<QPointF> bottomRectVec;
bottomRectVec.append(QPointF(bottomLeft.x() + recSpace, bottomLeft.y()));
bottomRectVec.append(QPointF(bottomLeft.x() + recWidth + recSpace, bottomRight.y() - recHeight * 2));
bottomRectVec.append(QPointF(bottomRight.x() - recWidth - recSpace, bottomRight.y() - recHeight * 2));
bottomRectVec.append(QPointF(bottomRight.x() - recSpace, bottomRight.y()));
//右侧下部分形状坐标集合
QVector<QPointF> bottomRightRectVec;
bottomRightRectVec.append(QPointF(midRight.x() - recWidth, midRight.y() + recHeight + recSpace));
bottomRightRectVec.append(QPointF(midRight.x() - recWidth / 2, midRight.y() + recSpace));
bottomRightRectVec.append(QPointF(midRight.x(), midRight.y() + recHeight + recSpace));
bottomRightRectVec.append(QPointF(bottomRight.x(), bottomRight.y()));
bottomRightRectVec.append(QPointF(bottomRight.x() - recWidth, bottomRight.y() - recHeight * 2));
//颜色渐变
QLinearGradient numberGradient(QPointF(0, 0), QPointF(0, height()));
numberGradient.setColorAt(0.0, numberColorStart);
numberGradient.setColorAt(1.0, numberColorEnd);
painter->setBrush(numberGradient);
//根据值绘制形状
switch (number) {
case 0:
painter->drawPolygon(topLeftRectVec);
painter->drawPolygon(topRectVec);
painter->drawPolygon(topRightRectVec);
//painter->drawPolygon(midRectVec);
painter->drawPolygon(bottomLeftRectVec);
painter->drawPolygon(bottomRectVec);
painter->drawPolygon(bottomRightRectVec);
break;
case 1:
//painter->drawPolygon(topLeftRectVec);
//painter->drawPolygon(topRectVec);
painter->drawPolygon(topRightRectVec);
//painter->drawPolygon(midRectVec);
//painter->drawPolygon(bottomLeftRectVec);
//painter->drawPolygon(bottomRectVec);
painter->drawPolygon(bottomRightRectVec);
break;
case 2:
//painter->drawPolygon(topLeftRectVec);
painter->drawPolygon(topRectVec);
painter->drawPolygon(topRightRectVec);
painter->drawPolygon(midRectVec);
painter->drawPolygon(bottomLeftRectVec);
painter->drawPolygon(bottomRectVec);
//painter->drawPolygon(bottomRightRectVec);
break;
case 3:
//painter->drawPolygon(topLeftRectVec);
painter->drawPolygon(topRectVec);
painter->drawPolygon(topRightRectVec);
painter->drawPolygon(midRectVec);
//painter->drawPolygon(bottomLeftRectVec);
painter->drawPolygon(bottomRectVec);
painter->drawPolygon(bottomRightRectVec);
break;
case 4:
painter->drawPolygon(topLeftRectVec);
//painter->drawPolygon(topRectVec);
painter->drawPolygon(topRightRectVec);
painter->drawPolygon(midRectVec);
//painter->drawPolygon(bottomLeftRectVec);
//painter->drawPolygon(bottomRectVec);
painter->drawPolygon(bottomRightRectVec);
break;
case 5:
painter->drawPolygon(topLeftRectVec);
painter->drawPolygon(topRectVec);
//painter->drawPolygon(topRightRectVec);
painter->drawPolygon(midRectVec);
//painter->drawPolygon(bottomLeftRectVec);
painter->drawPolygon(bottomRectVec);
painter->drawPolygon(bottomRightRectVec);
break;
case 6:
painter->drawPolygon(topLeftRectVec);
painter->drawPolygon(topRectVec);
//painter->drawPolygon(topRightRectVec);
painter->drawPolygon(midRectVec);
painter->drawPolygon(bottomLeft
没有合适的资源?快使用搜索试试~ 我知道了~
lcdnumber.7z
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共7个文件
cpp:3个
h:2个
pro:1个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
![star](https://csdnimg.cn/release/downloadcmsfe/public/img/star.98a08eaa.png)
温馨提示
基于QT的LCD数字显示,数字类似于数码管的风格显示
资源推荐
资源详情
资源评论
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![application/octet-stream](https://img-home.csdnimg.cn/images/20210720083646.png)
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![none](https://img-home.csdnimg.cn/images/20210720083646.png)
![thumb](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![none](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![deb](https://img-home.csdnimg.cn/images/20210720083646.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 7 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
- Jack.WH2022-03-08用户下载后在一定时间内未进行评价,系统默认好评。
![avatar](https://profile-avatar.csdnimg.cn/11124d2ce5db41368adec8b1ad4e99f7_xxzhaoming.jpg!1)
![avatar-vip](https://csdnimg.cn/release/downloadcmsfe/public/img/user-vip.1c89f3c5.png)
应用市场
- 粉丝: 469
- 资源: 3817
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)