Qt 定时器

Qt 定时器,第1张

如果此类继承于QObject,可以直接调用startTimer函数,函数原型如下:

此函数开启定时器,并返回定时器的ID。

另外通过重载timerEvent函数来进行事件处理,其函数原型如下:

在该函数中进行事件处理,可通过定时器ID的判断(event->timerId() == m_TimerID),来处理多个定时器。

关闭定时器的函数为killTimer,函数原型如下:

class Example:publicQWidget

{

    Q_OBJECT

public:

    Example(QWidgetparent= 0);

    ~ Example();

    virtualvoidtimerEvent(QTimerEventevent);

private:

    int m_TimerID; //定时器ID

    int m_TimerLeaveSeconds; //定时器剩余秒数

};

Example::Example(QWidgetparent)

    :QWidget(parent),

    m_TimerID(-1),

    m_TimerLeaveSeconds(0)

{

    uisetupUi(this);

    m_TimerID=startTimer(1000);

}

void Example::timerEvent(QTimerEventevent)

{

    if(event->timerId() ==m_TimerID)

    {

        //做相应的事件处理

        killTimer(m_TimerID);//达到条件,杀死定时器

    }

}

在类中添加QTimer类型成员,使用start()来开始并且把它的timeout()信号函数连接到适当的槽。当这段时间过去了,它将会发射timeout()信号。

QTimer timer = new QTimer(this);

connect(timer, SIGNAL(timeout()), this, SLOT(update()));

timer->start(1000);

start()函数是重载函数,其原型如下:

(1)void QTimer::start()

启动或者重新启动定时器,其时间间隔是interval,可通过 setInterval(int msec) 函数进行设置。

(2)void QTimer::start(int msec)

启动或者重新启动定时器,时间间隔单位是毫秒。

结束一个定时器。

connect函数进行信号与槽的连接,每隔设置的固定间隔就会执行槽函数(update())。

注意:如果在类头文件中声明了一个QTimer变量,并且在构造函数中new了,不要忘了在析构函数中delete,并置位NULL。

另外,QTimer的精确度依赖于底下的 *** 作系统和硬件。绝大多数平台支持20毫秒的精确度,一些平台可以提供更高的。如果Qt不能传送定时器触发所要求的数量,它将会默默地抛弃一些。

(1)void QTimer::singleShot(int msec, const QObject receiver, const char member)

这是个静态函数,能够在给定的时间间隔后调用槽,并不是多次触发该槽函数,该槽函数只执行一次。使用此函数非常方便,也不必创建本地qTimer对象。

实例:

        #include <qapplicationh>

        #include <qtimerh>

        int main( int argc, char argv )

        {

            QApplication a( argc, argv );

            QTimer::singleShot( 10601000, &a, SLOT(quit()) );

                // 创建并且显示你的窗口部件

            return aexec();

        }

这个示例程序会自动在10分钟之后终止(也就是600000毫秒)。

receiver是正在接收的对象并且member是一个槽。时间间隔是msec。

(2)bool QTimer::isActive () const

如果定时器正在运行,返回真,否则返回假。

(3)void QTimer::changeInterval ( int msec )

改变定时器时间间隔为msec毫秒。如果这个定时器信号是运行的,它将会被停止并且重新开始,否则它将会被开始。

代码修改如下:

Option Explicit

Private Declare Sub GetLocalTime Lib "kernel32" (lpSystemTime As SYSTEMTIME) '传回本地时间

Private Type SYSTEMTIME

wYear As Integer

wMonth As Integer

wDayOfWeek As Integer

wDay As Integer

wHour As Integer

wMinute As Integer

wSecond As Integer

wMilliseconds As Integer

End Type

Dim SysTime As SYSTEMTIME

Private Sub Form_Load()

Text1Text = "00:00:00:00"

Command1Caption = "开始"

Command2Caption = "结束"

Command2Enabled = False

Timer1Interval = 1

Timer1Enabled = False

End Sub

Private Sub Command1_Click()

If Command1Caption = "开始" Then

Timer1Enabled = True

Command1Caption = "暂停"

Command2Enabled = True

ElseIf Command1Caption = "暂停" Then

Timer1Enabled = False

Command1Caption = "继续"

ElseIf Command1Caption = "继续" Then

Timer1Enabled = True

Command1Caption = "暂停"

End If

End Sub

Private Sub Command2_Click()

Timer1Enabled = False

Command2Enabled = False

Text1Text = "00:00:00:00"

Command1Caption = "开始"

Command1SetFocus

End Sub

Private Sub Timer1_Timer()

GetLocalTime SysTime

Text1Text = SysTimewHour & ":" & Format(SysTimewMinute, "0#") & ":" & Format(SysTimewSecond, "0#") & ":" & SysTimewMilliseconds

End Sub

以上就是关于Qt 定时器全部的内容,包括:Qt 定时器、Qt计时器可以达到2毫秒以下的精度吗求高手指点、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址: https://outofmemory.cn/web/9406521.html

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

发表评论

登录后才能评论

评论列表(0条)

保存