#pragma execution_character_set("utf-8")
#include "lightbutton.h"
#include "qpainter.h"
#include "qpainterpath.h"
#include "qevent.h"
#include "qtimer.h"
#include "qdebug.h"
LightButton::LightButton(QWidget *parent) : QWidget(parent)
{
text = "";
textColor = QColor(255, 255, 255);
alarmColor = QColor(255, 107, 107);
normalColor = QColor(10, 10, 10);
borderOutColorStart = QColor(255, 255, 255);
borderOutColorEnd = QColor(166, 166, 166);
borderInColorStart = QColor(166, 166, 166);
borderInColorEnd = QColor(255, 255, 255);
bgColor = QColor(100, 184, 255);
showRect = false;
showOverlay = true;
overlayColor = QColor(255, 255, 255);
canMove = false;
pressed = false;
this->installEventFilter(this);
isAlarm = false;
timerAlarm = new QTimer(this);
connect(timerAlarm, SIGNAL(timeout()), this, SLOT(alarm()));
timerAlarm->setInterval(500);
}
bool LightButton::eventFilter(QObject *watched, QEvent *event)
{
int type = event->type();
QMouseEvent *mouseEvent = (QMouseEvent *)event;
if (type == QEvent::MouseButtonPress) {
if (this->rect().contains(mouseEvent->pos()) && (mouseEvent->button() == Qt::LeftButton)) {
lastPoint = mouseEvent->pos();
pressed = true;
}
} else if (type == QEvent::MouseMove && pressed) {
if (canMove) {
int dx = mouseEvent->pos().x() - lastPoint.x();
int dy = mouseEvent->pos().y() - lastPoint.y();
this->move(this->x() + dx, this->y() + dy);
}
} else if (type == QEvent::MouseButtonRelease && pressed) {
pressed = false;
Q_EMIT clicked();
}
return QWidget::eventFilter(watched, event);
}
void LightButton::paintEvent(QPaintEvent *)
{
int width = this->width();
int height = this->height();
int side = qMin(width, height);
//绘制准备工作,启用反锯齿,平移坐标轴中心,等比例缩放
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
if (showRect) {
//绘制矩形区域
painter.setPen(Qt::NoPen);
painter.setBrush(bgColor);
painter.drawRoundedRect(this->rect(), 5, 5);
//绘制文字
if (!text.isEmpty()) {
QFont font;
font.setPixelSize(side - 20);
painter.setFont(font);
painter.setPen(textColor);
painter.drawText(this->rect(), Qt::AlignCenter, text);
}
} else {
painter.translate(width / 2, height / 2);
painter.scale(side / 200.0, side / 200.0);
//绘制外边框
drawBorderOut(&painter);
//绘制内边框
drawBorderIn(&painter);
//绘制内部指示颜色
drawBg(&painter);
//绘制居中文字
drawText(&painter);
//绘制遮罩层
drawOverlay(&painter);
}
}
void LightButton::drawBorderOut(QPainter *painter)
{
int radius = 99;
painter->save();
painter->setPen(Qt::NoPen);
QLinearGradient borderGradient(0, -radius, 0, radius);
borderGradient.setColorAt(0, borderOutColorStart);
borderGradient.setColorAt(1, borderOutColorEnd);
painter->setBrush(borderGradient);
painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
painter->restore();
}
void LightButton::drawBorderIn(QPainter *painter)
{
int radius = 90;
painter->save();
painter->setPen(Qt::NoPen);
QLinearGradient borderGradient(0, -radius, 0, radius);
borderGradient.setColorAt(0, borderInColorStart);
borderGradient.setColorAt(1, borderInColorEnd);
painter->setBrush(borderGradient);
painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
painter->restore();
}
void LightButton::drawBg(QPainter *painter)
{
int radius = 80;
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(bgColor);
painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
painter->restore();
}
void LightButton::drawText(QPainter *painter)
{
if (text.isEmpty()) {
return;
}
int radius = 100;
painter->save();
QFont font;
font.setPixelSize(85);
painter->setFont(font);
painter->setPen(textColor);
QRect rect(-radius, -radius, radius * 2, radius * 2);
painter->drawText(rect, Qt::AlignCenter, text);
painter->restore();
}
void LightButton::drawOverlay(QPainter *painter)
{
if (!showOverlay) {
return;
}
int radius = 80;
painter->save();
painter->setPen(Qt::NoPen);
QPainterPath smallCircle;
QPainterPath bigCircle;
radius -= 1;
smallCircle.addEllipse(-radius, -radius, radius * 2, radius * 2);
radius *= 2;
bigCircle.addEllipse(-radius, -radius + 140, radius * 2, radius * 2);
//高光的形状为小圆扣掉大圆的部分
QPainterPath highlight = smallCircle - bigCircle;
QLinearGradient linearGradient(0, -radius / 2, 0, 0);
overlayColor.setAlpha(100);
linearGradient.setColorAt(0.0, overlayColor);
overlayColor.setAlpha(30);
linearGradient.setColorAt(1.0, overlayColor);
painter->setBrush(linearGradient);
painter->rotate(-20);
painter->drawPath(highlight);
painter->restore();
}
QSize LightButton::sizeHint() const
{
return QSize(100, 100);
}
QSize LightButton::minimumSizeHint() const
{
return QSize(10, 10);
}
QString LightButton::getText() const
{
return this->text;
}
void LightButton::setText(const QString &text)
{
if (this->text != text) {
this->text = text;
this->update();
}
}
QColor LightButton::getTextColor() const
{
return this->textColor;
}
void LightButton::setTextColor(const QColor &textColor)
{
if (this->textColor != textColor) {
this->textColor = textColor;
this->update();
}
}
QColor LightButton::getAlarmColor() const
{
return this->alarmColor;
}
void LightButton::setAlarmColor(const QColor &alarmColor)
{
if (this->alarmColor != alarmColor) {
this->alarmColor = alarmColor;
this->update();
}
}
QColor LightButton::getNormalColor() const
{
return this->normalColor;
}
void LightButton::setNormalColor(const QColor &normalColor)
{
if (this->normalColor != normalColor) {
this->normalColor = normalColor;
this->update();
}
}
QColor LightButton::getBorderOutColorStart() const
{
return this->borderOutColorStart;
}
void LightButton::setBorderOutColorStart(const QColor &borderOutColorStart)
{
if (this->borderOutColorStart != borderOutColorStart) {
this->borderOutColorStart = borderOutColorStart;
this->update();
}
}
QColor LightButton::getBorderOutColorEnd() const
{
return this->borderOutColorEnd;
}
void LightButton::setBorderOutColorEnd(const QColor &borderOutColorEnd)
{
if (this->borderOutColorEnd != borderOutColorEnd) {
this->borderOutColorEnd = borderOutColorEnd;
this->update();
}
}
QColor LightButton::getBorderInColorStart() const
{
return this->borderInColorStart;
}
void LightButton::setBorderInColorStart(const QColor &borderInColorStart)
{
if (this->borderInColorStart != borderInColorStart) {
this->borderInColorStart = borderInColorStart;
this->update();
}
}
QColor LightButton::getBorderInColorEnd() const
{
return this->borderInColorEnd;
}
void LightButton::setBorderInColorEnd(const QColor &borderInColorEnd)
{
if (this->borderInColorEnd != borderInColorEnd) {
this->borderInColorEnd = borderInColorEnd;
this->update();
}
}
QColor LightButton::getBgColor() const
{
return this->bgColor;
}
void LightButton::setBgColor(const QColor &bgColor)
{
if (this->bgColor != bgColor) {
this->bgColor = bgColor;
this->update();
}
}
bool LightButton::getCanMove() const
{
return this->canMove;
}
void LightButton::setCanMove(bool canMove)
{
if (this->canMove != canMove) {
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
用法链接:https://menghui666.blog.csdn.net/article/details/137476966?spm=1001.2014.3001.5502 基于Qt和C++实现的高亮发光按钮控件+源码 基于Qt和C++实现的高亮发光按钮控件+源码 基于Qt和C++实现的高亮发光按钮控件+源码 基于Qt和C++实现的高亮发光按钮控件+源码 基于Qt和C++实现的高亮发光按钮控件+源码 基于Qt和C++实现的高亮发光按钮控件+源码
资源推荐
资源详情
资源评论
收起资源包目录
lightbutton.zip (11个子文件)
lightbutton
frmlightbutton.h 393B
lightbutton.vcxproj 21KB
ui_frmlightbutton.h 2KB
frmlightbutton.ui 877B
lightbutton.h 5KB
lightbutton.vcxproj.user 168B
main.cpp 819B
lightbutton.vcxproj.filters 3KB
lightbutton.cpp 10KB
lightbutton.pro 397B
frmlightbutton.cpp 1KB
共 11 条
- 1
资源评论
梦回阑珊
- 粉丝: 5186
- 资源: 1681
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- (源码)基于C语言的系统服务框架.zip
- (源码)基于Spring MVC和MyBatis的选课管理系统.zip
- (源码)基于ArcEngine的GIS数据处理系统.zip
- (源码)基于JavaFX和MySQL的医院挂号管理系统.zip
- (源码)基于IdentityServer4和Finbuckle.MultiTenant的多租户身份认证系统.zip
- (源码)基于Spring Boot和Vue3+ElementPlus的后台管理系统.zip
- (源码)基于C++和Qt框架的dearoot配置管理系统.zip
- (源码)基于 .NET 和 EasyHook 的虚拟文件系统.zip
- (源码)基于Python的金融文档智能分析系统.zip
- (源码)基于Java的医药管理系统.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功