做项目时,为了界面美观些,把系统提供的窗口框架去掉后(无框架窗口),最令人恶心的是,无法改变无边框窗口的大小,也无法移动窗口。
1、首先要了解一下九宫格,什么是九宫格?
一个窗体可以被划分为上、下、左、右、左上、左下、右上、右下、中间。除了中间部分,其余都要写代码。
设外边框(如左部分)的宽度都为 Padding;
2、在头文件中我们加一个宏定义,及一个用于判断鼠标将要移动方向的枚举:
#define PADDING 1
enum Direction //拖动方向
{
UP=0,
DOWN=1,
LEFT,
RIGHT,
LEFTTOP,
LEFTBOTTOM,
RIGHTBOTTOM,
RIGHTTOP,
NONE
};
3、在头文件中声明:
public:
void region(const QPoint ¤tGlobalPoint); //鼠标的位置,改变光标
protected:
//鼠标按下移动及释放事件
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
private:
QPoint m_movePoint; //鼠标的位置
bool isLeftPressDown; // 判断左键是否按下
Direction dir; // 窗口大小改变时,记录改变方向
4、在构造函数中做初始化:
this->setMinimumHeight(100);
this->setMinimumWidth(150);
this->setWindowFlags(Qt::FramelessWindowHint); // 设置成无边框对话框
this->isLeftPressDown = false;
this->dir= NONE;
this->setMouseTracking(true); // 追踪鼠标(没有鼠标键被按下也可以追踪鼠标的位置)
5、定义一个方法 void region(const QPoint ¤tGlobalPoint);
用于判断鼠标位置,并且改变光标形状。
void MusicWidget::region(const QPoint ¤tGlobalPoint)
{
// 获取窗体在屏幕上的位置区域,topLeft为坐上角点,rightButton为右下角点
QRect rect = this->rect();
QPoint topLeft = this->mapToGlobal(rect.topLeft()); //将左上角的(0,0)转化为全局坐标
QPoint rightButton = this->mapToGlobal(rect.bottomRight());
int x = currentGlobalPoint.x(); //当前鼠标的坐标
int y = currentGlobalPoint.y();
if(((topLeft.x() + PADDING >= x) && (topLeft.x() <= x))
&& ((topLeft.y() + PADDING >= y) && (topLeft.y() <= y)))
{
// 左上角
dir = LEFTTOP;
this->setCursor(QCursor(Qt::SizeFDiagCursor)); // 设置光标形状
}else if(((x >= rightButton.x() - PADDING) && (x <= rightButton.x()))
&& ((y >= rightButton.y() - PADDING) && (y <= rightButton.y())))
{
// 右下角
dir = RIGHTBOTTOM;
this->setCursor(QCursor(Qt::SizeFDiagCursor));
}else if(((x <= topLeft.x() + PADDING) && (x >= topLeft.x()))
&& ((y >= rightButton.y() - PADDING) && (y <= rightButton.y())))
{
//左下角
dir = LEFTBOTTOM;
this->setCursor(QCursor(Qt::SizeBDiagCursor));
}else if(((x <= rightButton.x()) && (x >= rightButton.x() - PADDING))
&& ((y >= topLeft.y()) && (y <= topLeft.y() + PADDING)))
{
// 右上角
dir = RIGHTTOP;
this->setCursor(QCursor(Qt::SizeBDiagCursor));
}else if((x <= topLeft.x() + PADDING) && (x >= topLeft.x()))
{
// 左边
dir = LEFT;
this->setCursor(QCursor(Qt::SizeHorCursor));
}else if((x <= rightButton.x()) && (x >= rightButton.x() - PADDING))
{
// 右边
dir = RIGHT;
this->setCursor(QCursor(Qt::SizeHorCursor));
}else if((y >= topLeft.y()) && (y <= topLeft.y() + PADDING))
{
// 上边
dir = UP;
this->setCursor(QCursor(Qt::SizeVerCursor));
}else if((y <= rightButton.y()) && (y >= rightButton.y() - PADDING))
{
// 下边
dir = DOWN;
this->setCursor(QCursor(Qt::SizeVerCursor));
}else
{
// 默认
dir = NONE;
this->setCursor(QCursor(Qt::ArrowCursor));
}
}
6、接下来对三个鼠标事件进行重载:
mousePressEvent
//鼠标按下事件
void MusicWidget::mousePressEvent(QMouseEvent *event)
{
switch(event->button())
{
case Qt::LeftButton:
isLeftPressDown = true;
if(dir != NONE)
{
this->mouseGrabber(); //返回当前抓取鼠标输入的窗口
}
else
{
m_movePoint = event->globalPos() - this->frameGeometry().topLeft();
//globalPos()鼠标位置,topLeft()窗口左上角的位置
}
break;
case Qt::RightButton:
this->setWindowState(Qt::WindowMinimized);
break;
default:
MusicWidget::mousePressEvent(event);
}
}
mouseMoveEvent
//鼠标移动事件
void MusicWidget::mouseMoveEvent(QMouseEvent *event)
{
QPoint globalPoint = event->globalPos(); //鼠标全局坐标
QRect rect = this->rect(); //rect == QRect(0,0 680x412)
QPoint topLeft = mapToGlobal(rect.topLeft());
QPoint bottomRight = mapToGlobal(rect.bottomRight());
if (this->windowState() != Qt::WindowMaximized)
{
if(!isLeftPressDown) //没有按下左键时
{
this->region(globalPoint); //窗口大小的改变——判断鼠标位置,改变光标形状
}
else
{
if(dir != NONE)
{
QRect newRect(topLeft, bottomRight); //定义一个矩形
switch(dir)
{
case LEFT:
if(bottomRight.x() - globalPoint.x() <= this->minimumWidth())
{
newRect.setLeft(topLeft.x()); //小于界面的最小宽度时,设置为左上角横坐标为窗口x
//只改变左边界
}
else
{
newRect.setLeft(globalPoint.x());
}
break;
case RIGHT:
newRect.setWidth(globalPoint.x() - topLeft.x()); //只能改变右边界
break;
case UP:
if(bottomRight.y() - globalPoint.y() <= this->minimumHeight())
{
newRect.setY(topLeft.y());
}
else
{
newRect.setY(globalPoint.y());
}
break;
case DOWN:
newRect.setHeight(globalPoint.y() - topLeft.y());
break;
case LEFTTOP:
if(bottomRight.x() - globalPoint.x() <= this->minimumWidth())
{
newRect.setX(topLeft.x());
}
else
{
newRect.setX(globalPoint.x());
}
if(bottomRight.y() - globalPoint.y() <= this->minimumHeight())
{
newRect.setY(topLeft.y());
}
else
{
newRect.setY(globalPoint.y());
}
break;
case RIGHTTOP:
if (globalPoint.x() - topLeft.x() >= this->minimumWidth())
{
newRect.setWidth(globalPoint.x() - topLeft.x());
}
else
{
newRect.setWidth(bottomRight.x() - topLeft.x());
}
if (bottomRight.y() - globalPoint.y() >= this->minimumHeight())
{
newRect.setY(globalPoint.y());
}
else
{
newRect.setY(topLeft.y());
}
break;
case LEFTBOTTOM:
if (bottomRight.x() - globalPoint.x() >= this->minimumWidth())
{
newRect.setX(globalPoint.x());
}
else
{
newRect.setX(topLeft.x());
}
if (globalPoint.y() - topLeft.y() >= this->minimumHeight())
{
newRect.setHeight(globalPoint.y() - topLeft.y());
}
else
{
newRect.setHeight(bottomRight.y() - topLeft.y());
}
break;
case RIGHTBOTTOM:
newRect.setWidth(globalPoint.x() - topLeft.x());
newRect.setHeight(globalPoint.y() - topLeft.y());
break;
default:
break;
}
this->setGeometry(newRect);
}
else
{
move(event->globalPos() - m_movePoint); //移动窗口
event->accept();
}
}
}
}
mouseReleaseEvent
//鼠标释放事件
void MusicWidget::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
isLeftPressDown = false;
if (dir != NONE)
{
this->releaseMouse(); //释放鼠标抓取
this->setCursor(QCursor(Qt::ArrowCursor));
}
}
}