修复movewidget移出容器以外的bug
parent
94927e4427
commit
c69997e1b6
|
@ -1,4 +1,4 @@
|
||||||
#pragma execution_character_set("utf-8")
|
#pragma execution_character_set("utf-8")
|
||||||
|
|
||||||
#include "frmmovewidget.h"
|
#include "frmmovewidget.h"
|
||||||
#include "ui_frmmovewidget.h"
|
#include "ui_frmmovewidget.h"
|
||||||
|
|
|
@ -7,6 +7,7 @@ MoveWidget::MoveWidget(QObject *parent) : QObject(parent)
|
||||||
lastPoint = QPoint(0, 0);
|
lastPoint = QPoint(0, 0);
|
||||||
pressed = false;
|
pressed = false;
|
||||||
leftButton = true;
|
leftButton = true;
|
||||||
|
inControl = true;
|
||||||
widget = 0;
|
widget = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +30,23 @@ bool MoveWidget::eventFilter(QObject *watched, QEvent *event)
|
||||||
//计算坐标偏移值,调用move函数移动过去
|
//计算坐标偏移值,调用move函数移动过去
|
||||||
int offsetX = mouseEvent->pos().x() - lastPoint.x();
|
int offsetX = mouseEvent->pos().x() - lastPoint.x();
|
||||||
int offsetY = mouseEvent->pos().y() - lastPoint.y();
|
int offsetY = mouseEvent->pos().y() - lastPoint.y();
|
||||||
widget->move(widget->x() + offsetX, widget->y() + offsetY);
|
int x = widget->x() + offsetX;
|
||||||
|
int y = widget->y() + offsetY;
|
||||||
|
if (inControl) {
|
||||||
|
//可以自行调整限定在容器中的范围,这里默认保留20个像素在里面
|
||||||
|
int offset = 20;
|
||||||
|
bool xyOut = (x + widget->width() < offset || y + widget->height() < offset);
|
||||||
|
bool whOut = false;
|
||||||
|
QWidget *w = (QWidget *)widget->parent();
|
||||||
|
if (w != 0) {
|
||||||
|
whOut = (w->width() - x < offset || w->height() - y < offset);
|
||||||
|
}
|
||||||
|
if (xyOut || whOut) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
widget->move(x, y);
|
||||||
} else if (mouseEvent->type() == QEvent::MouseButtonRelease && pressed) {
|
} else if (mouseEvent->type() == QEvent::MouseButtonRelease && pressed) {
|
||||||
pressed = false;
|
pressed = false;
|
||||||
}
|
}
|
||||||
|
@ -50,3 +67,8 @@ void MoveWidget::setLeftButton(bool leftButton)
|
||||||
{
|
{
|
||||||
this->leftButton = leftButton;
|
this->leftButton = leftButton;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MoveWidget::setInControl(bool inControl)
|
||||||
|
{
|
||||||
|
this->inControl = inControl;
|
||||||
|
}
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 226 KiB |
|
@ -34,11 +34,14 @@ private:
|
||||||
QPoint lastPoint; //最后按下的坐标
|
QPoint lastPoint; //最后按下的坐标
|
||||||
bool pressed; //鼠标是否按下
|
bool pressed; //鼠标是否按下
|
||||||
bool leftButton; //限定鼠标左键
|
bool leftButton; //限定鼠标左键
|
||||||
|
bool inControl; //限定在容器内
|
||||||
QWidget *widget; //移动的控件
|
QWidget *widget; //移动的控件
|
||||||
|
|
||||||
public Q_SLOTS:
|
public Q_SLOTS:
|
||||||
//设置是否限定鼠标左键
|
//设置是否限定鼠标左键
|
||||||
void setLeftButton(bool leftButton);
|
void setLeftButton(bool leftButton);
|
||||||
|
//设置是否限定不能移出容器外面
|
||||||
|
void setInControl(bool inControl);
|
||||||
//设置要移动的控件
|
//设置要移动的控件
|
||||||
void setWidget(QWidget *widget);
|
void setWidget(QWidget *widget);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue