头文件:
cpp
//鼠标事件 用于拖动窗口
//一下三个函数都是QWidget的可重载成员函数
void mouseMoveEvent(QMouseEvent* ev) override;
void mousePressEvent(QMouseEvent* ev) override;
void mouseReleaseEvent(QMouseEvent* ev) override;
源文件:
cpp
/
/// 鼠标拖动窗口
static bool mouse_press = false;
static QPoint mouse_point;//鼠标的坐标
void XViewer::mouseMoveEvent(QMouseEvent* ev)
{
if (!mouse_press)
{
QWidget::mouseMoveEvent(ev);
return;
}
this->move(ev->globalPos() - mouse_point);//鼠标在移动的过程中执行
}
void XViewer::mousePressEvent(QMouseEvent* ev)
{
if (ev->button() == Qt::LeftButton)//左键按下的时候
{
mouse_press = true;//此时可移动
mouse_point = ev->pos();
}
}
void XViewer::mouseReleaseEvent(QMouseEvent* ev)
{
mouse_press = false;
}
结果表明我们可以进行移动窗口了