博客
关于我
Qt LED 开关效果
阅读量:619 次
发布时间:2019-03-13

本文共 3319 字,大约阅读时间需要 11 分钟。

Qt LED 开关效果

Qt Led开关效果

以下源码来自开源项目

程序效果

在这里插入图片描述

hled.h

#ifndef HLED_H#define HLED_H#include 
class QColor;class HLed : public QWidget{ Q_OBJECTpublic: HLed(QWidget *parent = 0); ~HLed(); QColor color() const; QSize sizeHint() const; QSize minimumSizeHint() const;public slots: void setColor(const QColor &color); void toggle(); void turnOn(bool on=true); void turnOff(bool off=true);protected: void paintEvent(QPaintEvent *); int ledWidth() const;private: struct Private; Private * const m_d;};#endif // HLED_H

hled.cpp

#include 
#include "hled.h"struct HLed::Private{ public: Private() : darkerFactor(300), color(Qt::green), isOn(true) { } int darkerFactor; QColor color; bool isOn;};HLed::HLed(QWidget *parent) :QWidget(parent), m_d(new Private){ }HLed::~HLed(){ delete m_d;}QColor HLed::color() const{ return m_d->color;}void HLed::setColor(const QColor &color){ if (m_d->color == color) return; update();}QSize HLed::sizeHint() const{ return QSize(20, 20);}QSize HLed::minimumSizeHint() const{ return QSize(16, 16);}void HLed::toggle(){ m_d->isOn = !m_d->isOn; update();}void HLed::turnOn(bool on){ m_d->isOn = on; update();}void HLed::turnOff(bool off){ turnOn(!off);}void HLed::paintEvent(QPaintEvent * /*event*/){ int width = ledWidth(); QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); QColor color = m_d->isOn ? m_d->color : m_d->color.darker(m_d->darkerFactor); QBrush brush; brush.setStyle(Qt::SolidPattern); brush.setColor(color); painter.setBrush(brush); // draw plain painter.drawEllipse(1, 1, width-1, width-1); QPen pen; pen.setWidth(2); int pos = width / 5 + 1; int lightWidth = width * 2 / 3; int lightQuote = 130 * 2 / (lightWidth ? lightWidth : 1) + 100; // draw bright spot while (lightWidth) { color = color.lighter(lightQuote); pen.setColor(color); painter.setPen(pen); painter.drawEllipse(pos, pos, lightWidth, lightWidth); lightWidth--; if (!lightWidth) break; painter.drawEllipse(pos, pos, lightWidth, lightWidth); lightWidth--; if (!lightWidth) break; painter.drawEllipse(pos, pos, lightWidth, lightWidth); pos++; lightWidth--; } //draw border painter.setBrush(Qt::NoBrush); int angle = -720; color = palette().color(QPalette::Light); for (int arc=120; arc<2880; arc+=240) { pen.setColor(color); painter.setPen(pen); int w = width - pen.width()/2; painter.drawArc(pen.width()/2, pen.width()/2, w, w, angle+arc, 240); painter.drawArc(pen.width()/2, pen.width()/2, w, w, angle-arc, 240); color = color.darker(110); }}int HLed::ledWidth() const{ int width = qMin(this->width(), this->height()); width -= 2; return width > 0 ? width : 0;}

调用示例

HLed *led;led = new HLed(Dialog);led->setObjectName(QString::fromUtf8("led"));led->setSizePolicy(sizePolicy);led->setMinimumSize(QSize(20, 20));led->setMaximumSize(QSize(25, 25));horizontalLayout->addWidget(led);led->turnOff();//默认关闭//update led's statusled->turnOn(port->isOpen());

源码下载

Github无法访问时可以通过下发地址直接下载

转载地址:http://hwxaz.baihongyu.com/

你可能感兴趣的文章
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 添加列,修改列,删除列
查看>>
mysql 添加索引
查看>>
MySQL 添加索引,删除索引及其用法
查看>>
mysql 状态检查,备份,修复
查看>>
MySQL 用 limit 为什么会影响性能?
查看>>
MySQL 用 limit 为什么会影响性能?有什么优化方案?
查看>>
MySQL 用户权限管理:授权、撤销、密码更新和用户删除(图文解析)
查看>>