当前位置: 首页 > article >正文

Qt中的事件

写一个 可以拖动的按钮

DraggablePushButton.h 头文件

#ifndef DRAGGABLEPUSHBUTTON_H
#define DRAGGABLEPUSHBUTTON_H

#include <QPushButton>
#include <QMouseEvent>

class DraggablePushButton : public QPushButton
{
    Q_OBJECT

public:
    explicit DraggablePushButton(QWidget *parent = nullptr);

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

private:
    void showDialog();

private:
    QPoint m_pressPosition;  // 鼠标按下位置
    int m_pressTime;         // 鼠标按下时间戳
    bool m_isDragging;       // 是否是拖动操作
};

#endif // DRAGGABLEPUSHBUTTON_H

实现  DraggablePushButton.cpp

#include "DraggablePushButton.h"
#include <QMessageBox>
#include <QApplication>
#include <QDebug>

DraggablePushButton::DraggablePushButton(QWidget *parent)
    : QPushButton(parent), m_isDragging(false)
{
}

void DraggablePushButton::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        m_pressPosition = event->pos();  // 记录鼠标按下的位置
        m_pressTime = event->timestamp();  // 记录鼠标按下的时间

        // 阻止默认的点击行为
        event->accept();
    }
    else {
        QPushButton::mousePressEvent(event);  // 其它鼠标按下事件处理
    }
}

void DraggablePushButton::mouseMoveEvent(QMouseEvent *event)
{
    if (m_isDragging) {
        // 鼠标拖动时更新按钮位置
        QPoint delta = event->pos() - m_pressPosition;
        move(pos() + delta);  // 移动按钮
        event->accept();
    }
    else {
        // 判断是否是拖动操作
        int distance = (event->pos() - m_pressPosition).manhattanLength();
        if (distance > QApplication::startDragDistance()) {
            m_isDragging = true;  // 开始拖动
        }
    }
}

void DraggablePushButton::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        int releaseTime = event->timestamp();  // 记录释放的时间
        int timeDiff = releaseTime - m_pressTime;  // 计算按下和释放的时间差

        if (m_isDragging) {
            // 如果是拖动操作
            qDebug() << "Dragged";
        } else {
            // 如果按下和释放的时间差较短,并且鼠标位置没有变化,判定为双击
            if (timeDiff < QApplication::doubleClickInterval()) {
                qDebug() << "Double Clicked";
                showDialog();  // 弹出对话框
            } else {
                qDebug() << "Single Clicked";
            }
        }

        m_isDragging = false;  // 重置拖动标志
        event->accept();
    }
    else {
        QPushButton::mouseReleaseEvent(event);  // 其它鼠标释放事件处理
    }
}

void DraggablePushButton::showDialog()
{
    // 弹出对话框
    QMessageBox::information(this, "Double Clicked", "You double-clicked the button!");
}

 


http://www.kler.cn/a/544351.html

相关文章:

  • 分布式系统中Session黏滞
  • Ubuntu安装geteck/jetlinks实战:源码启动
  • 无人机 ,遥控器与接收机之前的通信
  • 《一文讲透》第4期:KWDB 数据库运维(7)—— 集群规划
  • Ubuntu 上安装 Elasticsearch 7.6.0
  • 蓝桥杯试题:归并排序
  • 在freertos中,中断优先级和任务优先级之间的关系和使用方法
  • github不翻墙就可以访问
  • python-leetcode-数组中的第K个最大元素
  • 【Java八股文】02-Java集合面试篇
  • 蓝桥杯-洛谷刷题-day5(C++)(为未完成)
  • Eclipse 插件开发相关概念
  • Django REST Framework:如何获取序列化后的ID
  • Matlab工具包安装
  • 《战神:诸神黄昏》游戏闪退后提示弹窗“d3dx9_43.dll缺失”“找不到d3dx11_43.d”该怎么处理?
  • Linux云服务器sftp服务器如何监控
  • 企业文件安全:跨部门协作中的数据共享与安全管理
  • Flink (十七) :Table API SQL (五) 时区
  • SpringBoot:Could not autowire. No beans of ‘JdbcTemplate“ type found 问题处理
  • Java面试题总结 - Java多线程篇(附答案)
  • Ansible中Playbook的逻辑控制语句-when
  • element-ui时间组件同一个月内选择/30天内选择
  • 理解Unity中的ExecuteInEditMode与ExecuteAlways
  • Linux的0号进程、1号进程、2号进程
  • React进阶之React RouterSSR
  • redis之服务端