赞
踩
- #include <QPoint>
- #include <QMouseEvent>
- #include <QTimer>
- class CDemo : public QDialog
- {
- Q_OBJECT
-
- public:
- CDemo(QWidget *parent = Q_NULLPTR);
- protected:
- void mouseMoveEvent(QMouseEvent *event);
- void mousePressEvent(QMouseEvent *event);
- void mouseReleaseEvent(QMouseEvent* event);//鼠标释放事件
-
- private:
- Ui::CDemoClass ui;
- private:
- bool m_isLeftPressed = false; //判断是否是左键点击
- QPoint m_clickedPositon; //获取鼠标左键按下时光标在全局(屏幕而非窗口)的位置
- int m_distance=20;
-
- };
-
- #include "CDemo.h"
- #include <QPushButton>
- #include <QDebug>
- CDemo::CDemo(QWidget *parent)
- : QDialog(parent)
- {
- ui.setupUi(this);
- this->setWindowFlags(Qt::FramelessWindowHint); //隐藏菜单栏
- setMouseTracking(true);//设置鼠标追踪
- }
-
- void CDemo::mouseMoveEvent(QMouseEvent *event)
- {
- Q_UNUSED(event);
- if (this->isFullScreen()) return;//全屏时不处理
- QPoint t_pos(this->geometry().width(), this->geometry().height());
- m_distance = QLineF(t_pos, event->pos()).length();
- if ( m_distance < 10){
- setCursor(Qt::SizeFDiagCursor);
- }else{
- setCursor(Qt::ArrowCursor);
- }
- if (m_isLeftPressed)//是否左击
- {
- QPoint t_pos = event->globalPos();//当前鼠标在桌面上的位置
- t_pos = t_pos - m_clickedPositon;//减去点击时的点,得到新的点(x,y)是右下角的移动距离
- QRect t_lastWidget = this->geometry();//窗口的几何位置
- t_lastWidget.setBottomRight(t_lastWidget.bottomRight() + t_pos);//改变窗口右下角的位置
- //设置最小大小
- if (t_lastWidget.size().width()<200 || t_lastWidget.size().height() < 200)
- {
- return;
- }
- this->setGeometry(t_lastWidget);//更新窗口的集合位置
- m_clickedPositon = event->globalPos();//更新位置
- }
- }
-
- void CDemo::mousePressEvent(QMouseEvent *event)
- {
- Q_UNUSED(event);
- if (event->button() == Qt::LeftButton && m_distance < 10)
- {
- this->m_isLeftPressed = true;
- QPoint t_pos = event->globalPos();
- m_clickedPositon = t_pos;//记录点击时的点的坐标
- }
-
- }
-
- void CDemo::mouseReleaseEvent(QMouseEvent* event)
- {
- Q_UNUSED(event);
- if (m_isLeftPressed)
- m_isLeftPressed = false;
- setCursor(Qt::ArrowCursor);
- }
-
-
-
-
重写鼠标移动事件就可以了,如果想实现其它位置的缩放,可以自己根据鼠标的位置来实现,思路都是一样的
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。