重写mouseEvent 事件 怎么实现自定义的无边框窗口移动

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        m_Drag = true;
        m_DragPosition = event->globalPos() - this->pos();
        event->accept();
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if (m_Drag && (event->buttons() && Qt::LeftButton)) {
        move(event->globalPos() - m_DragPosition);
        event->accept();
    }
}

void MainWindow::mouseReleaseEvent(QMouseEvent *)
{
    m_Drag = false;
}
 bool m_Drag;
    QPoint m_DragPosition;

在网上有许多例子 但是还是不行,在你的窗口类中 添加这些代码还有函数声明为protected
还要添加变量
 bool m_Drag;
QPoint m_DragPosition; 因为函数有未命名的变量所以 添加
最后加头文件 #include<QMouseEvent> 

原文地址:https://www.cnblogs.com/IamQtCreator/p/4562228.html