QT 中的延时函数和精准定时器的用法

一、QT中的延时函数比较常用的三种方法

第一种:

void MainWindow::Delay(int msec)
{   // 这个最准
    /*非阻塞方式延时,现在很多人推荐的方法*/
    QEventLoop loop;
    QTimer::singleShot(msec, &loop, SLOT(quit()));
    loop.exec();
}

第二种:

void MainWindow::sleep(unsigned int msec)
{
    //非阻塞方式延时,不会卡住主界面,据说可能有问题
    QTime endTime = QTime::currentTime().addMSecs(msec);
    while (QTime::currentTime() < endTime) {
        QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
    }
}

第三种:

void MainWindow::Deferred(int msec)
{   // 这个最准
    QTimer timer;
    timer.setTimerType(Qt::PreciseTimer);
    timer.start(msec);
    while(timer.remainingTime() > 0)
        QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
}

二、定时器的两种写法

QT有两种方法设置定时器:

1. QObject
2. QTimer

QT的定时器,有两种机制:
1. 当时间小于20ms时且系统支持多媒体计时器,QT将采用多媒体计时器
2. 否则,QT使用普通定时器

一、调用系统支持的多媒体计时器

1.在.pro里面添加:

LIBS += -lwinmm

2.创建一个新的时间类mmtimer。mmtimer.h的代码如下:

#ifndef MMTIMER_H
#define MMTIMER_H

#include 
#include 
#include 

class MMTimer: public QObject
{
    Q_OBJECT
public:
    explicit MMTimer(int interval, QObject *parent = 0);
    ~MMTimer();
signals:
    void timeout();
 public slots:
    void start();
    void stop();

friend void WINAPI CALLBACK mmtimer_proc(uint, uint, DWORD_PTR, DWORD_PTR, DWORD_PTR);

private:
    int m_interval;
    int m_id;
};

#endif // MMTIMER_H

3.mmtimer.cpp的代码如下:

#include "mmtimer.h"
#include 
#ifdef __MINGW32__ //w32api bug
#define TIME_KILL_SYNCHRONOUS 0x0100
#endif

void WINAPI CALLBACK mmtimer_proc(uint timerId, uint, DWORD_PTR user, DWORD_PTR, DWORD_PTR)
{
    //timerId = 0;
    MMTimer *t = reinterpret_cast(user);
    emit t->timeout();
}

MMTimer::MMTimer(int interval, QObject *parent) :QObject(parent),m_interval(interval),m_id(0)
{

}

MMTimer::~MMTimer()
{
    stop();
}

void MMTimer::start()
{
    m_id = timeSetEvent(m_interval, 1, mmtimer_proc, (DWORD_PTR)this,
                 TIME_CALLBACK_FUNCTION | TIME_PERIODIC | TIME_KILL_SYNCHRONOUS);
}

void MMTimer::stop()
{
    if (m_id){
        timeKillEvent(m_id);
        m_id = 0;
    }
}

4.在主函数里面的使用,mainwindow.cpp的代码如下:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    SetTimer = new MMTimer(1000,0);//设置定时22ms
    connect(SetTimer,SIGNAL(timeout()),this,SLOT(handleTimeout()));//定时器时间到,运行超时处理函数handleTimeout()
    SetTimer->start();//定时器开始工作
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::handleTimeout()
{
    //运行超时处理事件
    qDebug()<<"1";
}

5.mainwindow.h的代码如下

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include "mmtimer.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
public slots:
    void handleTimeout();
private:
    Ui::MainWindow *ui;
    MMTimer *SetTimer;
};
#endif // MAINWINDOW_H

你可能感兴趣的:(QT开发,qt,开发语言,c++)