头文件—LabelRealClick.h
#ifndef LABELREALCLICK_H
#define LABELREALCLICK_H
#include <QLabel>
#include <QWidget>
#include <QTimer>
#include <QMouseEvent>
#include <QDebug>
#define cout qDebug() << "[" << __FILE__ << __LINE__ << "]:"
class LabelRealClick:public QLabel
{
Q_OBJECT
public:
LabelRealClick(QWidget *pParent = nullptr);
protected:
virtual void mousePressEvent(QMouseEvent *event) override;
virtual void mouseReleaseEvent(QMouseEvent *event) override;
virtual void mouseDoubleClickEvent(QMouseEvent *event) override;
signals:
void RClicked();
void DoubleClicked();
void LClicked();
public slots:
void OnTimeout();
private:
QTimer *m_timer;
bool m_isLPressed;
};
#endif // LABELREALCLICK_H
CPP文件—LabelRealClick.cpp
#include "LabelRealClick.h"
LabelRealClick::LabelRealClick(QWidget *pParent):QLabel(pParent)
{
this->m_timer = new QTimer(this);
connect(m_timer, &QTimer::timeout, this, [=](){OnTimeout();} );
m_isLPressed = false;
}
void LabelRealClick::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
m_isLPressed = true;
}
}
void LabelRealClick::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
if (m_isLPressed)
{
m_timer->start(180);
}
m_isLPressed = false;
}
else if (event->button() == Qt::RightButton)
{
emit RClicked();
cout << "RClicked";
}
}
void LabelRealClick::mouseDoubleClickEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
m_timer->stop();
emit DoubleClicked();
cout << "DoubleClicked";
}
}
void LabelRealClick::OnTimeout()
{
m_timer->stop();
emit LClicked();
cout << "pressLeftClicked";
}
原文链接:
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_43610939/article/details/112599178
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/279321.html