赞
踩
欢迎转载,请注明出处:https://blog.csdn.net/qq_39453936?spm=1010.2135.3001.5343
原文链接: https://blog.csdn.net/qq_39453936/article/details/121079718
//左侧描点区域 rectLeft = QRect(0, padding, padding, height - padding * 2); //上侧描点区域 rectTop = QRect(padding, 0, width - padding * 2, padding); //右侧描点区域 rectRight = QRect(width - padding, padding, padding, height - padding * 2); //下侧描点区域 rectBottom = QRect(padding, height - padding, width - padding * 2, padding); //左上角描点区域 rectLeftTop = QRect(0, 0, padding, padding); //右上角描点区域 rectRightTop = QRect(width - padding, 0, padding, padding); //左下角描点区域 rectLeftBottom = QRect(0, height - padding, padding, padding); //右下角描点区域 rectRightBottom = QRect(width - padding, height - padding, padding, padding);
if (rectLeft.contains(point)) { widget->setCursor(Qt::SizeHorCursor); } else if (rectRight.contains(point)) { widget->setCursor(Qt::SizeHorCursor); } else if (rectTop.contains(point)) { widget->setCursor(Qt::SizeVerCursor); } else if (rectBottom.contains(point)) { widget->setCursor(Qt::SizeVerCursor); } else if (rectLeftTop.contains(point)) { widget->setCursor(Qt::SizeFDiagCursor); } else if (rectRightTop.contains(point)) { widget->setCursor(Qt::SizeBDiagCursor); } else if (rectLeftBottom.contains(point)) { widget->setCursor(Qt::SizeBDiagCursor); } else if (rectRightBottom.contains(point)) { widget->setCursor(Qt::SizeFDiagCursor); } else { widget->setCursor(Qt::ArrowCursor); }
拉伸操作是按下鼠标移动窗口跟随变化。需要监听鼠标按下事件MouseButtonPress以及鼠标移动事件MouseMove。因为窗口移动操作也需要监听鼠标按下事件和鼠标移动事件,这里需要特殊处理下,在鼠标按下时记录当前的操作区域,在收到移动事件时判断是何种操作。
mousePressEvent:
//记住当前控件坐标和宽高以及鼠标按下的坐标 rectX = widget->x(); rectY = widget->y(); rectW = widget->width(); rectH = widget->height(); lastPos = mouseEvent->pos(); //判断按下的鼠标区域位置 if (rectLeft.contains(lastPos)) { pressedLeft = true; } else if (rectRight.contains(lastPos)) { pressedRight = true; } else if (rectTop.contains(lastPos)) { pressedTop = true; } else if (rectBottom.contains(lastPos)) { pressedBottom = true; } else if (rectLeftTop.contains(lastPos)) { pressedLeftTop = true; } else if (rectRightTop.contains(lastPos)) { pressedRightTop = true; } else if (rectLeftBottom.contains(lastPos)) { pressedLeftBottom = true; } else if (rectRightBottom.contains(lastPos)) { pressedRightBottom = true; } else { pressed = true; }
mouseMoveEvent:
QPoint point= mouseEvent->pos(); //根据当前鼠标位置,计算XY轴移动了多少 int offsetX = point.x() - lastPos.x(); int offsetY = point.y() - lastPos.y(); if (pressedLeft) { int resizeW = widget->width() - offsetX; if (widget->minimumWidth() <= resizeW) { widget->setGeometry(widget->x() + offsetX, rectY, resizeW, rectH); } } else if (pressedRight) { widget->setGeometry(rectX, rectY, rectW + offsetX, rectH); } else if (pressedTop) { int resizeH = widget->height() - offsetY; if (widget->minimumHeight() <= resizeH) { widget->setGeometry(rectX, widget->y() + offsetY, rectW, resizeH); } } else if (pressedBottom) { widget->setGeometry(rectX, rectY, rectW, rectH + offsetY); } else if (pressedLeftTop) { int resizeW = widget->width() - offsetX; int resizeH = widget->height() - offsetY; if (widget->minimumWidth() <= resizeW) { widget->setGeometry(widget->x() + offsetX, widget->y(), resizeW, resizeH); } if (widget->minimumHeight() <= resizeH) { widget->setGeometry(widget->x(), widget->y() + offsetY, resizeW, resizeH); } } else if (pressedRightTop) { int resizeW = rectW + offsetX; int resizeH = widget->height() - offsetY; if (widget->minimumHeight() <= resizeH) { widget->setGeometry(widget->x(), widget->y() + offsetY, resizeW, resizeH); } } else if (pressedLeftBottom) { int resizeW = widget->width() - offsetX; int resizeH = rectH + offsetY; if (widget->minimumWidth() <= resizeW) { widget->setGeometry(widget->x() + offsetX, widget->y(), resizeW, resizeH); } if (widget->minimumHeight() <= resizeH) { widget->setGeometry(widget->x(), widget->y(), resizeW, resizeH); } } else if (pressedRightBottom) { int resizeW = rectW + offsetX; int resizeH = rectH + offsetY; widget->setGeometry(widget->x(), widget->y(), resizeW, resizeH); } else if (pressed) { widget->move(widget->x() + offsetX, widget->y() + offsetY); }
mouseReleaseEvent:
//恢复所有状态
pressedLeft = false;
pressedRight = false;
pressedTop = false;
pressedBottom = false;
pressedLeftTop = false;
pressedRightTop = false;
pressedLeftBottom = false;
pressedRightBottom = false;
pressed = false;
为了使用这些功能更加的方便通用,可以通过installEventFilter监听需要此功能的窗口,从而实现窗口移动拉伸功能,加入使能控制使功能更加灵活,并且对只通过顶栏移动窗口特殊处理,添加窗口移动部件监听功能;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。