修正无边框窗体类
parent
5202754498
commit
bb89c4a67c
|
@ -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,8 +81,9 @@ 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 (resizeEnable) {
|
||||||
if (rectLeft.contains(point)) {
|
if (rectLeft.contains(point)) {
|
||||||
widget->setCursor(Qt::SizeHorCursor);
|
widget->setCursor(Qt::SizeHorCursor);
|
||||||
} else if (rectRight.contains(point)) {
|
} else if (rectRight.contains(point)) {
|
||||||
|
@ -100,58 +103,64 @@ bool FramelessWidget::eventFilter(QObject *watched, QEvent *event)
|
||||||
} else {
|
} else {
|
||||||
widget->setCursor(Qt::ArrowCursor);
|
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 (moveEnable) {
|
||||||
if (pressed) {
|
if (pressed) {
|
||||||
widget->move(widget->x() + dx, widget->y() + dy);
|
widget->move(widget->x() + offsetX, widget->y() + offsetY);
|
||||||
} else if (pressedLeft) {
|
}
|
||||||
int resizeW = widget->width() - dx;
|
}
|
||||||
|
|
||||||
|
if (resizeEnable) {
|
||||||
|
if (pressedLeft) {
|
||||||
|
int resizeW = widget->width() - offsetX;
|
||||||
if (widget->minimumWidth() <= resizeW) {
|
if (widget->minimumWidth() <= resizeW) {
|
||||||
widget->setGeometry(widget->x() + dx, rectY, resizeW, rectH);
|
widget->setGeometry(widget->x() + offsetX, rectY, resizeW, rectH);
|
||||||
}
|
}
|
||||||
} else if (pressedRight) {
|
} else if (pressedRight) {
|
||||||
widget->setGeometry(rectX, rectY, rectW + dx, rectH);
|
widget->setGeometry(rectX, rectY, rectW + offsetX, rectH);
|
||||||
} else if (pressedTop) {
|
} else if (pressedTop) {
|
||||||
int resizeH = widget->height() - dy;
|
int resizeH = widget->height() - offsetY;
|
||||||
if (widget->minimumHeight() <= resizeH) {
|
if (widget->minimumHeight() <= resizeH) {
|
||||||
widget->setGeometry(rectX, widget->y() + dy, rectW, resizeH);
|
widget->setGeometry(rectX, widget->y() + offsetY, rectW, resizeH);
|
||||||
}
|
}
|
||||||
} else if (pressedBottom) {
|
} else if (pressedBottom) {
|
||||||
widget->setGeometry(rectX, rectY, rectW, rectH + dy);
|
widget->setGeometry(rectX, rectY, rectW, rectH + offsetY);
|
||||||
} else if (pressedLeftTop) {
|
} else if (pressedLeftTop) {
|
||||||
int resizeW = widget->width() - dx;
|
int resizeW = widget->width() - offsetX;
|
||||||
int resizeH = widget->height() - dy;
|
int resizeH = widget->height() - offsetY;
|
||||||
if (widget->minimumWidth() <= resizeW) {
|
if (widget->minimumWidth() <= resizeW) {
|
||||||
widget->setGeometry(widget->x() + dx, widget->y(), resizeW, resizeH);
|
widget->setGeometry(widget->x() + offsetX, widget->y(), resizeW, resizeH);
|
||||||
}
|
}
|
||||||
if (widget->minimumHeight() <= resizeH) {
|
if (widget->minimumHeight() <= resizeH) {
|
||||||
widget->setGeometry(widget->x(), widget->y() + dy, resizeW, resizeH);
|
widget->setGeometry(widget->x(), widget->y() + offsetY, resizeW, resizeH);
|
||||||
}
|
}
|
||||||
} else if (pressedRightTop) {
|
} else if (pressedRightTop) {
|
||||||
int resizeW = rectW + dx;
|
int resizeW = rectW + offsetX;
|
||||||
int resizeH = widget->height() - dy;
|
int resizeH = widget->height() - offsetY;
|
||||||
if (widget->minimumHeight() <= resizeH) {
|
if (widget->minimumHeight() <= resizeH) {
|
||||||
widget->setGeometry(widget->x(), widget->y() + dy, resizeW, resizeH);
|
widget->setGeometry(widget->x(), widget->y() + offsetY, resizeW, resizeH);
|
||||||
}
|
}
|
||||||
} else if (pressedLeftBottom) {
|
} else if (pressedLeftBottom) {
|
||||||
int resizeW = widget->width() - dx;
|
int resizeW = widget->width() - offsetX;
|
||||||
int resizeH = rectH + dy;
|
int resizeH = rectH + offsetY;
|
||||||
if (widget->minimumWidth() <= resizeW) {
|
if (widget->minimumWidth() <= resizeW) {
|
||||||
widget->setGeometry(widget->x() + dx, widget->y(), resizeW, resizeH);
|
widget->setGeometry(widget->x() + offsetX, widget->y(), resizeW, resizeH);
|
||||||
}
|
}
|
||||||
if (widget->minimumHeight() <= resizeH) {
|
if (widget->minimumHeight() <= resizeH) {
|
||||||
widget->setGeometry(widget->x(), widget->y(), resizeW, resizeH);
|
widget->setGeometry(widget->x(), widget->y(), resizeW, resizeH);
|
||||||
}
|
}
|
||||||
} else if (pressedRightBottom) {
|
} else if (pressedRightBottom) {
|
||||||
int resizeW = rectW + dx;
|
int resizeW = rectW + offsetX;
|
||||||
int resizeH = rectH + dy;
|
int resizeH = rectH + offsetY;
|
||||||
widget->setGeometry(widget->x(), widget->y(), resizeW, resizeH);
|
widget->setGeometry(widget->x(), widget->y(), resizeW, resizeH);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else if (event->type() == QEvent::MouseButtonRelease) {
|
} else if (event->type() == QEvent::MouseButtonRelease) {
|
||||||
//恢复所有
|
//恢复所有
|
||||||
pressed = false;
|
pressed = false;
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
};
|
};
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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">
|
||||||
|
|
Loading…
Reference in New Issue