我是靠谱客的博主 高高缘分,最近开发中收集的这篇文章主要介绍Qt 检测PushButton 按钮长按 按下和弹起的类,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#ifndef SPMLPPUSHBUTTON_H
#define SPMLPPUSHBUTTON_H

#include <QPushButton>
#include <time.h>
#include <sys/time.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
class spmLPPushButton : public QPushButton
{

Q_OBJECT
public:

explicit spmLPPushButton(QObject *parent = 0, long delay=1000);

~spmLPPushButton();

struct timeval *getCurrentTm( struct timeval *ptv );

double getTimeSecDist(
struct timeval * tv_beg,
struct timeval * tv_end );

void setDetectDelay(long);

signals:

void longPressedDown();

void longPressedUp();

public slots:

void pbpressed();

void pbreleased();

private:

struct timeval *m_tvPressed;

struct timeval *m_tvReleased;

bool m_bInLongPressed;

long m_iDelay;
};

#endif // SPMLPPUSHBUTTON_H


#include "spmLPPushButton.h"
#include <SpmGui.h>
spmLPPushButton::spmLPPushButton(QObject *parent, long delay) :

QPushButton((QWidget *)parent),

m_tvPressed(NULL),

m_tvReleased(NULL),

m_bInLongPressed(FALSE),

m_iDelay( 1000 )
{

setAutoRepeat(true);

if(delay > 100)

{

setAutoRepeatDelay(delay);

m_iDelay = delay;

}

else

setAutoRepeatDelay(1000);

setAutoRepeatInterval(9999999); // Do not Do Auto Repeat Clicked

connect( this, SIGNAL( pressed() ), this, SLOT( pbpressed() ) );

connect( this, SIGNAL( released() ), this, SLOT( pbreleased() ) );

m_tvReleased = getCurrentTm(m_tvReleased);

m_tvPressed = getCurrentTm(m_tvPressed);
}
void spmLPPushButton::setDetectDelay(long delay)
{

if(delay > 100)

{

setAutoRepeatDelay(delay);

m_iDelay = delay;

}
}
struct timeval *spmLPPushButton::getCurrentTm( struct timeval *ptv )
{

if(ptv == NULL)

{

try

{

ptv = new (struct timeval);

}

catch(...)

{

std::cerr<<__FILE__<<"Error of New operation.n"<<std::endl;

exit(1);

}

}

memset( ptv, 0, sizeof(struct timeval) );

gettimeofday( ptv, NULL );

//time_t t;

//t = time(&t);

return ptv;
}
double spmLPPushButton::getTimeSecDist(
struct timeval * tv_beg,
struct timeval * tv_end )
{

double retVal = 0.000;

if( ( tv_beg == NULL ) ||

( tv_end == NULL ) )

{

printf( "@@@ Program takes error! Null timevaln" );

return 0;

}

long tdist = (long)(tv_end->tv_sec - tv_beg->tv_sec); //(long) difftime( tv_beg->tv_sec, tv_end->tv_sec );

long usecdst = tv_end->tv_usec - tv_beg->tv_usec;

retVal =
double(tdist ) +
double(usecdst)/1000000;

printf( "@@@ Program takes %ld+%ldu ret:%.3fseconds.n", tdist, usecdst, retVal );

return retVal;
}
void spmLPPushButton::pbpressed()
{

// std::cout<<ptt<<"@ Clicked On LongP Btn Pressed"<<std::endl;

m_tvPressed = getCurrentTm(m_tvPressed);
}
void spmLPPushButton::pbreleased()
{

// std::cout<<ptt<<"@ Clicked On LongP Btn Released"<<std::endl;

if( m_bInLongPressed )

{

emit longPressedUp();

std::cout<<"@@ LongP Detected Up"<<std::endl;

m_bInLongPressed = FALSE;

}

else

{

m_tvReleased = getCurrentTm(m_tvReleased);

if( getTimeSecDist( m_tvPressed, m_tvReleased ) >= 0.999 ) // The Long Press Detected Step0 1 is the seconds Delay

{

std::cout<<"@@ LongP Detected Down"<<std::endl;

emit longPressedDown();

m_bInLongPressed = TRUE;

}

else

{

}

}
}
spmLPPushButton::~spmLPPushButton()
{

delete m_tvReleased;

delete m_tvPressed;
}

最后

以上就是高高缘分为你收集整理的Qt 检测PushButton 按钮长按 按下和弹起的类的全部内容,希望文章能够帮你解决Qt 检测PushButton 按钮长按 按下和弹起的类所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部