双击时不运行单击事件——加延时判断

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

  1. //tqt.h  
  2. #ifndef TQT_H_  
  3. #define TQT_H_  
  4.   
  5. #include <QtGui>  
  6. #include <QtCore>  
  7.   
  8. class ClickedLabel : public QLabel  
  9. {  
  10.     Q_OBJECT  
  11. private:  
  12.     int press;  
  13.     //QTimer *timer;  
  14. protected:  
  15.     void mousePressEvent(QMouseEvent *event);  
  16. public:  
  17.     ClickedLabel(QLabel *parent = 0);  
  18.     ~ClickedLabel();  
  19.     private slots:  
  20.         void SingleClicked();  
  21. };  
  22.   
  23.   
  24. #endif  
  25.   
  26.   
  27. #include "tqt.h"  
  28.   
  29. ClickedLabel::ClickedLabel(QLabel *parent /* = 0 */)  
  30. : QLabel(parent)  
  31. {  
  32.     press = 0;  
  33.     setText("Please Click ME~~~");  
  34.     resize(200, 200);  
  35. }  
  36.   
  37. ClickedLabel::~ClickedLabel()  
  38. {  
  39.   
  40. }  
  41.   
  42. void ClickedLabel::mousePressEvent(QMouseEvent *event)  
  43. {  
  44.     press++;  
  45.     if(1 == press)  
  46.         QTimer::singleShot(300, this, SLOT(SingleClicked()));  
  47. }  
  48.   
  49. void ClickedLabel::SingleClicked()  
  50. {  
  51.     if(1 == press)  
  52.         QMessageBox::information(this, tr("OK"), tr("Signal Clicked"));  
  53.     else  
  54.         QMessageBox::information(this, tr("OK"), tr("Double Clicked"));  
  55.     press = 0;  
  56. }  
  57.   
  58.   
  59. //main.cpp  
  60. #include "tqt.h"  
  61.   
  62. int main(int argc, char **argv)  
  63. {  
  64.     QApplication app(argc, argv);  
  65.     ClickedLabel *label = new ClickedLabel;  
  66.     label->show();  
  67.     return app.exec();  
  68. }  

http://blog.csdn.net/small_qch/article/details/6742011

原文地址:https://www.cnblogs.com/findumars/p/4993548.html