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

先看效果:

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

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

关键代码:

cpp 复制代码
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实现窗口吸附屏幕边缘 & 自动收缩

相关推荐
委婉待续10 小时前
Qt的学习(一)
开发语言·qt·学习
笨笨马甲10 小时前
Qt Quick Layout功能及架构
开发语言·qt
feiyangqingyun11 小时前
Qt/C++开发监控GB28181系统/取流协议/同时支持udp/tcp被动/tcp主动
c++·qt·udp·gb28181
jllws112 小时前
Qt学习及使用_第1部分_认识Qt---学习目的及技术准备
qt·c++框架
到点就困告12 小时前
海康工业相机SDK二次开发(VS+QT+海康SDK+C++)
数码相机·qt·海康
唐墨1231 天前
android与Qt类比
android·开发语言·qt
道剑剑非道1 天前
QT开发技术【ffmpeg + QAudioOutput】音乐播放器 完善
开发语言·qt·ffmpeg
不惑_1 天前
用 PyQt5 打造一个可视化 JSON 数据解析工具
开发语言·qt·json
誰能久伴不乏1 天前
Qt 开发中的父类与父对象的区别和父对象传递:如何选择 `QWidget` 或 `QObject`?
java·开发语言·qt
誰能久伴不乏1 天前
理解继承与组合的本质:Qt 项目中的设计选择指南
开发语言·qt