QTimer太让人失望了,一秒触发一次事件都不准确。。

 今天做项目中,我用QTimer来模拟数据生成,在另外的设备上接受。另外设备上有时1秒读不到数据,查询原因很久,终于发现是QTimer的问题。

测试代码如下 有兴趣同学可以自己试试。

t = new QTimer(this);
    t->start(1000);
    connect(t,SIGNAL(timeout()),this,SLOT(on_showtime()));

 

void qtdemo::on_showtime()
{
    static int pre=-1;
    QDateTime dt = QDateTime::currentDateTime();
    if(pre==-1)
    {
    }
    else if(pre==59)
    {
        if(dt.time().second()!=0)
        {
           qDebug()<<"pre="<<pre;
           qDebug()<<"now "<<dt.time().second();
        }
    }
    else
    {
        if( (pre+1)!=dt.time().second())
        {
           qDebug()<<"pre="<<pre;
           qDebug()<<"now "<<dt.time().second();
        }
    }
    pre=dt.time().second();
}

运行后显示:

说明没有1秒触发1次事件。。。

另参考:http://qtcn.org/bbs/simple/?t57669.html

posted @ 2015-02-06 13:48  长风破浪  阅读(1931)  评论(0编辑  收藏  举报