1.头文件下 .h
#include <QMouseEvent> //引入鼠标事件
#define PADDING 5 鼠标拖拽的宽度
enum Direction { UP=0, DOWN=1, LEFT, RIGHT, LEFTTOP, LEFTBOTTOM, RIGHTBOTTOM, RIGHTTOP, NONE };
光标的位置
public:
void region(const QPoint ¤tGlobalPoint); //鼠标的位置,改变光标
private:
QPoint m_movePoint; //鼠标的位置
bool isLeftPressDown = false; // 判断左键是否按下
Direction dir = NONE; //默认
protected:
//鼠标按下移动及释放事件
void mousePressEvent(QMouseEvent *event); //按下
void mouseMoveEvent(QMouseEvent *event); //移动
void mouseReleaseEvent(QMouseEvent *event); //释放
void mouseDoubleClickEvent(QMouseEvent *e); //双击
源文件 .cpp
构造函数内
this->setMouseTracking(true); //监听鼠标
双击方法缩小
//双击放大缩小
void QUserTableWidget::mouseDoubleClickEvent(QMouseEvent *e)
{
if(this->isMaximized())
{
ui->title_max->setStyleSheet("background:url(:/img/max.png) center no-repeat;");//这个只是我切换图标
this->showNormal();
}
else
{
ui->title_max->setStyleSheet("background:url(:/img/max2.png) center no-repeat;");//这个只是我切换图标
this->showMaximized();
}
}
定义鼠标窗口脱拽的范围位置
void QUserTableWidget::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));
}
}
鼠标按下事件
//鼠标按下
void QUserTableWidget::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:
QUserTableWidget::mousePressEvent(event);
}
}
鼠标移动事件
//鼠标移动
void QUserTableWidget::mouseMoveEvent(QMouseEvent *event)
{
QPoint globalPoint = event->globalPos(); //鼠标全局坐标
QRect rect = this->rect(); //rect == QRect(0,0 1280x720)
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); //定义一个矩形 拖动后最大1000*1618
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();
}
}
}
}
鼠标释放事件
//鼠标释放
void QUserTableWidget::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
isLeftPressDown = false;
if (dir != NONE)
{
this->releaseMouse(); //释放鼠标抓取
this->setCursor(QCursor(Qt::ArrowCursor));
}
}
}
点击最小化按钮
this->setWindowState(Qt::WindowMinimized);
点击关闭程序按钮
QApplication* app;
app->exit(0);
点击最大化按钮
if(this->isMaximized()){
//若是最大化就还原
this->showNormal();
ui->title_max->setStyleSheet("background:url(:/img/max.png) center no-repeat;");
}else{
//若不是最大化就最大化
this->showMaximized();
ui->title_max->setStyleSheet("background:url(:/img/max2.png) center no-repeat;");
}
至此结束,有问题留言