即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

qt 实现、区分鼠标单击,双击事件

编程语言 shaosheng2008 15℃ 0评论

qt 只实现了鼠标左键的双击事件,同时会触发mousePressEvent事件,那么怎么区分鼠标的单击,双击呢??进入正题,请见以下源码,或者下载资源:http://download.csdn.net/detail/shaosheng2008/9823796

/**************************************/

//头文件: double_single_clicked.h

/*************************************/

#ifndef DOUBLE_SINGLE_CLICKED_H


#define DOUBLE_SINGLE_CLICKED_H








#include








class QTimer;


class QLabel;








class DoubleSingleClicked : public QWidget


{


Q_OBJECT








public:


DoubleSingleClicked(QWidget *parent = 0);


~DoubleSingleClicked();








protected:


void mouseDoubleClickEvent(QMouseEvent *e);


void mousePressEvent(QMouseEvent *);








private slots:


void slotSingleClicked();


private:


QTimer
*m_clickedTimer = nullptr;


QLabel
*m_label = nullptr;


};





#endif // DOUBLE_SINGLE_CLICKED_H



/***********************************************************************************************************/

//.cpp文件:double_single_clicked.cpp

/**********************************************************************************************************/

#include “double_single_clicked.h”


#include


#include


#include








DoubleSingleClicked::DoubleSingleClicked(QWidget *parent)


: QWidget(parent)


{


setFixedSize(600, 400);


m_label = new QLabel(this);


m_label->move(15, 50);


m_label->setText(QStringLiteral(“区分鼠标单击,双击事件!”));








m_clickedTimer = new QTimer(this);


connect(m_clickedTimer, SIGNAL(timeout()), this, SLOT(slotSingleClicked()));


}








DoubleSingleClicked::~DoubleSingleClicked()


{








}








void DoubleSingleClicked::mouseDoubleClickEvent(QMouseEvent *e)


{


if (m_clickedTimer->isActive())


m_clickedTimer->stop();


m_label->setText(QStringLiteral(“鼠标双击!!”));


}








void DoubleSingleClicked::mousePressEvent(QMouseEvent *e)


{


if (Qt::LeftButton == e->button())


{


m_clickedTimer->start(300);


}


}








void DoubleSingleClicked::slotSingleClicked()


{


m_label->setText(QStringLiteral(“鼠标单击!!”));


if (m_clickedTimer->isActive())


m_clickedTimer->stop();


}


欢迎加入Q群(565993954)进行技术交流、讨论!!


转载请注明:CodingBlog » qt 实现、区分鼠标单击,双击事件

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情