修正无边框类

master
feiyangqingyun 2019-10-11 17:13:42 +08:00
parent bb89c4a67c
commit 2a5256d1c6
4 changed files with 167 additions and 161 deletions

View File

@ -34,55 +34,26 @@ bool FramelessWidget::eventFilter(QObject *watched, QEvent *event)
int height = widget->height();
//左侧描点区域
rectLeft = QRectF(0, padding, padding, height - padding * 2);
rectLeft = QRect(0, padding, padding, height - padding * 2);
//上侧描点区域
rectTop = QRectF(padding, 0, width - padding * 2, padding);
rectTop = QRect(padding, 0, width - padding * 2, padding);
//右侧描点区域
rectRight = QRectF(width - padding, padding, padding, height - padding * 2);
rectRight = QRect(width - padding, padding, padding, height - padding * 2);
//下侧描点区域
rectBottom = QRectF(padding, height - padding, width - padding * 2, padding);
rectBottom = QRect(padding, height - padding, width - padding * 2, padding);
//左上角描点区域
rectLeftTop = QRectF(0, 0, padding, padding);
rectLeftTop = QRect(0, 0, padding, padding);
//右上角描点区域
rectRightTop = QRectF(width - padding, 0, padding, padding);
rectRightTop = QRect(width - padding, 0, padding, padding);
//左下角描点区域
rectLeftBottom = QRectF(0, height - padding, padding, padding);
rectLeftBottom = QRect(0, height - padding, padding, padding);
//右下角描点区域
rectRightBottom = QRectF(width - padding, height - padding, padding, padding);
} else {
QMouseEvent *mouseEvent = (QMouseEvent *)event;
if (event->type() == QEvent::MouseButtonPress) {
//记住当前控件坐标和宽高以及鼠标按下的坐标
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;
}
} else if (event->type() == QEvent::MouseMove) {
rectRightBottom = QRect(width - padding, height - padding, padding, padding);
} else if (event->type() == QEvent::HoverMove) {
//设置对应鼠标形状,这个必须放在这里而不是下面,因为可以在鼠标没有按下的时候识别
QPoint point = mouseEvent->pos();
QHoverEvent *hoverEvent = (QHoverEvent *)event;
QPoint point = hoverEvent->pos();
if (resizeEnable) {
if (rectLeft.contains(point)) {
widget->setCursor(Qt::SizeHorCursor);
@ -161,6 +132,37 @@ bool FramelessWidget::eventFilter(QObject *watched, QEvent *event)
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();
//判断按下的手柄的区域位置
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;
}
} else if (event->type() == QEvent::MouseMove) {
//改成用HoverMove识别
} else if (event->type() == QEvent::MouseButtonRelease) {
//恢复所有
pressed = false;
@ -175,7 +177,6 @@ bool FramelessWidget::eventFilter(QObject *watched, QEvent *event)
widget->setCursor(Qt::ArrowCursor);
}
}
}
return QObject::eventFilter(watched, event);
}
@ -203,8 +204,7 @@ void FramelessWidget::setWidget(QWidget *widget)
this->widget->setMouseTracking(true);
//绑定事件过滤器
this->widget->installEventFilter(this);
//设置无边框属性
this->widget->setWindowFlags(widget->windowFlags() | Qt::FramelessWindowHint);
//this->widget->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
//设置悬停为真,必须设置这个,不然当父窗体里边还有子窗体全部遮挡了识别不到MouseMove,需要识别HoverMove
this->widget->setAttribute(Qt::WA_Hover, true);
}
}

View File

@ -51,14 +51,14 @@ private:
int rectX, rectY, rectW, rectH; //窗体坐标+宽高
QPoint lastPos; //鼠标按下处坐标
QRectF rectLeft; //左侧区域
QRectF rectRight; //右侧区域
QRectF rectTop; //上侧区域
QRectF rectBottom; //下侧区域
QRectF rectLeftTop; //左上侧区域
QRectF rectRightTop; //右上侧区域
QRectF rectLeftBottom; //左下侧区域
QRectF rectRightBottom; //右下侧区域
QRect rectLeft; //左侧区域
QRect rectRight; //右侧区域
QRect rectTop; //上侧区域
QRect rectBottom; //下侧区域
QRect rectLeftTop; //左上侧区域
QRect rectRightTop; //右上侧区域
QRect rectLeftBottom; //左下侧区域
QRect rectRightBottom; //右下侧区域
public Q_SLOTS:
//设置边距

View File

@ -9,6 +9,7 @@
frmFramelessWidget::frmFramelessWidget(QWidget *parent) : QWidget(parent), ui(new Ui::frmFramelessWidget)
{
ui->setupUi(this);
w = 0;
}
frmFramelessWidget::~frmFramelessWidget()
@ -23,9 +24,11 @@ void frmFramelessWidget::closeEvent(QCloseEvent *)
void frmFramelessWidget::on_btnOpen_clicked()
{
QWidget *w = new QWidget;
w->setAttribute(Qt::WA_DeleteOnClose);
w->setWindowFlags(Qt::WindowStaysOnTopHint);
if (w == 0) {
w = new QWidget;
//设置无边框属性
w->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
//w->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
w->setWindowTitle("自由拉伸无边框窗体");
w->setMinimumSize(200, 120);
w->resize(480, 320);
@ -54,6 +57,8 @@ void frmFramelessWidget::on_btnOpen_clicked()
frameless = new FramelessWidget(w);
frameless->setWidget(w);
}
w->show();
}

View File

@ -22,6 +22,7 @@ protected:
private:
Ui::frmFramelessWidget *ui;
QWidget *w;
FramelessWidget *frameless;
private slots: