双击时不运行单击事件

思路很简单,就是加一个延时,判断在特定时间内是否有第二个鼠标事件


//tqt.h
#ifndef TQT_H_
#define TQT_H_

#include <QtGui>
#include <QtCore>

class ClickedLabel : public QLabel
{
	Q_OBJECT
private:
	int press;
	//QTimer *timer;
protected:
	void mousePressEvent(QMouseEvent *event);
public:
	ClickedLabel(QLabel *parent = 0);
	~ClickedLabel();
	private slots:
		void SingleClicked();
};


#endif


#include "tqt.h"

ClickedLabel::ClickedLabel(QLabel *parent /* = 0 */)
: QLabel(parent)
{
	press = 0;
	setText("Please Click ME~~~");
	resize(200, 200);
}

ClickedLabel::~ClickedLabel()
{

}

void ClickedLabel::mousePressEvent(QMouseEvent *event)
{
	press++;
	if(1 == press)
		QTimer::singleShot(300, this, SLOT(SingleClicked()));
}

void ClickedLabel::SingleClicked()
{
	if(1 == press)
		QMessageBox::information(this, tr("OK"), tr("Signal Clicked"));
	else
		QMessageBox::information(this, tr("OK"), tr("Double Clicked"));
	press = 0;
}


//main.cpp
#include "tqt.h"

int main(int argc, char **argv)
{
	QApplication app(argc, argv);
	ClickedLabel *label = new ClickedLabel;
	label->show();
	return app.exec();
}


你可能感兴趣的:(Signal)