Qt中鼠标的双击事件和单击事件的实现方式

背景

同一个部件既要响应鼠标单击事件又要响应双击事件,而且两者响应的动作没有交集,跟不存在包含关系(如果双击事件动作包含单击事件的动作,那么只需要将双击事件特有的部分放到mouseDoubleClickEvent中去处理就好了)。问题点:两者响应的方式完全不同,举例:对一个QPushButton控件响应单击事件和双击事件,两个事件都会触发弹窗,两个不同的弹窗。

原理

Qt中,在双击事件mouseDoubleClickEvent中会触发单击事件mousePressEvent事件,原因是如下:
(1)、鼠标 按下->弹起 ,一个单击信号就发射了;
(2)、在单击后的一段(很短)的时间内,鼠标 按下->弹起,一个双击信号发射;

实现

  1. 方式一(思路见原文
    使用单击事件的处理函数mousePressEvent和双击事件的处理函数mouseDoubleClickEvent,外加一个定时器来区分单击事件
#ifndef CBUTTON_H
#define CBUTTON_H
#include <QPushButton>
#include <QTimer>

class CButton : public QPushButton
{
    Q_OBJECT
public:
    explicit CButton(QWidget *pParent);

private:
    void mousePressEvent(QMouseEvent *e);
    void mouseDoubleClickEvent(QMouseEvent *event);

private slots:
    void slotTimerTimeOut();
    void mouseClicked();

private:
    QTimer m_cTimer;
};

#endif // CBUTTON_H
#include "cbutton.h"
#include <QDebug>

CButton::CButton(QWidget *pParent):QPushButton(pParent)
{
    connect(&m_cTimer,SIGNAL(timeout()),this,SLOT(slotTimerTimeOut()));
}

void CButton::mousePressEvent(QMouseEvent *e)
{
    qDebug()<<"CButton::mousePressEvent"<<endl;
    m_cTimer.start(200);
}

void CButton::mouseDoubleClickEvent(QMouseEvent *event)
{
    m_cTimer.stop();
    qDebug()<<"CButton::mouseDoubleClickEvent"<<endl;
}

void CButton::slotTimerTimeOut()
{
    m_cTimer.stop();
    mouseClicked();
}

void CButton::mouseClicked()
{
    qDebug()<<"CButton::mouseClicked"<<endl;
}

2.方式二 (思路见原文
 使用单击事件信号,增加一个变量和一个定时器来区分单击事件、双击事件

#ifndef CWINBUTTON_H
#define CWINBUTTON_H

#include <QPushButton>
#include <QTimer>

class CWinButton : public QPushButton
{
    Q_OBJECT
public:
    explicit CWinButton(QWidget *pParent=nullptr);

private slots:
    void slotTimerTimeOut();
    void clicked();

private:
    int m_nClickTimes;
    QTimer m_cTimer;
};

#endif // CWINBUTTON_H
#include "cwinbutton.h"
#include <QDebug>

CWinButton::CWinButton(QWidget *pParent):QPushButton(pParent)
{
    m_nClickTimes =0;
    connect(&m_cTimer,SIGNAL(timeout()),this,SLOT(slotTimerTimeOut()));
    connect(this,SIGNAL(clicked(bool)),this,SLOT(clicked()));
}

void CWinButton::slotTimerTimeOut()
{
    qDebug()<<"CWinButton::slotTimerTimeOut"<<endl;
    m_cTimer.stop();
    if(1==m_nClickTimes){
        qDebug()<<"click event"<<endl;
        //TODO Click respond.
    }else if(2==m_nClickTimes){
        qDebug()<<"double click event"<<endl;
        //TODO Double click respond.
    }
    m_nClickTimes=0;
}

void CWinButton::clicked()
{
    qDebug()<<"CWinButton::clicked"<<endl;
    m_nClickTimes++;
    m_cTimer.start(200);
}

总结

方式一中双击事件触发的点击过程中,会引起单击事件,无法杜绝。方式二的实现清楚的区分开了两种事件。在两种事件响应互不关联的情况下,这种方式是最佳选择。

Qt,可以通过以下两种方式来区分鼠标单击双击操作: 1. 使用QMouseEvent事件鼠标按下并释放时,会触发QMouseEvent事件。在处理QMouseEvent事件时,我们可以通过判断事件类型和时间间隔来区分单击双击操作。例如,我们可以使用QTime类记录上一次鼠标按下的时间,并在本次鼠标按下时计算时间间隔。如果时间间隔小于某个值(例如300ms),则认为是双击操作;否则认为是单击操作。 示例代码: ``` void Widget::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { static QTime lastClickTime; int interval = lastClickTime.elapsed(); lastClickTime.start(); if (interval < QApplication::doubleClickInterval()) { qDebug() << "double click"; } else { qDebug() << "single click"; } } } ``` 2. 使用QShortcut快捷键 QShortcut是Qt提供的一种快捷键机制。我们可以通过为Widget对象添加QShortcut对象来捕获鼠标单击双击事件。在QShortcut对象的构造函数,我们可以设置快捷键的组合键和触发方式。例如,我们可以设置快捷键为鼠标左键,并设置触发方式单击双击。在处理QShortcut对象的activated()信号时,我们就可以区分单击双击操作。 示例代码: ``` void Widget::initShortcuts() { QShortcut *singleClickShortcut = new QShortcut(QKeySequence(Qt::LeftButton), this); connect(singleClickShortcut, &QShortcut::activated, this, &Widget::handleSingleClick); QShortcut *doubleClickShortcut = new QShortcut(QKeySequence(Qt::LeftButton | Qt::DoubleClick), this); connect(doubleClickShortcut, &QShortcut::activated, this, &Widget::handleDoubleClick); } void Widget::handleSingleClick() { qDebug() << "single click"; } void Widget::handleDoubleClick() { qDebug() << "double click"; } ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值