修正无边框类
parent
bb89c4a67c
commit
2a5256d1c6
|
@ -34,146 +34,147 @@ bool FramelessWidget::eventFilter(QObject *watched, QEvent *event)
|
||||||
int height = widget->height();
|
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);
|
rectRightBottom = QRect(width - padding, height - padding, padding, padding);
|
||||||
} else {
|
} else if (event->type() == QEvent::HoverMove) {
|
||||||
QMouseEvent *mouseEvent = (QMouseEvent *)event;
|
//设置对应鼠标形状,这个必须放在这里而不是下面,因为可以在鼠标没有按下的时候识别
|
||||||
if (event->type() == QEvent::MouseButtonPress) {
|
QHoverEvent *hoverEvent = (QHoverEvent *)event;
|
||||||
//记住当前控件坐标和宽高以及鼠标按下的坐标
|
QPoint point = hoverEvent->pos();
|
||||||
rectX = widget->x();
|
if (resizeEnable) {
|
||||||
rectY = widget->y();
|
if (rectLeft.contains(point)) {
|
||||||
rectW = widget->width();
|
widget->setCursor(Qt::SizeHorCursor);
|
||||||
rectH = widget->height();
|
} else if (rectRight.contains(point)) {
|
||||||
lastPos = mouseEvent->pos();
|
widget->setCursor(Qt::SizeHorCursor);
|
||||||
|
} else if (rectTop.contains(point)) {
|
||||||
//判断按下的手柄的区域位置
|
widget->setCursor(Qt::SizeVerCursor);
|
||||||
if (rectLeft.contains(lastPos)) {
|
} else if (rectBottom.contains(point)) {
|
||||||
pressedLeft = true;
|
widget->setCursor(Qt::SizeVerCursor);
|
||||||
} else if (rectRight.contains(lastPos)) {
|
} else if (rectLeftTop.contains(point)) {
|
||||||
pressedRight = true;
|
widget->setCursor(Qt::SizeFDiagCursor);
|
||||||
} else if (rectTop.contains(lastPos)) {
|
} else if (rectRightTop.contains(point)) {
|
||||||
pressedTop = true;
|
widget->setCursor(Qt::SizeBDiagCursor);
|
||||||
} else if (rectBottom.contains(lastPos)) {
|
} else if (rectLeftBottom.contains(point)) {
|
||||||
pressedBottom = true;
|
widget->setCursor(Qt::SizeBDiagCursor);
|
||||||
} else if (rectLeftTop.contains(lastPos)) {
|
} else if (rectRightBottom.contains(point)) {
|
||||||
pressedLeftTop = true;
|
widget->setCursor(Qt::SizeFDiagCursor);
|
||||||
} else if (rectRightTop.contains(lastPos)) {
|
|
||||||
pressedRightTop = true;
|
|
||||||
} else if (rectLeftBottom.contains(lastPos)) {
|
|
||||||
pressedLeftBottom = true;
|
|
||||||
} else if (rectRightBottom.contains(lastPos)) {
|
|
||||||
pressedRightBottom = true;
|
|
||||||
} else {
|
} else {
|
||||||
pressed = true;
|
widget->setCursor(Qt::ArrowCursor);
|
||||||
}
|
}
|
||||||
} else if (event->type() == QEvent::MouseMove) {
|
}
|
||||||
//设置对应鼠标形状,这个必须放在这里而不是下面,因为可以在鼠标没有按下的时候识别
|
|
||||||
QPoint point = mouseEvent->pos();
|
//根据当前鼠标位置,计算XY轴移动了多少
|
||||||
if (resizeEnable) {
|
int offsetX = point.x() - lastPos.x();
|
||||||
if (rectLeft.contains(point)) {
|
int offsetY = point.y() - lastPos.y();
|
||||||
widget->setCursor(Qt::SizeHorCursor);
|
|
||||||
} else if (rectRight.contains(point)) {
|
//根据按下处的位置判断是否是移动控件还是拉伸控件
|
||||||
widget->setCursor(Qt::SizeHorCursor);
|
if (moveEnable) {
|
||||||
} else if (rectTop.contains(point)) {
|
if (pressed) {
|
||||||
widget->setCursor(Qt::SizeVerCursor);
|
widget->move(widget->x() + offsetX, widget->y() + offsetY);
|
||||||
} else if (rectBottom.contains(point)) {
|
}
|
||||||
widget->setCursor(Qt::SizeVerCursor);
|
}
|
||||||
} else if (rectLeftTop.contains(point)) {
|
|
||||||
widget->setCursor(Qt::SizeFDiagCursor);
|
if (resizeEnable) {
|
||||||
} else if (rectRightTop.contains(point)) {
|
if (pressedLeft) {
|
||||||
widget->setCursor(Qt::SizeBDiagCursor);
|
int resizeW = widget->width() - offsetX;
|
||||||
} else if (rectLeftBottom.contains(point)) {
|
if (widget->minimumWidth() <= resizeW) {
|
||||||
widget->setCursor(Qt::SizeBDiagCursor);
|
widget->setGeometry(widget->x() + offsetX, rectY, resizeW, rectH);
|
||||||
} else if (rectRightBottom.contains(point)) {
|
|
||||||
widget->setCursor(Qt::SizeFDiagCursor);
|
|
||||||
} else {
|
|
||||||
widget->setCursor(Qt::ArrowCursor);
|
|
||||||
}
|
}
|
||||||
}
|
} else if (pressedRight) {
|
||||||
|
widget->setGeometry(rectX, rectY, rectW + offsetX, rectH);
|
||||||
//根据当前鼠标位置,计算XY轴移动了多少
|
} else if (pressedTop) {
|
||||||
int offsetX = point.x() - lastPos.x();
|
int resizeH = widget->height() - offsetY;
|
||||||
int offsetY = point.y() - lastPos.y();
|
if (widget->minimumHeight() <= resizeH) {
|
||||||
|
widget->setGeometry(rectX, widget->y() + offsetY, rectW, resizeH);
|
||||||
//根据按下处的位置判断是否是移动控件还是拉伸控件
|
|
||||||
if (moveEnable) {
|
|
||||||
if (pressed) {
|
|
||||||
widget->move(widget->x() + offsetX, widget->y() + offsetY);
|
|
||||||
}
|
}
|
||||||
}
|
} else if (pressedBottom) {
|
||||||
|
widget->setGeometry(rectX, rectY, rectW, rectH + offsetY);
|
||||||
if (resizeEnable) {
|
} else if (pressedLeftTop) {
|
||||||
if (pressedLeft) {
|
int resizeW = widget->width() - offsetX;
|
||||||
int resizeW = widget->width() - offsetX;
|
int resizeH = widget->height() - offsetY;
|
||||||
if (widget->minimumWidth() <= resizeW) {
|
if (widget->minimumWidth() <= resizeW) {
|
||||||
widget->setGeometry(widget->x() + offsetX, rectY, resizeW, rectH);
|
widget->setGeometry(widget->x() + offsetX, widget->y(), resizeW, resizeH);
|
||||||
}
|
}
|
||||||
} else if (pressedRight) {
|
if (widget->minimumHeight() <= resizeH) {
|
||||||
widget->setGeometry(rectX, rectY, rectW + offsetX, rectH);
|
widget->setGeometry(widget->x(), widget->y() + offsetY, resizeW, resizeH);
|
||||||
} else if (pressedTop) {
|
}
|
||||||
int resizeH = widget->height() - offsetY;
|
} else if (pressedRightTop) {
|
||||||
if (widget->minimumHeight() <= resizeH) {
|
int resizeW = rectW + offsetX;
|
||||||
widget->setGeometry(rectX, widget->y() + offsetY, rectW, resizeH);
|
int resizeH = widget->height() - offsetY;
|
||||||
}
|
if (widget->minimumHeight() <= resizeH) {
|
||||||
} else if (pressedBottom) {
|
widget->setGeometry(widget->x(), widget->y() + offsetY, resizeW, resizeH);
|
||||||
widget->setGeometry(rectX, rectY, rectW, rectH + offsetY);
|
}
|
||||||
} else if (pressedLeftTop) {
|
} else if (pressedLeftBottom) {
|
||||||
int resizeW = widget->width() - offsetX;
|
int resizeW = widget->width() - offsetX;
|
||||||
int resizeH = widget->height() - offsetY;
|
int resizeH = rectH + offsetY;
|
||||||
if (widget->minimumWidth() <= resizeW) {
|
if (widget->minimumWidth() <= resizeW) {
|
||||||
widget->setGeometry(widget->x() + offsetX, 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() + 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);
|
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::MouseButtonRelease) {
|
|
||||||
//恢复所有
|
|
||||||
pressed = false;
|
|
||||||
pressedLeft = false;
|
|
||||||
pressedRight = false;
|
|
||||||
pressedTop = false;
|
|
||||||
pressedBottom = false;
|
|
||||||
pressedLeftTop = false;
|
|
||||||
pressedRightTop = false;
|
|
||||||
pressedLeftBottom = false;
|
|
||||||
pressedRightBottom = false;
|
|
||||||
widget->setCursor(Qt::ArrowCursor);
|
|
||||||
}
|
}
|
||||||
|
} 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -203,8 +204,7 @@ void FramelessWidget::setWidget(QWidget *widget)
|
||||||
this->widget->setMouseTracking(true);
|
this->widget->setMouseTracking(true);
|
||||||
//绑定事件过滤器
|
//绑定事件过滤器
|
||||||
this->widget->installEventFilter(this);
|
this->widget->installEventFilter(this);
|
||||||
//设置无边框属性
|
//设置悬停为真,必须设置这个,不然当父窗体里边还有子窗体全部遮挡了识别不到MouseMove,需要识别HoverMove
|
||||||
this->widget->setWindowFlags(widget->windowFlags() | Qt::FramelessWindowHint);
|
this->widget->setAttribute(Qt::WA_Hover, true);
|
||||||
//this->widget->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,14 +51,14 @@ private:
|
||||||
int rectX, rectY, rectW, rectH; //窗体坐标+宽高
|
int rectX, rectY, rectW, rectH; //窗体坐标+宽高
|
||||||
QPoint lastPos; //鼠标按下处坐标
|
QPoint lastPos; //鼠标按下处坐标
|
||||||
|
|
||||||
QRectF rectLeft; //左侧区域
|
QRect rectLeft; //左侧区域
|
||||||
QRectF rectRight; //右侧区域
|
QRect rectRight; //右侧区域
|
||||||
QRectF rectTop; //上侧区域
|
QRect rectTop; //上侧区域
|
||||||
QRectF rectBottom; //下侧区域
|
QRect rectBottom; //下侧区域
|
||||||
QRectF rectLeftTop; //左上侧区域
|
QRect rectLeftTop; //左上侧区域
|
||||||
QRectF rectRightTop; //右上侧区域
|
QRect rectRightTop; //右上侧区域
|
||||||
QRectF rectLeftBottom; //左下侧区域
|
QRect rectLeftBottom; //左下侧区域
|
||||||
QRectF rectRightBottom; //右下侧区域
|
QRect rectRightBottom; //右下侧区域
|
||||||
|
|
||||||
public Q_SLOTS:
|
public Q_SLOTS:
|
||||||
//设置边距
|
//设置边距
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
frmFramelessWidget::frmFramelessWidget(QWidget *parent) : QWidget(parent), ui(new Ui::frmFramelessWidget)
|
frmFramelessWidget::frmFramelessWidget(QWidget *parent) : QWidget(parent), ui(new Ui::frmFramelessWidget)
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
w = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
frmFramelessWidget::~frmFramelessWidget()
|
frmFramelessWidget::~frmFramelessWidget()
|
||||||
|
@ -23,37 +24,41 @@ void frmFramelessWidget::closeEvent(QCloseEvent *)
|
||||||
|
|
||||||
void frmFramelessWidget::on_btnOpen_clicked()
|
void frmFramelessWidget::on_btnOpen_clicked()
|
||||||
{
|
{
|
||||||
QWidget *w = new QWidget;
|
if (w == 0) {
|
||||||
w->setAttribute(Qt::WA_DeleteOnClose);
|
w = new QWidget;
|
||||||
w->setWindowFlags(Qt::WindowStaysOnTopHint);
|
//设置无边框属性
|
||||||
w->setWindowTitle("自由拉伸无边框窗体");
|
w->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
|
||||||
w->setMinimumSize(200, 120);
|
//w->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
|
||||||
w->resize(480, 320);
|
w->setWindowTitle("自由拉伸无边框窗体");
|
||||||
|
w->setMinimumSize(200, 120);
|
||||||
|
w->resize(480, 320);
|
||||||
|
|
||||||
//设置下背景颜色区别看
|
//设置下背景颜色区别看
|
||||||
QPalette palette = w->palette();
|
QPalette palette = w->palette();
|
||||||
palette.setBrush(QPalette::Background, QColor(162, 121, 197));
|
palette.setBrush(QPalette::Background, QColor(162, 121, 197));
|
||||||
w->setPalette(palette);
|
w->setPalette(palette);
|
||||||
|
|
||||||
QPushButton *btn = new QPushButton(w);
|
QPushButton *btn = new QPushButton(w);
|
||||||
btn->setText("关闭");
|
btn->setText("关闭");
|
||||||
btn->setGeometry(10, 10, 130, 25);
|
btn->setGeometry(10, 10, 130, 25);
|
||||||
connect(btn, SIGNAL(clicked(bool)), w, SLOT(close()));
|
connect(btn, SIGNAL(clicked(bool)), w, SLOT(close()));
|
||||||
|
|
||||||
QCheckBox *cboxMove = new QCheckBox(w);
|
QCheckBox *cboxMove = new QCheckBox(w);
|
||||||
cboxMove->setText("可移动");
|
cboxMove->setText("可移动");
|
||||||
cboxMove->setChecked(true);
|
cboxMove->setChecked(true);
|
||||||
cboxMove->setGeometry(10, 40, 70, 25);
|
cboxMove->setGeometry(10, 40, 70, 25);
|
||||||
connect(cboxMove, SIGNAL(stateChanged(int)), this, SLOT(stateChanged1(int)));
|
connect(cboxMove, SIGNAL(stateChanged(int)), this, SLOT(stateChanged1(int)));
|
||||||
|
|
||||||
QCheckBox *cboxResize = new QCheckBox(w);
|
QCheckBox *cboxResize = new QCheckBox(w);
|
||||||
cboxResize->setText("可拉伸");
|
cboxResize->setText("可拉伸");
|
||||||
cboxResize->setChecked(true);
|
cboxResize->setChecked(true);
|
||||||
cboxResize->setGeometry(80, 40, 70, 25);
|
cboxResize->setGeometry(80, 40, 70, 25);
|
||||||
connect(cboxResize, SIGNAL(stateChanged(int)), this, SLOT(stateChanged2(int)));
|
connect(cboxResize, SIGNAL(stateChanged(int)), this, SLOT(stateChanged2(int)));
|
||||||
|
|
||||||
|
frameless = new FramelessWidget(w);
|
||||||
|
frameless->setWidget(w);
|
||||||
|
}
|
||||||
|
|
||||||
frameless = new FramelessWidget(w);
|
|
||||||
frameless->setWidget(w);
|
|
||||||
w->show();
|
w->show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,7 @@ protected:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::frmFramelessWidget *ui;
|
Ui::frmFramelessWidget *ui;
|
||||||
|
QWidget *w;
|
||||||
FramelessWidget *frameless;
|
FramelessWidget *frameless;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
|
|
Loading…
Reference in New Issue