更新代码
This commit is contained in:
@@ -9,15 +9,14 @@ FramelessWidget::FramelessWidget(QObject *parent) : QObject(parent)
|
||||
resizeEnable = true;
|
||||
widget = 0;
|
||||
|
||||
pressed = false;
|
||||
pressedLeft = false;
|
||||
pressedRight = false;
|
||||
pressedTop = false;
|
||||
pressedBottom = false;
|
||||
pressedLeftTop = false;
|
||||
pressedRightTop = false;
|
||||
pressedLeftBottom = false;
|
||||
pressedRightBottom = false;
|
||||
mousePressed = false;
|
||||
mousePoint = QPoint(0, 0);
|
||||
mouseRect = QRect(0, 0, 0, 0);
|
||||
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
pressedArea << false;
|
||||
pressedRect << QRect(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
//如果父类是窗体则直接设置
|
||||
if (parent->isWidgetType()) {
|
||||
@@ -28,48 +27,62 @@ FramelessWidget::FramelessWidget(QObject *parent) : QObject(parent)
|
||||
bool FramelessWidget::eventFilter(QObject *watched, QEvent *event)
|
||||
{
|
||||
if (widget != 0 && watched == widget) {
|
||||
if (event->type() == QEvent::Resize) {
|
||||
if (event->type() == QEvent::WindowStateChange) {
|
||||
//解决mac系统上无边框最小化失效的BUG
|
||||
#ifdef Q_OS_MACOS
|
||||
if (widget->windowState() & Qt::WindowMinimized) {
|
||||
isMin = true;
|
||||
} else {
|
||||
if (isMin) {
|
||||
//设置无边框属性
|
||||
widget->setWindowFlags(flags | Qt::FramelessWindowHint);
|
||||
widget->setVisible(true);
|
||||
isMin = false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
} else if (event->type() == QEvent::Resize) {
|
||||
//重新计算八个描点的区域,描点区域的作用还有就是计算鼠标坐标是否在某一个区域内
|
||||
int width = widget->width();
|
||||
int height = widget->height();
|
||||
|
||||
//左侧描点区域
|
||||
rectLeft = QRect(0, padding, padding, height - padding * 2);
|
||||
//上侧描点区域
|
||||
rectTop = QRect(padding, 0, width - padding * 2, padding);
|
||||
pressedRect[0] = QRect(0, padding, padding, height - padding * 2);
|
||||
//右侧描点区域
|
||||
rectRight = QRect(width - padding, padding, padding, height - padding * 2);
|
||||
pressedRect[1] = QRect(width - padding, padding, padding, height - padding * 2);
|
||||
//上侧描点区域
|
||||
pressedRect[2] = QRect(padding, 0, width - padding * 2, padding);
|
||||
//下侧描点区域
|
||||
rectBottom = QRect(padding, height - padding, width - padding * 2, padding);
|
||||
pressedRect[3] = QRect(padding, height - padding, width - padding * 2, padding);
|
||||
|
||||
//左上角描点区域
|
||||
rectLeftTop = QRect(0, 0, padding, padding);
|
||||
pressedRect[4] = QRect(0, 0, padding, padding);
|
||||
//右上角描点区域
|
||||
rectRightTop = QRect(width - padding, 0, padding, padding);
|
||||
pressedRect[5] = QRect(width - padding, 0, padding, padding);
|
||||
//左下角描点区域
|
||||
rectLeftBottom = QRect(0, height - padding, padding, padding);
|
||||
pressedRect[6] = QRect(0, height - padding, padding, padding);
|
||||
//右下角描点区域
|
||||
rectRightBottom = QRect(width - padding, height - padding, padding, padding);
|
||||
pressedRect[7] = QRect(width - padding, height - padding, padding, padding);
|
||||
} else if (event->type() == QEvent::HoverMove) {
|
||||
//设置对应鼠标形状,这个必须放在这里而不是下面,因为可以在鼠标没有按下的时候识别
|
||||
QHoverEvent *hoverEvent = (QHoverEvent *)event;
|
||||
QPoint point = hoverEvent->pos();
|
||||
if (resizeEnable) {
|
||||
if (rectLeft.contains(point)) {
|
||||
if (pressedRect.at(0).contains(point)) {
|
||||
widget->setCursor(Qt::SizeHorCursor);
|
||||
} else if (rectRight.contains(point)) {
|
||||
} else if (pressedRect.at(1).contains(point)) {
|
||||
widget->setCursor(Qt::SizeHorCursor);
|
||||
} else if (rectTop.contains(point)) {
|
||||
} else if (pressedRect.at(2).contains(point)) {
|
||||
widget->setCursor(Qt::SizeVerCursor);
|
||||
} else if (rectBottom.contains(point)) {
|
||||
} else if (pressedRect.at(3).contains(point)) {
|
||||
widget->setCursor(Qt::SizeVerCursor);
|
||||
} else if (rectLeftTop.contains(point)) {
|
||||
} else if (pressedRect.at(4).contains(point)) {
|
||||
widget->setCursor(Qt::SizeFDiagCursor);
|
||||
} else if (rectRightTop.contains(point)) {
|
||||
} else if (pressedRect.at(5).contains(point)) {
|
||||
widget->setCursor(Qt::SizeBDiagCursor);
|
||||
} else if (rectLeftBottom.contains(point)) {
|
||||
} else if (pressedRect.at(6).contains(point)) {
|
||||
widget->setCursor(Qt::SizeBDiagCursor);
|
||||
} else if (rectRightBottom.contains(point)) {
|
||||
} else if (pressedRect.at(7).contains(point)) {
|
||||
widget->setCursor(Qt::SizeFDiagCursor);
|
||||
} else {
|
||||
widget->setCursor(Qt::ArrowCursor);
|
||||
@@ -77,32 +90,35 @@ bool FramelessWidget::eventFilter(QObject *watched, QEvent *event)
|
||||
}
|
||||
|
||||
//根据当前鼠标位置,计算XY轴移动了多少
|
||||
int offsetX = point.x() - lastPos.x();
|
||||
int offsetY = point.y() - lastPos.y();
|
||||
int offsetX = point.x() - mousePoint.x();
|
||||
int offsetY = point.y() - mousePoint.y();
|
||||
|
||||
//根据按下处的位置判断是否是移动控件还是拉伸控件
|
||||
if (moveEnable) {
|
||||
if (pressed) {
|
||||
widget->move(widget->x() + offsetX, widget->y() + offsetY);
|
||||
}
|
||||
if (moveEnable && mousePressed) {
|
||||
widget->move(widget->x() + offsetX, widget->y() + offsetY);
|
||||
}
|
||||
|
||||
if (resizeEnable) {
|
||||
if (pressedLeft) {
|
||||
int rectX = mouseRect.x();
|
||||
int rectY = mouseRect.y();
|
||||
int rectW = mouseRect.width();
|
||||
int rectH = mouseRect.height();
|
||||
|
||||
if (pressedArea.at(0)) {
|
||||
int resizeW = widget->width() - offsetX;
|
||||
if (widget->minimumWidth() <= resizeW) {
|
||||
widget->setGeometry(widget->x() + offsetX, rectY, resizeW, rectH);
|
||||
}
|
||||
} else if (pressedRight) {
|
||||
} else if (pressedArea.at(1)) {
|
||||
widget->setGeometry(rectX, rectY, rectW + offsetX, rectH);
|
||||
} else if (pressedTop) {
|
||||
} else if (pressedArea.at(2)) {
|
||||
int resizeH = widget->height() - offsetY;
|
||||
if (widget->minimumHeight() <= resizeH) {
|
||||
widget->setGeometry(rectX, widget->y() + offsetY, rectW, resizeH);
|
||||
}
|
||||
} else if (pressedBottom) {
|
||||
} else if (pressedArea.at(3)) {
|
||||
widget->setGeometry(rectX, rectY, rectW, rectH + offsetY);
|
||||
} else if (pressedLeftTop) {
|
||||
} else if (pressedArea.at(4)) {
|
||||
int resizeW = widget->width() - offsetX;
|
||||
int resizeH = widget->height() - offsetY;
|
||||
if (widget->minimumWidth() <= resizeW) {
|
||||
@@ -111,13 +127,13 @@ bool FramelessWidget::eventFilter(QObject *watched, QEvent *event)
|
||||
if (widget->minimumHeight() <= resizeH) {
|
||||
widget->setGeometry(widget->x(), widget->y() + offsetY, resizeW, resizeH);
|
||||
}
|
||||
} else if (pressedRightTop) {
|
||||
} else if (pressedArea.at(5)) {
|
||||
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) {
|
||||
} else if (pressedArea.at(6)) {
|
||||
int resizeW = widget->width() - offsetX;
|
||||
int resizeH = rectH + offsetY;
|
||||
if (widget->minimumWidth() <= resizeW) {
|
||||
@@ -126,55 +142,47 @@ bool FramelessWidget::eventFilter(QObject *watched, QEvent *event)
|
||||
if (widget->minimumHeight() <= resizeH) {
|
||||
widget->setGeometry(widget->x(), widget->y(), resizeW, resizeH);
|
||||
}
|
||||
} else if (pressedRightBottom) {
|
||||
} else if (pressedArea.at(7)) {
|
||||
int resizeW = rectW + offsetX;
|
||||
int resizeH = rectH + offsetY;
|
||||
widget->setGeometry(widget->x(), widget->y(), resizeW, resizeH);
|
||||
}
|
||||
}
|
||||
} else if (event->type() == QEvent::MouseButtonPress) {
|
||||
//记住当前控件坐标和宽高以及鼠标按下的坐标
|
||||
//记住鼠标按下的坐标+窗体区域
|
||||
QMouseEvent *mouseEvent = (QMouseEvent *)event;
|
||||
rectX = widget->x();
|
||||
rectY = widget->y();
|
||||
rectW = widget->width();
|
||||
rectH = widget->height();
|
||||
lastPos = mouseEvent->pos();
|
||||
mousePoint = mouseEvent->pos();
|
||||
mouseRect = widget->geometry();
|
||||
|
||||
//判断按下的手柄的区域位置
|
||||
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;
|
||||
if (pressedRect.at(0).contains(mousePoint)) {
|
||||
pressedArea[0] = true;
|
||||
} else if (pressedRect.at(1).contains(mousePoint)) {
|
||||
pressedArea[1] = true;
|
||||
} else if (pressedRect.at(2).contains(mousePoint)) {
|
||||
pressedArea[2] = true;
|
||||
} else if (pressedRect.at(3).contains(mousePoint)) {
|
||||
pressedArea[3] = true;
|
||||
} else if (pressedRect.at(4).contains(mousePoint)) {
|
||||
pressedArea[4] = true;
|
||||
} else if (pressedRect.at(5).contains(mousePoint)) {
|
||||
pressedArea[5] = true;
|
||||
} else if (pressedRect.at(6).contains(mousePoint)) {
|
||||
pressedArea[6] = true;
|
||||
} else if (pressedRect.at(7).contains(mousePoint)) {
|
||||
pressedArea[7] = true;
|
||||
} else {
|
||||
pressed = true;
|
||||
mousePressed = true;
|
||||
}
|
||||
} else if (event->type() == QEvent::MouseMove) {
|
||||
//改成用HoverMove识别
|
||||
} else if (event->type() == QEvent::MouseButtonRelease) {
|
||||
//恢复所有
|
||||
pressed = false;
|
||||
pressedLeft = false;
|
||||
pressedRight = false;
|
||||
pressedTop = false;
|
||||
pressedBottom = false;
|
||||
pressedLeftTop = false;
|
||||
pressedRightTop = false;
|
||||
pressedLeftBottom = false;
|
||||
pressedRightBottom = false;
|
||||
widget->setCursor(Qt::ArrowCursor);
|
||||
mousePressed = false;
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
pressedArea[i] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,5 +214,8 @@ void FramelessWidget::setWidget(QWidget *widget)
|
||||
this->widget->installEventFilter(this);
|
||||
//设置悬停为真,必须设置这个,不然当父窗体里边还有子窗体全部遮挡了识别不到MouseMove,需要识别HoverMove
|
||||
this->widget->setAttribute(Qt::WA_Hover, true);
|
||||
|
||||
isMin = false;
|
||||
flags = widget->windowFlags();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user