修正无边框窗体类

master
feiyangqingyun 2019-10-11 15:15:12 +08:00
parent 5202754498
commit bb89c4a67c
5 changed files with 134 additions and 70 deletions

View File

@ -5,6 +5,8 @@
FramelessWidget::FramelessWidget(QObject *parent) : QObject(parent) FramelessWidget::FramelessWidget(QObject *parent) : QObject(parent)
{ {
padding = 8; padding = 8;
moveEnable = true;
resizeEnable = true;
widget = 0; widget = 0;
pressed = false; pressed = false;
@ -79,78 +81,85 @@ bool FramelessWidget::eventFilter(QObject *watched, QEvent *event)
pressed = true; pressed = true;
} }
} else if (event->type() == QEvent::MouseMove) { } else if (event->type() == QEvent::MouseMove) {
//设置对应鼠标形状 //设置对应鼠标形状,这个必须放在这里而不是下面,因为可以在鼠标没有按下的时候识别
QPoint point = mouseEvent->pos(); QPoint point = mouseEvent->pos();
if (rectLeft.contains(point)) { if (resizeEnable) {
widget->setCursor(Qt::SizeHorCursor); if (rectLeft.contains(point)) {
} else if (rectRight.contains(point)) { widget->setCursor(Qt::SizeHorCursor);
widget->setCursor(Qt::SizeHorCursor); } else if (rectRight.contains(point)) {
} else if (rectTop.contains(point)) { widget->setCursor(Qt::SizeHorCursor);
widget->setCursor(Qt::SizeVerCursor); } else if (rectTop.contains(point)) {
} else if (rectBottom.contains(point)) { widget->setCursor(Qt::SizeVerCursor);
widget->setCursor(Qt::SizeVerCursor); } else if (rectBottom.contains(point)) {
} else if (rectLeftTop.contains(point)) { widget->setCursor(Qt::SizeVerCursor);
widget->setCursor(Qt::SizeFDiagCursor); } else if (rectLeftTop.contains(point)) {
} else if (rectRightTop.contains(point)) { widget->setCursor(Qt::SizeFDiagCursor);
widget->setCursor(Qt::SizeBDiagCursor); } else if (rectRightTop.contains(point)) {
} else if (rectLeftBottom.contains(point)) { widget->setCursor(Qt::SizeBDiagCursor);
widget->setCursor(Qt::SizeBDiagCursor); } else if (rectLeftBottom.contains(point)) {
} else if (rectRightBottom.contains(point)) { widget->setCursor(Qt::SizeBDiagCursor);
widget->setCursor(Qt::SizeFDiagCursor); } else if (rectRightBottom.contains(point)) {
} else { widget->setCursor(Qt::SizeFDiagCursor);
widget->setCursor(Qt::ArrowCursor); } else {
widget->setCursor(Qt::ArrowCursor);
}
} }
//根据当前鼠标位置,计算XY轴移动了多少 //根据当前鼠标位置,计算XY轴移动了多少
QPoint pos = mouseEvent->pos(); int offsetX = point.x() - lastPos.x();
int dx = pos.x() - lastPos.x(); int offsetY = point.y() - lastPos.y();
int dy = pos.y() - lastPos.y();
//根据按下处的位置判断是否是移动控件还是拉伸控件 //根据按下处的位置判断是否是移动控件还是拉伸控件
if (pressed) { if (moveEnable) {
widget->move(widget->x() + dx, widget->y() + dy); if (pressed) {
} else if (pressedLeft) { widget->move(widget->x() + offsetX, widget->y() + offsetY);
int resizeW = widget->width() - dx;
if (widget->minimumWidth() <= resizeW) {
widget->setGeometry(widget->x() + dx, rectY, resizeW, rectH);
} }
} else if (pressedRight) { }
widget->setGeometry(rectX, rectY, rectW + dx, rectH);
} else if (pressedTop) { if (resizeEnable) {
int resizeH = widget->height() - dy; if (pressedLeft) {
if (widget->minimumHeight() <= resizeH) { int resizeW = widget->width() - offsetX;
widget->setGeometry(rectX, widget->y() + dy, rectW, resizeH); if (widget->minimumWidth() <= resizeW) {
} widget->setGeometry(widget->x() + offsetX, rectY, resizeW, rectH);
} else if (pressedBottom) { }
widget->setGeometry(rectX, rectY, rectW, rectH + dy); } else if (pressedRight) {
} else if (pressedLeftTop) { widget->setGeometry(rectX, rectY, rectW + offsetX, rectH);
int resizeW = widget->width() - dx; } else if (pressedTop) {
int resizeH = widget->height() - dy; int resizeH = widget->height() - offsetY;
if (widget->minimumWidth() <= resizeW) { if (widget->minimumHeight() <= resizeH) {
widget->setGeometry(widget->x() + dx, widget->y(), resizeW, resizeH); widget->setGeometry(rectX, widget->y() + offsetY, rectW, resizeH);
} }
if (widget->minimumHeight() <= resizeH) { } else if (pressedBottom) {
widget->setGeometry(widget->x(), widget->y() + dy, resizeW, resizeH); widget->setGeometry(rectX, rectY, rectW, rectH + offsetY);
} } else if (pressedLeftTop) {
} else if (pressedRightTop) { int resizeW = widget->width() - offsetX;
int resizeW = rectW + dx; int resizeH = widget->height() - offsetY;
int resizeH = widget->height() - dy; if (widget->minimumWidth() <= resizeW) {
if (widget->minimumHeight() <= resizeH) { widget->setGeometry(widget->x() + offsetX, widget->y(), resizeW, resizeH);
widget->setGeometry(widget->x(), widget->y() + dy, resizeW, resizeH); }
} if (widget->minimumHeight() <= resizeH) {
} else if (pressedLeftBottom) { widget->setGeometry(widget->x(), widget->y() + offsetY, resizeW, resizeH);
int resizeW = widget->width() - dx; }
int resizeH = rectH + dy; } else if (pressedRightTop) {
if (widget->minimumWidth() <= resizeW) { int resizeW = rectW + offsetX;
widget->setGeometry(widget->x() + dx, widget->y(), resizeW, resizeH); int resizeH = widget->height() - offsetY;
} if (widget->minimumHeight() <= resizeH) {
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); widget->setGeometry(widget->x(), widget->y(), resizeW, resizeH);
} }
} else if (pressedRightBottom) {
int resizeW = rectW + dx;
int resizeH = rectH + dy;
widget->setGeometry(widget->x(), widget->y(), resizeW, resizeH);
} }
} else if (event->type() == QEvent::MouseButtonRelease) { } else if (event->type() == QEvent::MouseButtonRelease) {
//恢复所有 //恢复所有
@ -176,6 +185,16 @@ void FramelessWidget::setPadding(int padding)
this->padding = 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) void FramelessWidget::setWidget(QWidget *widget)
{ {
if (this->widget == 0) { if (this->widget == 0) {
@ -185,7 +204,7 @@ void FramelessWidget::setWidget(QWidget *widget)
//绑定事件过滤器 //绑定事件过滤器
this->widget->installEventFilter(this); this->widget->installEventFilter(this);
//设置无边框属性 //设置无边框属性
this->widget->setWindowFlags(Qt::FramelessWindowHint); this->widget->setWindowFlags(widget->windowFlags() | Qt::FramelessWindowHint);
//this->widget->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint); //this->widget->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
} }
} }

View File

@ -6,6 +6,8 @@
* 1:widget * 1:widget
* 2: * 2:
* 3:,便 * 3:,便
* 4:
* 5:
*/ */
#include <QWidget> #include <QWidget>
@ -32,6 +34,8 @@ protected:
private: private:
int padding; //边距 int padding; //边距
bool moveEnable; //可移动
bool resizeEnable; //可拉伸
QWidget *widget; //无边框窗体 QWidget *widget; //无边框窗体
bool pressed; //鼠标按下 bool pressed; //鼠标按下
@ -59,6 +63,10 @@ private:
public Q_SLOTS: public Q_SLOTS:
//设置边距 //设置边距
void setPadding(int padding); void setPadding(int padding);
//设置是否可拖动+拉伸
void setMoveEnable(bool moveEnable);
void setResizeEnable(bool resizeEnable);
//设置要无边框的窗体 //设置要无边框的窗体
void setWidget(QWidget *widget); void setWidget(QWidget *widget);
}; };

View File

@ -3,6 +3,7 @@
#include "frmframelesswidget.h" #include "frmframelesswidget.h"
#include "ui_frmframelesswidget.h" #include "ui_frmframelesswidget.h"
#include "qpushbutton.h" #include "qpushbutton.h"
#include "qcheckbox.h"
#include "framelesswidget.h" #include "framelesswidget.h"
frmFramelessWidget::frmFramelessWidget(QWidget *parent) : QWidget(parent), ui(new Ui::frmFramelessWidget) frmFramelessWidget::frmFramelessWidget(QWidget *parent) : QWidget(parent), ui(new Ui::frmFramelessWidget)
@ -15,11 +16,18 @@ frmFramelessWidget::~frmFramelessWidget()
delete ui; delete ui;
} }
void frmFramelessWidget::closeEvent(QCloseEvent *)
{
exit(0);
}
void frmFramelessWidget::on_btnOpen_clicked() void frmFramelessWidget::on_btnOpen_clicked()
{ {
QWidget *w = new QWidget; QWidget *w = new QWidget;
w->setAttribute(Qt::WA_DeleteOnClose); w->setAttribute(Qt::WA_DeleteOnClose);
w->setWindowFlags(Qt::WindowStaysOnTopHint);
w->setWindowTitle("自由拉伸无边框窗体"); w->setWindowTitle("自由拉伸无边框窗体");
w->setMinimumSize(200, 120);
w->resize(480, 320); w->resize(480, 320);
//设置下背景颜色区别看 //设置下背景颜色区别看
@ -28,11 +36,33 @@ void frmFramelessWidget::on_btnOpen_clicked()
w->setPalette(palette); w->setPalette(palette);
QPushButton *btn = new QPushButton(w); QPushButton *btn = new QPushButton(w);
connect(btn, SIGNAL(clicked(bool)), w, SLOT(close()));
btn->setGeometry(10, 10, 100, 25);
btn->setText("关闭"); btn->setText("关闭");
btn->setGeometry(10, 10, 130, 25);
connect(btn, SIGNAL(clicked(bool)), w, SLOT(close()));
FramelessWidget *f = new FramelessWidget(w); QCheckBox *cboxMove = new QCheckBox(w);
f->setWidget(w); cboxMove->setText("可移动");
cboxMove->setChecked(true);
cboxMove->setGeometry(10, 40, 70, 25);
connect(cboxMove, SIGNAL(stateChanged(int)), this, SLOT(stateChanged1(int)));
QCheckBox *cboxResize = new QCheckBox(w);
cboxResize->setText("可拉伸");
cboxResize->setChecked(true);
cboxResize->setGeometry(80, 40, 70, 25);
connect(cboxResize, SIGNAL(stateChanged(int)), this, SLOT(stateChanged2(int)));
frameless = new FramelessWidget(w);
frameless->setWidget(w);
w->show(); w->show();
} }
void frmFramelessWidget::stateChanged1(int arg1)
{
frameless->setMoveEnable(arg1 != 0);
}
void frmFramelessWidget::stateChanged2(int arg1)
{
frameless->setResizeEnable(arg1 != 0);
}

View File

@ -2,6 +2,7 @@
#define FRMFRAMELESSWIDGET_H #define FRMFRAMELESSWIDGET_H
#include <QWidget> #include <QWidget>
class FramelessWidget;
namespace Ui namespace Ui
{ {
@ -16,11 +17,17 @@ public:
explicit frmFramelessWidget(QWidget *parent = 0); explicit frmFramelessWidget(QWidget *parent = 0);
~frmFramelessWidget(); ~frmFramelessWidget();
protected:
void closeEvent(QCloseEvent *);
private: private:
Ui::frmFramelessWidget *ui; Ui::frmFramelessWidget *ui;
FramelessWidget *frameless;
private slots: private slots:
void on_btnOpen_clicked(); void on_btnOpen_clicked();
void stateChanged1(int arg1);
void stateChanged2(int arg1);
}; };
#endif // FRMFRAMELESSWIDGET_H #endif // FRMFRAMELESSWIDGET_H

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>500</width> <width>600</width>
<height>300</height> <height>480</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">