Qt——鼠标拖动缩放窗口源码
- #ifndef MOVEWIDGET_H
- #define MOVEWIDGET_H
- #include <QWidget>
- #include <QEvent>
- class MoveWidget : public QWidget
- {
- Q_OBJECT
- public:
- MoveWidget(QWidget *parent);
- ~MoveWidget();
- void setResizable(bool bResiable); //设置是否可以缩放
- void setMargin(const int &); //设置鼠标距离界面边缘的范围
- protected:
- virtual bool eventFilter(QObject *, QEvent *);
- private:
- void changeMouseStyle(const QPoint &);
- private:
- bool m_bResizable; //界面是否可以缩放
- int m_iMarginWidth; //鼠标位置相对于界面边缘的距离
- bool m_bPressed; //鼠标是否按下
- QPoint m_ptPressPos;//鼠标按下时的位置
- };
- #endif // MOVEWIDGET_H
- #include "MoveWidget.h"
- #include <QHoverEvent>
- enum MouseStyle{ NORMAL, TOP, BOTTOM, LEFT, RIGHT, TOPLEFT, BOTTOMLEFT, BOTTOMRIGHT, TOPRIGHT } mouseStyle;
- MoveWidget::MoveWidget(QWidget *parent)
- : QWidget(parent)
- , m_bResizable(true)
- , m_iMarginWidth(6)
- , m_bPressed(false)
- , m_ptPressPos(0, 0)
- {
- this->setWindowFlags(Qt::FramelessWindowHint | Qt::Window);
- this->setAttribute(Qt::WA_Hover);
- this->installEventFilter(this);
- }
- MoveWidget::~MoveWidget()
- {
- }
- bool MoveWidget::eventFilter(QObject *, QEvent *event)
- {
- switch (event->type())
- {
- //[1]鼠标在界面上移动
- case QEvent::HoverMove:
- {
- QHoverEvent *hoverEvent = dynamic_cast<QHoverEvent *>(event);
- if (!m_bPressed)
- {
- changeMouseStyle(hoverEvent->pos());
- }
- else
- {
- if (mouseStyle == NORMAL)
- {
- this->move(this->mapToGlobal(hoverEvent->pos()) - m_ptPressPos);
- return true;
- }
- QPoint ptGlobalPos = this->mapToGlobal(hoverEvent->pos());
- QPoint ptTopLeft = this->frameGeometry().topLeft();
- QPoint ptBottomRight = this->frameGeometry().bottomRight();
- switch (mouseStyle)
- {
- case TOP:
- if (ptBottomRight.y() - ptGlobalPos.y() > this->minimumHeight())
- {
- ptTopLeft.setY(ptGlobalPos.y());
- }
- else
- {
- ptTopLeft.setY(ptBottomRight.y() - this->minimumHeight());
- }
- break;
- case BOTTOM:
- if (ptGlobalPos.y() - ptTopLeft.y() > this->minimumHeight())
- {
- ptBottomRight.setY(ptGlobalPos.y());
- }
- else
- {
- ptBottomRight.setY(ptTopLeft.y() + this->minimumHeight());
- }
- break;
- case LEFT:
- if (ptBottomRight.x() - ptGlobalPos.x() > this->minimumWidth())
- {
- ptTopLeft.setX(ptGlobalPos.x());
- }
- else
- {
- ptTopLeft.setX(ptBottomRight.x() - this->minimumWidth());
- }
- break;
- case RIGHT:
- if (ptGlobalPos.x() - ptTopLeft.x() > this->minimumWidth())
- {
- ptBottomRight.setX(ptGlobalPos.x());
- }
- else
- {
- ptBottomRight.setX(ptTopLeft.x() + this->minimumWidth());
- }
- break;
- case TOPLEFT:
- if (ptBottomRight.x() - ptGlobalPos.x() > this->minimumWidth())
- {
- ptTopLeft.setX(ptGlobalPos.x());
- }
- else
- {
- ptTopLeft.setX(ptBottomRight.x() - this->minimumWidth());
- }
- if (ptBottomRight.y() - ptGlobalPos.y() > this->minimumHeight())
- {
- ptTopLeft.setY(ptGlobalPos.y());
- }
- else
- {
- ptTopLeft.setY(ptBottomRight.y() - this->minimumHeight());
- }
- break;
- case BOTTOMLEFT:
- if (ptBottomRight.x() - ptGlobalPos.x() > this->minimumWidth())
- {
- ptTopLeft.setX(ptGlobalPos.x());
- }
- else
- {
- ptTopLeft.setX(ptBottomRight.x() - this->minimumWidth());
- }
- if (ptGlobalPos.y() - ptTopLeft.y() > this->minimumHeight())
- {
- ptBottomRight.setY(ptGlobalPos.y());
- }
- else
- {
- ptBottomRight.setY(ptTopLeft.y() + this->minimumHeight());
- }
- break;
- case BOTTOMRIGHT:
- if (ptGlobalPos.x() - ptTopLeft.x() > this->minimumWidth())
- {
- ptBottomRight.setX(ptGlobalPos.x());
- }
- else
- {
- ptBottomRight.setX(ptTopLeft.x() + this->minimumWidth());
- }
- if (ptGlobalPos.y() - ptTopLeft.y() > this->minimumHeight())
- {
- ptBottomRight.setY(ptGlobalPos.y());
- }
- else
- {
- ptBottomRight.setY(ptTopLeft.y() + this->minimumHeight());
- }
- break;
- case TOPRIGHT:
- if (ptBottomRight.y() - ptGlobalPos.y() > this->minimumHeight())
- {
- ptTopLeft.setY(ptGlobalPos.y());
- }
- else
- {
- ptTopLeft.setY(ptBottomRight.y() - this->minimumHeight());
- }
- if (ptGlobalPos.x() - ptTopLeft.x() > this->minimumWidth())
- {
- ptBottomRight.setX(ptGlobalPos.x());
- }
- else
- {
- ptBottomRight.setX(ptTopLeft.x() + this->minimumWidth());
- }
- break;
- default:
- break;
- }
- this->setGeometry(QRect(ptTopLeft, ptBottomRight));
- }
- return true;
- }
- break;
- //[1]end 鼠标在界面上移动
- //[2]鼠标按下
- case QEvent::MouseButtonPress:
- {
- m_bPressed = true;
- QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent *>(event);
- m_ptPressPos = mouseEvent->globalPos() - this->frameGeometry().topLeft();
- }
- break;
- //[2]end 鼠标按下
- //[3]鼠标松开
- case QEvent::MouseButtonRelease:
- {
- m_bPressed = false;
- QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent *>(event);
- changeMouseStyle(mouseEvent->pos());
- }
- break;
- //[3]end 鼠标松开
- default:
- break;
- }
- return false;
- }
- void MoveWidget::changeMouseStyle(const QPoint &ptMousePos)
- {
- if (!m_bResizable)
- {
- setCursor(Qt::ArrowCursor);//正常样式
- mouseStyle = NORMAL;
- return;
- }
- int iPosX = ptMousePos.x();
- int iPosY = ptMousePos.y();
- int iWidth = this->width();
- int iHeight = this->height();
- if (iPosX >= iWidth - m_iMarginWidth && iPosX <= iWidth)
- {
- setCursor(Qt::SizeHorCursor);//右
- if (iPosY >= 0 && iPosY <= m_iMarginWidth)
- {
- setCursor(Qt::SizeBDiagCursor);//右上
- mouseStyle = TOPRIGHT;
- return;
- }
- if (iPosY >= iHeight - m_iMarginWidth && iPosY <= iHeight)
- {
- setCursor(Qt::SizeFDiagCursor);//右下
- mouseStyle = BOTTOMRIGHT;
- return;
- }
- mouseStyle = RIGHT;
- return;
- }
- if (iPosX >= 0 && iPosX <= m_iMarginWidth)
- {
- setCursor(Qt::SizeHorCursor);//左
- if (iPosY >= 0 && iPosY <= m_iMarginWidth)
- {
- setCursor(Qt::SizeFDiagCursor);//左上
- mouseStyle = TOPLEFT;
- return;
- }
- if (iPosY >= iHeight - m_iMarginWidth && iPosY <= iHeight)
- {
- setCursor(Qt::SizeBDiagCursor);//左下
- mouseStyle = BOTTOMLEFT;
- return;
- }
- mouseStyle = LEFT;
- return;
- }
- if (iPosY >= 0 && iPosY <= m_iMarginWidth)
- {
- setCursor(Qt::SizeVerCursor);//上
- mouseStyle = TOP;
- return;
- }
- if (iPosY >= iHeight - m_iMarginWidth && iPosY <= iHeight)
- {
- setCursor(Qt::SizeVerCursor);//下
- mouseStyle = BOTTOM;
- return;
- }
- setCursor(Qt::ArrowCursor);//正常样式
- mouseStyle = NORMAL;
- return;
- }
- void MoveWidget::setResizable(bool bResiable)
- {
- m_bResizable = bResiable;
- }
- void MoveWidget::setMargin(const int &iWidth)
- {
- m_iMarginWidth = iWidth;
- }
Qt——鼠标拖动缩放窗口源码的更多相关文章
- qt 鼠标拖动窗口放大缩小
// 鼠标拖动 具体实现void mouseMoveEvent(QMouseEvent * pEvent) { if (pEvent->buttons() & Qt::LeftButto ...
- Bootstrap 模态窗口源码分析
前言: bootstrap的 js插件的源码写的非常好,也算是编写jquery插件的模范写法,本来还想大篇详细的分析一下呢,唉,没时间啊,很早之前看过的源码了,现在贴在了博客上, 300来行的代码,其 ...
- AndroidTv Home界面实现原理(二)——Leanback 库的主页卡位缩放动画源码解析
先看个效果图: 上一篇中,我们留了问题,在 Tv Home 界面这种很常见聚焦卡位放大动画效果,我们这一篇就来看看 Leanback 库是怎么实现的. 如果要我们自己实现的话,思路应该不难,就是写个放 ...
- Qt creator中文输入—fctix-qt5 源码编译 libfcitxplatforminputcontextplugin.so
fctix-qt5 的源码有两个地方可以下载: wget https://download.fcitx-im.org/fcitx-qt5/fcitx-qt5-1.0.5.tar.xztar -xJf ...
- iOS电商常见动画与布局、微信悬浮窗、音乐播放器、歌词解析、拖动视图等源码
iOS精选源码 MXScroll 介绍 混合使用UIScrollView ios 电商demo(实现各种常见动画效果和页面布局) 一行代码集成微信悬浮窗 可拖动,大小的视图,可放置在屏幕边缘. 在使用 ...
- QT:轻松获取网页源码
获取网页源码的小例子,代码很简单,就不多作解释了. 不过一定要注意网页的编码问题,否则会出现乱码的!!! #include <QtCore> #include <QtNetwork& ...
- 40.qt quick- 高仿微信实现局域网聊天V4版本(支持gif动图表情包、消息聊天、拖动缩放窗口)
在上章37.qt quick- 高仿微信实现局域网聊天V3版本(添加登录界面.UDP校验登录.皮肤更换.3D旋转),我们已经实现了: 添加登录界面. UDP校验登录. 皮肤更换. 3D旋转(主界面和登 ...
- qt 鼠标拖动窗口 跳动 解决
因为获取当前的位置,似乎没有把标题栏的高度记进去. 所以移动前,得考虑到标题栏的高度. 用以下方式获取标题栏高度: QApplication::style()->pixelMetric(QSty ...
- Qt——鼠标拖动调整窗口大小
要求:鼠标移到界面边角时,鼠标样式相应地发生改变. 实现方法一: 重写mouseMoveEvent,如果鼠标没有按下,则根据鼠标在界面上的位置设置鼠标样式,如果鼠标按下,则根据位置判断该怎样调整界面大 ...
随机推荐
- Codeforces Round #587 (Div. 3) C题 【判断两个矩形是否完全覆盖一个矩形问题】 {补题 [差点上分系列]}
C. White Sheet There is a white sheet of paper lying on a rectangle table. The sheet is a rectangle ...
- pyecharts v1 版本 学习笔记 折线图,面积图
折线图 折线图 基本demo import pyecharts.options as opts from pyecharts.charts import Line c = ( Line() .add_ ...
- Java中堆内存和栈内存有什么区别
本文链接:https://blog.csdn.net/wangbo1998/article/details/80379016Java把内存分成两种,一种叫做栈内存,一种叫做堆内存 在函数中定义的一些基 ...
- 五十五.ansible概述、ansible基础 、ad-hoc、批量配置管理
1.环境准备 (自动化工具,批量操作) 6台 2cpu,1.5G以上内存,20G硬盘,1网卡 1.1 基础环境准备 1)启动6台虚拟机,ansible.sh 2)真机配置yum仓库 ]# tar ...
- learning express step(十四)
learning express error handle code: const express = require('express'); const app = express(); const ...
- [DK] 化学竞赛的大奖
https://www.luogu.org/problemnew/show/T16502 无向图 缩点 树的直径 到直径两个端点的距离的较大值 #include <iostream> ...
- ECMAScript 提案阶段
stage0 strawman任何讨论.想法.改变或者还没加到提案的特性都在这个阶段.只有TC39成员可以提交. stage1 proposal (1)产出一个正式的提案. (2)发现潜在的问题,例如 ...
- mysql建表问题
PUBLIC Stack Overflow Tags Users Jobs TeamsQ&A for workLearn More MySQL error: The maximum col ...
- 【做题记录】AtCoder AGC做题记录
做一下AtCoder的AGC锻炼一下思维吧 目前已做题数: 75 总共题数: 239 每一场比赛后面的字母是做完的题,括号里是写完题解的题 AGC001: ABCDEF (DEF) AGC002: A ...
- 取出easyui的datagrid的总数
var grid = $('#datagrid'); var options = grid.datagrid('getPager').data("pagination").opti ...