欢迎转载,请注明出处:https://blog.csdn.net/qq_39453936?spm=1010.2135.3001.5343
原文链接: https://blog.csdn.net/qq_39453936/article/details/121079718
效果图
实现思路
- 由于窗口需要同时监听响应移动和拉伸,所以为了区分具体操作,我们需要划出分移动和拉伸的区域位置。拉伸的区域宽度padding看情况而定,下图可看出,拉伸区域有4条边和4个顶角,移动区域在中间。鼠标在什么区域进行操作我们做对应的处理即可实现相应的功能。
计算区域
- 由于拉伸会使窗口大小发现变化需要重新计算区域,所以需要监听窗口Resize事件,拉伸区域描点计算:
//左侧描点区域 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);
光标变化
- 为了更好的交互体验,我们在鼠标划过对应的区域时显示相应的鼠标光标:
监听MouseMove事件,在相应区域内显示相应的光标。注意,需要setMouseTracking(true)才能再不按下鼠标的情况下监听鼠标的位置,
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监听需要此功能的窗口,从而实现窗口移动拉伸功能,加入使能控制使功能更加灵活,并且对只通过顶栏移动窗口特殊处理,添加窗口移动部件监听功能;
小白发文,欢迎指正