添加鼠标点击事件
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
QPoint m_startPoint;
QPoint m_windowPoint;
头文件
#include <QtWidgets/QWidget>
#include <QMouseEvent>
判断是否点击移动
void MainWindow::mousePressEvent(QMouseEvent *event)//窗口移动事件
{
//当鼠标左键点击时.
if (event->button() == Qt::LeftButton)
{
m_move = true;
//记录鼠标的世界坐标.
m_startPoint = event->globalPos();
//记录窗体的世界坐标.
m_windowPoint = this->frameGeometry().topLeft();
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)//窗口移动事件
{
if (event->buttons() & Qt::LeftButton)
{
//移动中的鼠标位置相对于初始位置的相对位置.
QPoint relativePos = event->globalPos() - m_startPoint;
//然后移动窗体即可.
this->move(m_windowPoint + relativePos );
}
}
}