欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Qt實(shí)現(xiàn)高精度定時(shí)器

 更新時(shí)間:2022年08月04日 16:30:51   作者:_____C  
這篇文章主要為大家詳細(xì)介紹了Qt實(shí)現(xiàn)高精度定時(shí)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一般而言,Qt有兩種使用定時(shí)器的方式, QObject和QTimer,對(duì)于第一種需要重寫(xiě)timerEvent事件來(lái)實(shí)現(xiàn),第二種需要聲明一個(gè)QTimer的對(duì)象或指針,用QTimer::timeout()信號(hào)連接槽函數(shù),設(shè)置定時(shí)器類(lèi)型mTimer.setTimerType(Qt::PreciseTimer);

第一種即使高精度的定時(shí)器,保持毫秒級(jí)別;第二種粗計(jì)時(shí)器盡量將精度保持在所需間隔的5%以?xún)?nèi);第三種非常粗糙的計(jì)時(shí)器只能保持完整的秒精度.

#ifndef BACKENDPROIXY_H
#define BACKENDPROIXY_H
?
#include <QObject>
#include <QTimer>
#include <QTime>
?
class BackendProxy : public QObject
{
? ? Q_OBJECT
public:
? ? explicit BackendProxy(QObject *parent = nullptr);
?
signals:
private slots:
? ? void onTimeOut();
private:
? ? QTimer mTimer;
? ? QTime lastTime;
?
};
?
#endif // BACKENDPROIXY_H
#include "backendproixy.h"
?
#include <QDebug>
?
BackendProxy::BackendProxy(QObject *parent) : QObject(parent)
{
? ? connect(&mTimer,&QTimer::timeout,this,&BackendProxy::onTimeOut);
? ? mTimer.setTimerType(Qt::PreciseTimer);
? ? mTimer.start(50);
}
?
?
void BackendProxy::onTimeOut()
{
? ? QTime currentTime;
? ? int elapsed = 0;
? ? if(lastTime == QTime()){
? ? ? ? lastTime = QTime::currentTime();
? ? }else{
? ? ? ? currentTime = QTime::currentTime();
? ? ? ? elapsed = lastTime.msecsTo(currentTime);
? ? ? ? lastTime = QTime::currentTime();
? ? }
? ? qDebug()<<"Run.elapsed ="<<elapsed<<"ms";
}

下面分別展示三種類(lèi)型的時(shí)間間隔:

Qt::PreciseTimer:

Qt::CoarseTimer:

Qt::VeryCoarseTimer:

顯而易見(jiàn),第一種的精度最高,但偶爾也會(huì)超過(guò)20ms,對(duì)于一些實(shí)時(shí)性較高的通訊來(lái)說(shuō),還是達(dá)不到要求.使用線程加延時(shí)能達(dá)到最多正負(fù)1ms的誤差,一下輸出我使用的是10ms一個(gè)周期:

現(xiàn)在也貼上代碼:

#ifndef PERFORMANCEFREQUENCY_H
#define PERFORMANCEFREQUENCY_H
?
#include<QThread>
#include<QDebug>
#include<QUdpSocket>
#include <QHostAddress>
?
?
#define SEND_TIME 10
?
class PerformanceFrequency : public QThread
{
? ? Q_OBJECT
public:
? ? explicit PerformanceFrequency(QObject *parent = nullptr);
? ? void setThreadRunning(bool start){bRunning = start;}
? ? void appendByte(QByteArray array);
? ? void removeOneByte(QByteArray array);
signals:
? ? void sendJaguarJointControl(QByteArray ba);
? ? void heartTime(int time);
protected:
? ? void run() override;
private:
? ? QList<QByteArray> listByte;
? ? bool bRunning = true;
?
};
?
#endif // PERFORMANCEFREQUENCY_H
#include "performancefrequency.h"
#include <QTime>
?
#include <QMutex>
#include <QMutexLocker>
?
PerformanceFrequency::PerformanceFrequency(QObject *parent)
? ? : QThread(parent)
{
? ? QByteArray heart;
? ? heart[0] = 0xf0;
? ? heart[1] = heart[2] = heart[3] = heart[4] = heart[5] = heart[6] = heart[7] = ?0;
? ? listByte.append(heart);
}
?
void PerformanceFrequency::run()
{
? ? while(bRunning){
? ? ? ? QTime startTime = QTime::currentTime();
? ? ? ? msleep(SEND_TIME);
? ? ? ? for(int i = 0;i < listByte.size();i++){
? ? ? ? ? ? emit sendJaguarJointControl(listByte.at(i));
? ? ? ? }
?
? ? ? ? QTime stopTime = QTime::currentTime();
? ? ? ? int elapsed = startTime.msecsTo(stopTime);
? ? ? ? emit heartTime(elapsed);
? ? ? ? qDebug()<<"Run.elapsed ="<<elapsed<<"ms";
? ? }
}
?
void PerformanceFrequency::appendByte(QByteArray array)
{
? ? static QMutex mutex;
? ? QMutexLocker locker(&mutex);
? ? listByte.append(array);
}
void PerformanceFrequency::removeOneByte(QByteArray array)
{
? ? static QMutex mutex;
? ? QMutexLocker locker(&mutex);
? ? listByte.removeOne(array);
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論