修正无边框窗体类
parent
5202754498
commit
bb89c4a67c
|
@ -5,6 +5,8 @@
|
|||
FramelessWidget::FramelessWidget(QObject *parent) : QObject(parent)
|
||||
{
|
||||
padding = 8;
|
||||
moveEnable = true;
|
||||
resizeEnable = true;
|
||||
widget = 0;
|
||||
|
||||
pressed = false;
|
||||
|
@ -79,8 +81,9 @@ bool FramelessWidget::eventFilter(QObject *watched, QEvent *event)
|
|||
pressed = true;
|
||||
}
|
||||
} else if (event->type() == QEvent::MouseMove) {
|
||||
//设置对应鼠标形状
|
||||
//设置对应鼠标形状,这个必须放在这里而不是下面,因为可以在鼠标没有按下的时候识别
|
||||
QPoint point = mouseEvent->pos();
|
||||
if (resizeEnable) {
|
||||
if (rectLeft.contains(point)) {
|
||||
widget->setCursor(Qt::SizeHorCursor);
|
||||
} else if (rectRight.contains(point)) {
|
||||
|
@ -100,58 +103,64 @@ bool FramelessWidget::eventFilter(QObject *watched, QEvent *event)
|
|||
} else {
|
||||
widget->setCursor(Qt::ArrowCursor);
|
||||
}
|
||||
}
|
||||
|
||||
//根据当前鼠标位置,计算XY轴移动了多少
|
||||
QPoint pos = mouseEvent->pos();
|
||||
int dx = pos.x() - lastPos.x();
|
||||
int dy = pos.y() - lastPos.y();
|
||||
int offsetX = point.x() - lastPos.x();
|
||||
int offsetY = point.y() - lastPos.y();
|
||||
|
||||
//根据按下处的位置判断是否是移动控件还是拉伸控件
|
||||
if (moveEnable) {
|
||||
if (pressed) {
|
||||
widget->move(widget->x() + dx, widget->y() + dy);
|
||||
} else if (pressedLeft) {
|
||||
int resizeW = widget->width() - dx;
|
||||
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() + dx, rectY, resizeW, rectH);
|
||||
widget->setGeometry(widget->x() + offsetX, rectY, resizeW, rectH);
|
||||
}
|
||||
} else if (pressedRight) {
|
||||
widget->setGeometry(rectX, rectY, rectW + dx, rectH);
|
||||
widget->setGeometry(rectX, rectY, rectW + offsetX, rectH);
|
||||
} else if (pressedTop) {
|
||||
int resizeH = widget->height() - dy;
|
||||
int resizeH = widget->height() - offsetY;
|
||||
if (widget->minimumHeight() <= resizeH) {
|
||||
widget->setGeometry(rectX, widget->y() + dy, rectW, resizeH);
|
||||
widget->setGeometry(rectX, widget->y() + offsetY, rectW, resizeH);
|
||||
}
|
||||
} else if (pressedBottom) {
|
||||
widget->setGeometry(rectX, rectY, rectW, rectH + dy);
|
||||
widget->setGeometry(rectX, rectY, rectW, rectH + offsetY);
|
||||
} else if (pressedLeftTop) {
|
||||
int resizeW = widget->width() - dx;
|
||||
int resizeH = widget->height() - dy;
|
||||
int resizeW = widget->width() - offsetX;
|
||||
int resizeH = widget->height() - offsetY;
|
||||
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) {
|
||||
widget->setGeometry(widget->x(), widget->y() + dy, resizeW, resizeH);
|
||||
widget->setGeometry(widget->x(), widget->y() + offsetY, resizeW, resizeH);
|
||||
}
|
||||
} else if (pressedRightTop) {
|
||||
int resizeW = rectW + dx;
|
||||
int resizeH = widget->height() - dy;
|
||||
int resizeW = rectW + offsetX;
|
||||
int resizeH = widget->height() - offsetY;
|
||||
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) {
|
||||
int resizeW = widget->width() - dx;
|
||||
int resizeH = rectH + dy;
|
||||
int resizeW = widget->width() - offsetX;
|
||||
int resizeH = rectH + offsetY;
|
||||
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) {
|
||||
widget->setGeometry(widget->x(), widget->y(), resizeW, resizeH);
|
||||
}
|
||||
} else if (pressedRightBottom) {
|
||||
int resizeW = rectW + dx;
|
||||
int resizeH = rectH + dy;
|
||||
int resizeW = rectW + offsetX;
|
||||
int resizeH = rectH + offsetY;
|
||||
widget->setGeometry(widget->x(), widget->y(), resizeW, resizeH);
|
||||
}
|
||||
}
|
||||
} else if (event->type() == QEvent::MouseButtonRelease) {
|
||||
//恢复所有
|
||||
pressed = false;
|
||||
|
@ -176,6 +185,16 @@ 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) {
|
||||
|
@ -185,7 +204,7 @@ void FramelessWidget::setWidget(QWidget *widget)
|
|||
//绑定事件过滤器
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
* 1:可以指定需要无边框的widget
|
||||
* 2:边框四周八个方位都可以自由拉伸
|
||||
* 3:可设置对应位置的边距,以便识别更大区域
|
||||
* 4:可设置是否允许拖动
|
||||
* 5:可设置是否允许拉伸
|
||||
*/
|
||||
|
||||
#include <QWidget>
|
||||
|
@ -32,6 +34,8 @@ protected:
|
|||
|
||||
private:
|
||||
int padding; //边距
|
||||
bool moveEnable; //可移动
|
||||
bool resizeEnable; //可拉伸
|
||||
QWidget *widget; //无边框窗体
|
||||
|
||||
bool pressed; //鼠标按下
|
||||
|
@ -59,6 +63,10 @@ private:
|
|||
public Q_SLOTS:
|
||||
//设置边距
|
||||
void setPadding(int padding);
|
||||
//设置是否可拖动+拉伸
|
||||
void setMoveEnable(bool moveEnable);
|
||||
void setResizeEnable(bool resizeEnable);
|
||||
|
||||
//设置要无边框的窗体
|
||||
void setWidget(QWidget *widget);
|
||||
};
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include "frmframelesswidget.h"
|
||||
#include "ui_frmframelesswidget.h"
|
||||
#include "qpushbutton.h"
|
||||
#include "qcheckbox.h"
|
||||
#include "framelesswidget.h"
|
||||
|
||||
frmFramelessWidget::frmFramelessWidget(QWidget *parent) : QWidget(parent), ui(new Ui::frmFramelessWidget)
|
||||
|
@ -15,11 +16,18 @@ frmFramelessWidget::~frmFramelessWidget()
|
|||
delete ui;
|
||||
}
|
||||
|
||||
void frmFramelessWidget::closeEvent(QCloseEvent *)
|
||||
{
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void frmFramelessWidget::on_btnOpen_clicked()
|
||||
{
|
||||
QWidget *w = new QWidget;
|
||||
w->setAttribute(Qt::WA_DeleteOnClose);
|
||||
w->setWindowFlags(Qt::WindowStaysOnTopHint);
|
||||
w->setWindowTitle("自由拉伸无边框窗体");
|
||||
w->setMinimumSize(200, 120);
|
||||
w->resize(480, 320);
|
||||
|
||||
//设置下背景颜色区别看
|
||||
|
@ -28,11 +36,33 @@ void frmFramelessWidget::on_btnOpen_clicked()
|
|||
w->setPalette(palette);
|
||||
|
||||
QPushButton *btn = new QPushButton(w);
|
||||
connect(btn, SIGNAL(clicked(bool)), w, SLOT(close()));
|
||||
btn->setGeometry(10, 10, 100, 25);
|
||||
btn->setText("关闭");
|
||||
btn->setGeometry(10, 10, 130, 25);
|
||||
connect(btn, SIGNAL(clicked(bool)), w, SLOT(close()));
|
||||
|
||||
FramelessWidget *f = new FramelessWidget(w);
|
||||
f->setWidget(w);
|
||||
QCheckBox *cboxMove = new QCheckBox(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();
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
#include <QWidget>
|
||||
class FramelessWidget;
|
||||
|
||||
namespace Ui
|
||||
{
|
||||
|
@ -16,11 +17,17 @@ public:
|
|||
explicit frmFramelessWidget(QWidget *parent = 0);
|
||||
~frmFramelessWidget();
|
||||
|
||||
protected:
|
||||
void closeEvent(QCloseEvent *);
|
||||
|
||||
private:
|
||||
Ui::frmFramelessWidget *ui;
|
||||
FramelessWidget *frameless;
|
||||
|
||||
private slots:
|
||||
void on_btnOpen_clicked();
|
||||
void stateChanged1(int arg1);
|
||||
void stateChanged2(int arg1);
|
||||
};
|
||||
|
||||
#endif // FRMFRAMELESSWIDGET_H
|
||||
|
|
|
@ -6,8 +6,8 @@
|
|||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>500</width>
|
||||
<height>300</height>
|
||||
<width>600</width>
|
||||
<height>480</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
|
|
Loading…
Reference in New Issue