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

Qt实现窗口吸附屏幕边缘 自动收缩

先看效果:

 N年前的QQ就可以吸附到屏幕边缘,聊天时候非常方便,不用点击状态栏图标即可呼出QQ界面

自己尝试做了一个糙版的屏幕吸附效果。

关键代码:

void Widget::mouseMoveEvent(QMouseEvent *e)
{int dx = e->globalX() - lastPoint_.x();int dy = e->globalY() - lastPoint_.y();int targetx,targety;bool enableAnimation = false;if(mousePress_ == false) {return;}//! 垂直方向if(this->y() < EdgeAttachMargin && this->y() != 0) {//! 吸附顶部targety = 0;enableAnimation = true;} else {targety = this->y();}//! 水平方向if(this->x() < EdgeAttachMargin && this->x() != 0) {//! 吸附左边enableAnimation = 1;targetx = 0;} else {int rightx = this->x()+this->width();if(rightx > (QApplication::desktop()->width()-EdgeAttachMargin) && rightx != QApplication::desktop()->width()) {//! 吸附右边targetx = QApplication::desktop()->width()-this->width();enableAnimation = 1;} else {targetx = this->x();}}if(1 == enableAnimation){if(targetx == 0) {hideType_ = HideType::to_xleft;} else if(targetx == QApplication::desktop()->width()-this->width()) {hideType_ = HideType::to_xright;} else if(targety == 0) {hideType_ = HideType::to_y;}startAnimation(QPoint(targetx,targety),QPoint(this->x(),this->y()));} else {if(hideType_ == HideType::xleft) {int adjustX = x();int adjustY = y() + dy;if(dx > EdgeAttachMargin) {adjustX = x() + dx;hideType_ = HideType::none;lastPoint_.rx() = e->globalX();}this->move(adjustX, adjustY);lastPoint_.ry() = e->globalY();} else if (hideType_ == HideType::xright) {int adjustX = x();int adjustY = y() + dy;if(dx < -EdgeAttachMargin) {adjustX = x() + dx;hideType_ = HideType::none;lastPoint_.rx() = e->globalX();}this->move(adjustX, adjustY);lastPoint_.ry() = e->globalY();} else if(hideType_ == HideType::y) {int adjustX = x() + dx;int adjustY = y();if(dy > EdgeAttachMargin) {adjustY = y() + dy;hideType_ = HideType::none;lastPoint_.ry() = e->globalY();}this->move(adjustX, adjustY);lastPoint_.rx() = e->globalX();} else {int adjustX = x() + dx;int adjustY = y() + dy;if(adjustX < 0) adjustX = 0;if(adjustX > QApplication::desktop()->width() - width()) adjustX = QApplication::desktop()->width() - width();if(adjustY < 0) adjustY = 0;if(adjustY > QApplication::desktop()->availableGeometry().height() - height()) adjustY = QApplication::desktop()->availableGeometry().height() - height();this->move(adjustX, adjustY);lastPoint_   = e->globalPos();}}
}

鼠标悬停展开 / 离去收缩功能,

主要依据void enterEvent(QEvent *event); & void leaveEvent(QEvent *event);两个函数展开

动画效果使用QPropertyAnimation进行,可以参见另一篇博文中的例子:https://blog.csdn.net/wisdomroc/article/details/135975578


全套代码链接:Qt实现窗口吸附屏幕边缘 & 自动收缩 

http://www.lryc.cn/news/292141.html

相关文章:

  • shell脚本之免交互
  • Ajax入门与使用
  • 蓝桥杯备战——11.NE555测频
  • 代码随想录算法训练营第三十三天|509. 斐波那契数 ,● 70. 爬楼梯 , 746. 使用最小花费爬楼梯
  • Node.js 文件系统操作指南
  • Kotlin 协程1:深入理解withContext
  • (自用)learnOpenGL学习总结-高级OpenGL-几何着色器
  • 坚持刷题 | 完全二叉树的节点个数
  • K8S网络
  • 【蓝桥杯51单片机入门记录】LED
  • 轻松使用python将PDF转换为图片(成功)
  • 【目标检测】对DETR的简单理解
  • [工具探索]Safari 和 Google Chrome 浏览器内核差异
  • 文本生成高清、连贯视频,谷歌推出时空扩散模型
  • 时隔3年 | 微软 | Windows Server 2025 重磅发布
  • 有趣的css - 动态的毛玻璃背景
  • 桥接模式解析
  • MySQL数据库基础第一篇(SQL通用语法与分类)
  • 【Qt学习笔记】(一)初识Qt
  • YIA主题如何关闭新版本升级提示?WordPress主题怎么取消升级提醒?
  • 消息队列的应用场景
  • Arcgis10.3安装
  • 用Python和 Cryptography库给你的文件加密解密
  • element-ui button 仿写 demo
  • Maya------创建多边形工具
  • SQL分组统计条数时,不存在组类型,如何显示条数为0
  • 通过日期计算星期函数(C语言版)
  • 配置支持 OpenAPI 的 ASP.NET Core 应用
  • 前端自己整理的学习面试笔记
  • jQuery html的使用