zoukankan      html  css  js  c++  java
  • Qt timer学习

    QTimer(重复和单发计时器)
      应用QTimer时,先创建一个QTimer类,利用connect将timeout()与对应槽函数连接,在调用start()函数设置定时器时间间隔,每经过设置时间后,定时器会发出一个timeout(),

      相应的槽函数就会被触发,直到调用stop()函数停止。
      举例:
        QTimer *timer = new QTimer(this);
        connect(timer,SIGNAL(timeout()),this,SLOT(function));
        timer->start(1000);
        也可以不用定义QTimer类,直接调用QTimer的成员函数singleShot(),定时器只执行一次
        QTimer::singleShot(200,this,SLOT(updateCaption()));//200/1000秒后启动功能函数
    -----------------------------------------------------------------------------
    2.成员函数
      1)void QTimer::singleShot(int msec,Qt::TimerType timeType,const QObject *receiver,const *member)//在规定的时间间隔调用函数
      举例:
      #include<QApplication>
      #include<QTimer>
      int main(int argc,char *argv[])
      {
        QApplication app(argc,argv);
        QTimer::singleShot(10000,&app,SLOT(quit()));
        ........


        return app.exec();
      }//功能表述,在10分钟后,应用程序将关闭
    2)void QTimer::start(int msec);
      启动或者重启服务器,msec为时间间隔,没有参数时,时间间隔为0.
    3)void QTimer::stop();
      停止计时器
    4)void QTimer::timeout();
      当定时器时间到时,信号被发射。
    5)int QTimer::timerID()
      返回正在运行的计时器的ID号,否则返回为-1
    -----------------------------------------------------------------------------
    注意:
    QTimer的精确度依赖于底下的操作系统和硬件,绝大多数平台支持20毫秒的精确度,一些平台可以提供更高的。如果Qt不能传送定时器触发所要求的数量,它将会默默地抛弃一些。
      另一个使用QTimer的方法是为你的对象调用QObject::startTimer()和在你的类中(当然必须继承QObect)重新实现QObject::timerEvent()事件处理器。缺点是timerEvent()事件  处理器不支持像单触发器或信号那样的高级水平。
    一些操作系统限制可能用到的定时器的数量,Qt会尽力在限制范围内工作。
    -----------------------------------------------------------------------------

    The future's not set,there is no fate but what we make for ourselves.
  • 相关阅读:
    LeetCode 88. Merge Sorted Array
    LeetCode 75. Sort Colors
    LeetCode 581. Shortest Unsorted Continuous Subarray
    LeetCode 20. Valid Parentheses
    LeetCode 53. Maximum Subarray
    LeetCode 461. Hamming Distance
    LeetCode 448. Find All Numbers Disappeared in an Array
    LeetCode 976. Largest Perimeter Triangle
    LeetCode 1295. Find Numbers with Even Number of Digits
    如何自学并且系统学习计算机网络?(知乎问答)
  • 原文地址:https://www.cnblogs.com/wang1994/p/5943031.html
Copyright © 2011-2022 走看看