Qt实现自定义窗口拖动

1. widget重新三个函数

void mousePressEvent(QMouseEvent* event) override;
void mouseMoveEvent(QMouseEvent* event) override;    
void mouseReleaseEvent(QMouseEvent* event)  override;

2. 实现如下

void KMainWidget::mousePressEvent(QMouseEvent* event)
{
    Q_UNUSED(event);
    m_isPress = true;
    m_startPos = event->globalPos() - this->frameGeometry().topLeft();
}

void KMainWidget::mouseMoveEvent(QMouseEvent* event)
{
    if (m_isPress)
    {
        move(this->mapToGlobal(event->pos() - m_startPos));
    }
}

void KMainWidget::mouseReleaseEvent(QMouseEvent* event)
{
    m_isPress = false;
}
posted @ 2022-09-26 10:42  进击的汪sir  阅读(216)  评论(0编辑  收藏  举报