Qt鼠标拖拽窗口移动事件

QT中实现窗口的拖拽主要利用的是三个事件的重写

mousePressEvent        

重写后检测到鼠标按下左键时记录按下的point的坐标以及拖拽的窗口的坐标point

mouseMoveEvent

重写后判断若是在拖动状态,记录鼠标移动的位置差,即目前的位置减去鼠标原来的位置,并将窗口move到窗口的坐标point加上目前的位置差的最新位置

mouseReleaseEvent

重写后在鼠标左键放下后判断拖拽结束

.h文件

#ifndef DRAGWIDGET_H
#define DRAGWIDGET_H

#include <QtWidgets/QWidget>
#include "ui_dragwidget.h"
#include <QMouseEvent>

class DragWidget : public QWidget
{
    Q_OBJECT

public:
    DragWidget(QWidget *parent = Q_NULLPTR);

protected:
    void mousePressEvent(QMouseEvent* event);
    void mouseMoveEvent(QMouseEvent* event);
    void mouseReleaseEvent(QMouseEvent* event);

private:
    Ui::DragWidgetClass ui;

    bool is_drag_ = false;

    QPoint mouse_start_point_;
    QPoint window_start_point_;
};

#endif //DRAGWIDGET_H

.cpp文件

#include "dragwidget.h"

DragWidget::DragWidget(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);
}

void DragWidget::mousePressEvent(QMouseEvent* event)
{
    if (event->button() == Qt::LeftButton)
    {
        is_drag_ = true;
        //获得鼠标的初始位置
        mouse_start_point_ = event->globalPos();
        //获得窗口的初始位置
        window_start_point_ = this->frameGeometry().topLeft();
    }
}

void DragWidget::mouseMoveEvent(QMouseEvent* event)
{
    //判断是否在拖拽移动
    if (is_drag_)
    {
        //获得鼠标移动的距离
        QPoint move_distance = event->globalPos() - mouse_start_point_;
        //改变窗口的位置
        this->move(window_start_point_ + move_distance);
    }
}

void DragWidget::mouseReleaseEvent(QMouseEvent* event)
{
    //放下左键即停止移动
    if (event->button() == Qt::LeftButton)
    {
        is_drag_ = false;
    }
}

  • 8
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您讲解如何在QT中实现自定义窗口拖拽移动。 首先,我们需要重写QWidget的mousePressEvent和mouseMoveEvent两个事件函数。 在mousePressEvent中,我们需要记录下鼠标按下时的坐标和窗口左上角的坐标,以便计算窗口移动的偏移量。 在mouseMoveEvent中,我们需要计算鼠标移动的偏移量,并将窗口的位置进行相应的调整。 以下是示例代码: ```cpp void MyWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { m_dragPosition = event->globalPos() - frameGeometry().topLeft(); } } void MyWindow::mouseMoveEvent(QMouseEvent *event) { if (event->buttons() & Qt::LeftButton) { QPoint newPos = event->globalPos() - m_dragPosition; move(newPos); } } ``` 在上面的代码中,m_dragPosition是一个QPoint类型的成员变量,用于记录鼠标按下时的坐标和窗口左上角的坐标的差值。 最后,我们需要在窗口的构造函数中设置窗口的样式为Qt::FramelessWindowHint,以去掉窗口的边框和标题栏。 完整的示例代码如下: ```cpp #include "mywindow.h" #include <QMouseEvent> MyWindow::MyWindow(QWidget *parent) : QWidget(parent) { setFixedSize(400, 300); setWindowFlags(Qt::FramelessWindowHint); } void MyWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { m_dragPosition = event->globalPos() - frameGeometry().topLeft(); } } void MyWindow::mouseMoveEvent(QMouseEvent *event) { if (event->buttons() & Qt::LeftButton) { QPoint newPos = event->globalPos() - m_dragPosition; move(newPos); } } ``` 希望这个简单的示例对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值