QTimer 的使用

QTimer 的使用,第1张

QTimer 的使用 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(1000,this,SLOT(updateCaption())); // 1秒后启动功能函数

成员函数

1)void QTimer::singleShot(int msec,Qt::TimerType timeType,const QObject receiver,const member) //在规定的时间间隔调用函数
举例:
  #include
  #include

  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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/586249.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-12
下一篇 2022-04-12

发表评论

登录后才能评论

评论列表(0条)

保存