// 鼠标拖动 具体实现
void mouseMoveEvent(QMouseEvent * pEvent)
{
if (pEvent->buttons() & Qt::LeftButton)
{
if (resizeDir == nodir)
{//移动窗口
QPoint newPos = pEvent->globalPos(); move(newPos - m_PressedPos); emit signal_drawWidgetPositionChange(this->pos().x(), this->pos().y());
}
else
{
//ptop,pbottom,pleft,pright; //窗口上下左右的值
int ptop = frameGeometry().top();
int pbottom = frameGeometry().bottom();
int pleft = frameGeometry().left();
int pright = frameGeometry().right();
if(resizeDir & top)
{
//检测更改尺寸方向中包含的上下左右分量
if(height() == minimumHeight())
{
ptop = min(pEvent->globalY(),ptop);
}
else if(height() == maximumHeight())
{
ptop = max(pEvent->globalY(),ptop);
}
else
{
ptop = pEvent->globalY();
}
}
else if(resizeDir & bottom)
{
if(height() == minimumHeight())
{
pbottom = max(pEvent->globalY(),ptop);
}
else if(height() == maximumHeight())
{
pbottom = min(pEvent->globalY(),ptop);
}
else
{
pbottom = pEvent->globalY();
}
} if(resizeDir & left)
{ //检测左右分量
if(width() == minimumWidth())
{
pleft = min(pEvent->globalX(),pleft);
}
else if(width() == maximumWidth())
{
pleft = max(pEvent->globalX(),pleft);
}
else
{
pleft = pEvent->globalX();
}
}
else if(resizeDir & right)
{
if(width() == minimumWidth())
{
pright = max(pEvent->globalX(),pright);
}
else if(width() == maximumWidth())
{
pright = min(pEvent->globalX(),pright);
}
else
{
pright = pEvent->globalX();
}
}
//setGeometry(QRect(QPoint(pleft,ptop),QPoint(pright, pbottom))); // 限制大小 if (m_iMaxWindowWidth <= pright - pleft)
{
if (m_iMaxWindowHeight <= pbottom - ptop)
{
setGeometry(QRect(QPoint(pleft,ptop),QPoint(pleft + m_iMaxWindowWidth, ptop + m_iMaxWindowHeight)));
}else
{
setGeometry(QRect(QPoint(pleft,ptop),QPoint(pleft + m_iMaxWindowWidth, pbottom)));
}
}else
{
if (m_iMaxWindowHeight <= pbottom - ptop)
{
setGeometry(QRect(QPoint(pleft,ptop),QPoint(pright, ptop + m_iMaxWindowHeight)));
}else
{
setGeometry(QRect(QPoint(pleft,ptop),QPoint(pright, pbottom)));
}
}
}
}
else
{
findWidgetEdge();
}//当不拖动窗口、不改变窗口大小尺寸的时候 检测鼠标边缘
}

// 计算窗口边缘

void findWidgetEdge()
{
int diffLeft = abs(cursor().pos().x() - frameGeometry().left()); //计算鼠标距离窗口上下左右有多少距离
int diffRight = abs(cursor().pos().x() - frameGeometry().right());
int diffTop = abs(cursor().pos().y() - frameGeometry().top());
int diffBottom = abs(cursor().pos().y() - frameGeometry().bottom()); QCursor tempCursor; //获得当前鼠标样式
tempCursor = cursor(); if(diffTop < m_iEdgeMargin)
{
//根据 边缘距离 分类改变尺寸的方向
if(diffLeft < m_iEdgeMargin)
{
resizeDir = topLeft;
tempCursor.setShape(Qt::SizeFDiagCursor);
}
else if(diffRight < m_iEdgeMargin)
{
resizeDir = topRight;
tempCursor.setShape(Qt::SizeBDiagCursor);
}
else
{
resizeDir = top;
tempCursor.setShape(Qt::SizeVerCursor);
}
}
else if(diffBottom < m_iEdgeMargin)
{
if(diffLeft < m_iEdgeMargin)
{
resizeDir = bottomLeft;
tempCursor.setShape(Qt::SizeBDiagCursor);
}
else if(diffRight < m_iEdgeMargin)
{
resizeDir = bottomRight;
tempCursor.setShape(Qt::SizeFDiagCursor);
}
else
{
resizeDir = bottom;
tempCursor.setShape(Qt::SizeVerCursor);
}
}
else if(diffLeft < m_iEdgeMargin)
{
resizeDir = left;
tempCursor.setShape(Qt::SizeHorCursor);
}
else if(diffRight < m_iEdgeMargin)
{
resizeDir = right;
tempCursor.setShape(Qt::SizeHorCursor);
}
else
{
resizeDir = nodir;
tempCursor.setShape(Qt::ArrowCursor);
} setCursor(tempCursor); //重新设置鼠标,主要是改样式
}
void mouseReleaseEvent(QMouseEvent *)
{
if(resizeDir != nodir)
{ //还原鼠标样式
findWidgetEdge();
}
// this->setCursor(Qt::ArrowCursor);
}
void mousePressEvent(QMouseEvent * pEvent)
{
if (Qt::LeftButton == pEvent->button())
{
this->setCursor(Qt::PointingHandCursor);
m_PressedPos = pEvent->globalPos() - this->pos();
m_dragPosition = pEvent->globalPos() - frameGeometry().topLeft(); //获得鼠标按键位置相对窗口左上面的位置
}
}

qt 鼠标拖动窗口放大缩小的更多相关文章

  1. qt 鼠标拖动窗口 跳动 解决

    因为获取当前的位置,似乎没有把标题栏的高度记进去. 所以移动前,得考虑到标题栏的高度. 用以下方式获取标题栏高度: QApplication::style()->pixelMetric(QSty ...

  2. Winform 图片鼠标滚动查看(放大,缩小,旋转,拖动查看)[日常随笔]

    方法千千万,我只是其中一笔[通过控制PictureBox来控制图片,图片完全施展在控件中]...几久不做,还真有点陌生! 窗体构造中添加鼠标滚动: /// <summary> /// 窗体 ...

  3. 鼠标滚轮图片放大缩小功能,使用layer弹框后不起作用

    今天在项目中遇到的一个问题:点击按钮使用layer弹框弹出一张图片,需要加一个鼠标滚轮放大缩小,图片也跟着放大缩小的功能.于是在网上找了一个demo. DEMO: <!DOCTYPE html ...

  4. Qt——鼠标拖动缩放窗口源码

    #ifndef MOVEWIDGET_H #define MOVEWIDGET_H #include <QWidget> #include <QEvent> class Mov ...

  5. Qt——鼠标拖动调整窗口大小

    要求:鼠标移到界面边角时,鼠标样式相应地发生改变. 实现方法一: 重写mouseMoveEvent,如果鼠标没有按下,则根据鼠标在界面上的位置设置鼠标样式,如果鼠标按下,则根据位置判断该怎样调整界面大 ...

  6. [Unity3D]Unity3D游戏开发之鼠标滚轮实现放大缩小

    今天为大家分享的是在Rpg游戏中十分常见的鼠标滚轮调整摄像机视野效果.首先我们先创建一个游戏场景: 接下来我们编写一段脚本代码: [csharp] view plaincopyprint" ...

  7. dephi中单击鼠标拖动窗口(使用WM_SYSCOMMAND)

    procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Int ...

  8. mxGraph画图区域使用鼠标滚轮实现放大/缩小

    // 重写鼠标滚轮事件 mxEvent.addMouseWheelListener = function (funct) { } // 添加初次载入事件 window.onload = functio ...

  9. [js/jquery]移动端手势拖动,放大,缩小预览图片

    摘要 有这样的需求需要在手机端预览图片的时候,实现图片的手势拖动,放大缩小功能.最终通过touch.js这个插件实现了效果. touch.js Touch.js是移动设备上的手势识别与事件库, 由百度 ...

随机推荐

  1. conda、pip换源以及conda、pip命令比较

    conda换源: conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda ...

  2. light oj 1071 dp(吃金币升级版)

    #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> ...

  3. dubbo入门学习(五)-----dubbo的高可用

    zookeeper宕机与dubbo直连 现象 zookeeper注册中心宕机,还可以消费dubbo暴露的服务. 原因 健壮性 l 监控中心宕掉不影响使用,只是丢失部分采样数据 l 数据库宕掉后,注册中 ...

  4. 仓库盘点功能-ThinkPHP_学习随笔

    public function check() { $db = M('Bookinfo'); $region = I('post.region'); $c = $db -> count(); f ...

  5. CentOS设置打开终端快捷键

  6. PAT甲级——A1059 Prime Factors

    Given any positive integer N, you are supposed to find all of its prime factors, and write them in t ...

  7. WPF 自适应布局控件

    public class KDLayoutGroup : Grid { public double LabelWidth { get; set; } public double GetLabelWid ...

  8. a标签实现 批量下载

    // 批量下载 download (name, href) { var a = document.createElement('a') var e = document.createEvent('Mo ...

  9. springcloud注册中心eureka

    1.前提 springcloud的注册中心是以springboot为基础搭建起来的. 开发工具:IDEA 项目管理工具:maven 2.搭建步骤 创建一个web项目(建议使用IDEA工具构建项目) 修 ...

  10. TZ_01MyBatis_jdbcConfig.properties

    jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT jd ...