Qt 把连续两次单击当成双击

方法1: 主要通过计时器,在一段时间内的连续两次单击,触发双击事件

 


void init()
{
  m_nClickCount = 0;
  m_timer = new QTimer(this);
  connect(m_timer, SIGNAL(timeout()), this, SLOT(onKeyOneClick()));
}

void
mouseReleaseEvent(QMouseEvent*) { if (!m_timer->isActive()) //计数期间,则不重新开始 { m_timer->start(300);//300ms是判断双击的时间间隔,不唯一,可根据实际情况改变 } m_nClickCount += 1;
  //300ms 内,两次单击则触发双击效果 ,然后停止计数
if(m_nClickCount == 2) { emit sigClose(); m_nClickCount = 0; m_timer->stop(); } } void onKeyOneClick() { m_timer->stop(); m_nClickCount = 0; }

 

方法2:

void MainWindow::mouseReleaseEvent(QMouseEvent *)
{
    static int num = 0;
    static double time_Start = 0 ;
    static double time_End = 0 ;

    num+=1;
    if(num == 1){
        time_Start = (double)clock();
        qDebug()<< "num == 1";
        if((time_Start - time_End) < 400){//连续快速两次单击
            //
            qDebug()<< "click quickly " << time_Start - time_End ;
        }
    }
    if(num == 2){
        num = 0;
        qDebug()<< "num == 2";
        time_End = (double)clock();
        if((time_End - time_Start) < 400){//连续快速两次单击
            //
            qDebug()<< "click quickly " << time_End - time_Start ;
        }
    }
}

 方法3:

void MainWindow::mouseReleaseEvent(QMouseEvent *)
{
    static int num = 0;
    static  struct timeval tpstart,tpend;
    float timeuse;

    num+=1;
    if(num == 1){
         gettimeofday(&tpstart,NULL);
//        timeuse=(1000000*(tpend.tv_sec-tpstart.tv_sec) + tpend.tv_usec-tpstart.tv_usec)/1000.0;
//        qDebug()<< "num == 1 " << timeuse ;
//        if(timeuse< 400){//连续快速两次单击
//            //
//            qDebug()<< "click quickly "  ;
//        }
    }
    if(num == 2){
        num = 0;
        gettimeofday(&tpend,NULL);
        timeuse=(1000000*(tpend.tv_sec-tpstart.tv_sec) + tpend.tv_usec-tpstart.tv_usec)/1000.0;
        qDebug()<< "num == 2 "  << timeuse;
        if(timeuse < 400){//连续快速两次单击
            //
            qDebug()<< "click quickly " ;
        }
    }
}

 

posted @ 2019-09-20 14:00  cicero  阅读(1213)  评论(0编辑  收藏  举报