QTimer的用法

1.singleShot的用法

    代码:

    QTextEdit *testEdit = new QTextEdit("hello world");

     testEdit->setMaximumHeight(20);

    QTimer::singleShot( 5*1000, testEdit, SLOT(clear()));

    statusBar()->addWidget(testEdit);

    解析:

    QTimer在5秒后发出信号,testEdit接受信号后调用clear函数,清空testEdit内的内容。

    singleShot只发出一次信号,单触发定时器

2. Qtimer的用法

    代码及注释:

    QLabel *label = new QLabel;

    QTimer *timer = new QTimer( label ); // 产生一个定时器

    connect( timer, SIGNAL(timeout()), label, SLOT(clear()) ); // 连接这个定时器的信号和槽,利用定时器的timeout(),从而触发clear()槽函数

    timer->start(5000); //开始定时器,并设定定时周期,每隔5秒就会重启定时器,可以重复触发定时,除非你利用stop()将定时器关掉

    timer->setSingleShot(true); // 仅仅启动定时器一次


    statusBar()->addWidget(label);

   解析:

    每5秒,清理label一次,而singleShot只清理一次。

   

原文地址:https://www.cnblogs.com/toby-zhang/p/5852085.html