当前位置: 首页 > news >正文

Qt实现圆形窗口

重新实现paintEvent()函数。

效果如下:

 效果为蓝色区域,背景是vs接面,代码直接复制可用,留给有需要的人。

#ifndef CircleWidget_h__
#define CircleWidget_h__#include <QWidget>class CCircleWidget : public QWidget
{Q_OBJECTpublic:explicit CCircleWidget(QWidget* parent = 0); // explicit 避免歧义CCircleWidget(const QString& strImagePath, int nRadius, QWidget* parent = 0);public:void SetNormalOutLineColor(QColor color);void SetHoverOutLineColor(QColor color);void SetPressedOutLineColor(QColor color);void SetOutLineWidth(int nWidth);void SetImagePath(const QString& strImagePath);void SetHovered(bool bHovered);void SetPressed(bool bPressed);protected: // overridevoid Init();void paintEvent(QPaintEvent*) override;                // 绘图事件,调用update()时触发void mousePressEvent(QMouseEvent* e) override;          // 鼠标按下事件void mouseReleaseEvent(QMouseEvent* e) override;        // 鼠标释放事件void enterEvent(QEvent*) override;                     // 鼠标进入事件void leaveEvent(QEvent*) override;                     // 鼠标离开事件signals:void SigClicked(); // 点击信号private:QString m_strImagePath; // 图片路径QColor m_normalColor;QColor m_hoverColor;QColor m_pressedColor;int m_nWidth;int m_nRadius;        // 圆形半径bool m_bPressed;      // 左键单击控制bool m_bHovered;      // 鼠标悬停控制
};#endif // CircleWidget_h__
#include "CircleWidget.h"
#include <QPainter>
#include <QMouseEvent>CCircleWidget::CCircleWidget(QWidget* parent): QWidget(parent), m_strImagePath(":/dotnetIcon.png"), m_normalColor("#58a3ef"), m_hoverColor("#6eb8f5"), m_pressedColor("#417cac"), m_nWidth(4), m_nRadius(46), m_bPressed(false), m_bHovered(false)
{Init();
}CCircleWidget::CCircleWidget(const QString& strImagePath, int nRadius, QWidget* parent): QWidget(parent), m_strImagePath(strImagePath), m_normalColor("#58a3ef"), m_hoverColor("#6eb8f5"), m_pressedColor("#417cac"), m_nWidth(4), m_nRadius(nRadius), m_bPressed(false), m_bHovered(false)
{Init();
}void CCircleWidget::SetNormalOutLineColor(QColor color)
{m_normalColor = color;
}void CCircleWidget::SetHoverOutLineColor(QColor color)
{m_hoverColor = color;
}void CCircleWidget::SetPressedOutLineColor(QColor color)
{m_pressedColor = color;
}void CCircleWidget::SetOutLineWidth(int nWidth)
{m_nWidth = nWidth;
}void CCircleWidget::SetImagePath(const QString& strImagePath)
{m_strImagePath = strImagePath;
}void CCircleWidget::SetHovered(bool bHovered)
{m_bHovered = bHovered;update();
}void CCircleWidget::SetPressed(bool bPressed)
{m_bPressed = bPressed;update();
}void CCircleWidget::Init()
{// 无边框setWindowFlags(Qt::FramelessWindowHint);// 窗口背景透明setAttribute(Qt::WA_TranslucentBackground, true);
}void CCircleWidget::paintEvent(QPaintEvent*)
{QPainter p(this);p.setRenderHint(QPainter::Antialiasing);QPen pen;pen.setWidth(m_nWidth);if (m_bPressed){pen.setColor("#417cac"); // 按下时的边框颜色}else if (m_bHovered){pen.setColor("#6eb8f5"); // 悬停时的边框颜色}else{pen.setColor("#58a3ef"); // 正常状态的边框颜色}p.setPen(pen);p.setBrush(Qt::NoBrush);// 设置圆心为圆的半径,以确保圆形完整显示//QPoint center(m_nRadius + m_nWidth / 2, m_nRadius + m_nWidth / 2);QPoint center(rect().center());// 绘制背景圆形p.drawEllipse(center, m_nRadius, m_nRadius);// 绘制图片QPixmap pixmap(m_strImagePath);QRect imageRect(0, 0, 2 * m_nRadius, 2 * m_nRadius); // 计算图片绘制区域QPainterPath clipPath;clipPath.addEllipse(center, m_nRadius, m_nRadius);p.setClipPath(clipPath);p.drawPixmap(center.x() - m_nRadius, center.y() - m_nRadius, imageRect.width(), imageRect.height(), pixmap);
}void CCircleWidget::mousePressEvent(QMouseEvent* e)
{m_bPressed = true;update();
}void CCircleWidget::mouseReleaseEvent(QMouseEvent* e)
{m_bPressed = false;update();
}void CCircleWidget::enterEvent(QEvent*)
{m_bHovered = true;update();
}void CCircleWidget::leaveEvent(QEvent*)
{m_bHovered = false;update();
}
http://www.lryc.cn/news/419187.html

相关文章:

  • LeetCode 算法:有效的括号 c++
  • react和vue的diff算法的差别
  • 算法【滑动窗口】
  • 【RISC-V设计-06】- RISC-V处理器设计K0A之ALU
  • MyIP:强大且简单好用!
  • Redis作为缓存,如何与MySql的数据进行同步?
  • Android 通知栏推送功能
  • 【LVS】防火墙mark标记解决调度问题
  • 算法笔记|Day20回溯算法II
  • Oracle认证1Z0-071线上考试注意事项
  • 【C++ 面试 - 基础题】每日 3 题(八)
  • 影响LabVIEW工作效率的因素有哪些
  • linux 裸机.之SPV5210,dnw,usb,sdk,fastboot刷机(一)
  • 性能测试工具LoadRunner
  • 智能归来:深入探索人工智能回归模型的奥秘
  • swift 中,对象() 和 对象.init() 的共同点和异同点
  • Google安装JSON-handle扩展
  • 剖析算法内部结构----------贪心算法
  • uni-app开发微信小程序注意事项,不要用element-ui
  • Hibernate的检索策略(lazy、fetch、batch-size)
  • 算法训练(leetcode)第四十六天 | 110. 字符串接龙、105. 有向图的完全可达性、106. 岛屿的周长
  • 自定义Mybatis-Plus分布式ID生成器(解决ID长度超过JavaScript整数安全范围问题)
  • 2024剪辑神器盘点:四大热门剪辑软件推荐!
  • sql注入靶场sqli-labs常见sql注入漏洞详解
  • [C++] 模板进阶:特化与编译链接全解析
  • oracle-备份
  • oracle 并行parallel的插入insert用法
  • 夜莺监控使用指南
  • MySQLDM笔记-查询库中是否存在列出的表名及查询库中列出的不存在的表名
  • 第9天 xxl-job