在以前的一篇文章中寫過一個通用的移動控件,做用就是用來傳入任意的widget控件,能夠在父類容器中自由移動。本篇文章要寫的是一個通用的無邊框類,確切的說這不叫控件應該叫組件纔對,控件是要看得見的東西,有繪製需求的,而這個須要依附在控件上。在咱們平時作的項目中,爲了U界面的美觀,不少會採用自定義無邊框窗體來處理,本身美化標題欄等,都會面臨一個相同的問題,就是設置好自定義無邊框窗體之後,要本身處理窗體的移動和拉伸,若是有多個無邊框窗體,不少人會想到每一個地方都寫重複的代碼來實現移動或者拉伸,何不封裝一個類來完成這個功能呢,直接傳入窗體便可。在QDialog窗體中能夠經過設置一個屬性sizeGripEnabled來實現右下角的拉伸,這個還不足以知足全部的需求,不少時候咱們還須要在四個角和上下左右都能拉伸大小,這個就須要重寫了,安裝事件過濾器,識別到鼠標移動到某個區域,鼠標形狀自動變化,而後識別是否已經按下,按下的話作對應的處理,對應的處理核心就是從新設置窗體的XY軸座標和大小。linux
開源地址:https://gitee.com/feiyangqingyun/QWidgetDemo https://github.com/feiyangqingyun/QWidgetDemoc++
#ifndef FRAMELESSWIDGET_H #define FRAMELESSWIDGET_H /** * 無邊框窗體類 做者:feiyangqingyun(QQ:517216493) 2019-10-03 * 1:能夠指定須要無邊框的widget * 2:邊框四周八個方位均可以自由拉伸 * 3:可設置對應位置的邊距,以便識別更大區域 * 4:可設置是否容許拖動 * 5:可設置是否容許拉伸 */ #include <QWidget> #ifdef quc #if (QT_VERSION < QT_VERSION_CHECK(5,7,0)) #include <QtDesigner/QDesignerExportWidget> #else #include <QtUiPlugin/QDesignerExportWidget> #endif class QDESIGNER_WIDGET_EXPORT FramelessWidget : public QObject #else class FramelessWidget : public QObject #endif { Q_OBJECT public: explicit FramelessWidget(QObject *parent = 0); protected: bool eventFilter(QObject *watched, QEvent *event); private: int padding; //邊距 bool moveEnable; //可移動 bool resizeEnable; //可拉伸 QWidget *widget; //無邊框窗體 bool pressed; //鼠標按下 bool pressedLeft; //鼠標按下左側 bool pressedRight; //鼠標按下右側 bool pressedTop; //鼠標按下上側 bool pressedBottom; //鼠標按下下側 bool pressedLeftTop; //鼠標按下左上側 bool pressedRightTop; //鼠標按下右上側 bool pressedLeftBottom; //鼠標按下左下側 bool pressedRightBottom; //鼠標按下右下側 int rectX, rectY, rectW, rectH; //窗體座標+寬高 QPoint lastPos; //鼠標按下處座標 QRect rectLeft; //左側區域 QRect rectRight; //右側區域 QRect rectTop; //上側區域 QRect rectBottom; //下側區域 QRect rectLeftTop; //左上側區域 QRect rectRightTop; //右上側區域 QRect rectLeftBottom; //左下側區域 QRect rectRightBottom; //右下側區域 public Q_SLOTS: //設置邊距 void setPadding(int padding); //設置是否可拖動+拉伸 void setMoveEnable(bool moveEnable); void setResizeEnable(bool resizeEnable); //設置要無邊框的窗體 void setWidget(QWidget *widget); }; #endif // FRAMELESSWIDGET_H
#include "framelesswidget.h" #include "qevent.h" #include "qdebug.h" FramelessWidget::FramelessWidget(QObject *parent) : QObject(parent) { padding = 8; moveEnable = true; resizeEnable = true; widget = 0; pressed = false; pressedLeft = false; pressedRight = false; pressedTop = false; pressedBottom = false; pressedLeftTop = false; pressedRightTop = false; pressedLeftBottom = false; pressedRightBottom = false; //若是父類是窗體則直接設置 if (parent->isWidgetType()) { setWidget((QWidget *)parent); } } bool FramelessWidget::eventFilter(QObject *watched, QEvent *event) { if (widget != 0 && watched == widget) { 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); //右側描點區域 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); } else if (event->type() == QEvent::HoverMove) { //設置對應鼠標形狀,這個必須放在這裏而不是下面,由於能夠在鼠標沒有按下的時候識別 QHoverEvent *hoverEvent = (QHoverEvent *)event; QPoint point = hoverEvent->pos(); if (resizeEnable) { 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); } } //根據當前鼠標位置,計算XY軸移動了多少 int offsetX = point.x() - lastPos.x(); int offsetY = point.y() - lastPos.y(); //根據按下處的位置判斷是不是移動控件仍是拉伸控件 if (moveEnable) { if (pressed) { widget->move(widget->x() + offsetX, widget->y() + offsetY); } } if (resizeEnable) { 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 (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; pressedLeft = false; pressedRight = false; pressedTop = false; pressedBottom = false; pressedLeftTop = false; pressedRightTop = false; pressedLeftBottom = false; pressedRightBottom = false; widget->setCursor(Qt::ArrowCursor); } } return QObject::eventFilter(watched, event); } void FramelessWidget::setPadding(int padding) { this->padding = padding; } void FramelessWidget::setMoveEnable(bool moveEnable) { this->moveEnable = moveEnable; } void FramelessWidget::setResizeEnable(bool resizeEnable) { this->resizeEnable = resizeEnable; } void FramelessWidget::setWidget(QWidget *widget) { if (this->widget == 0) { this->widget = widget; //設置鼠標追蹤爲真 this->widget->setMouseTracking(true); //綁定事件過濾器 this->widget->installEventFilter(this); //設置懸停爲真,必須設置這個,否則當父窗體裏邊還有子窗體所有遮擋了識別不到MouseMove,須要識別HoverMove this->widget->setAttribute(Qt::WA_Hover, true); } }