QML定时器

QML中的Timer组件用于创建定时器,它可以一次性或周期性触发操作。关键属性包括interval(间隔时间),repeat(是否重复)和running(是否运行)。当触发器被激活时,它会发出triggered()信号。示例代码展示了如何设置单次和重复的定时器,以及如何通过触发不同函数来控制定时器的启停和重置。
摘要由CSDN通过智能技术生成

QML使用Timer使用定时器

Timer

计时器可用于触发操作一次,或以给定的间隔重复触发。

 常用属性:

interval

设置触发器之间的间隔(以毫秒为单位)。

默认间隔为 1000 毫秒。

repeat

设置重复,为真,则以指定的间隔重复触发计时器

默认为(false)

running设置启动,true为启动,false(默认)
triggeredOnStart触发启动

信号:

triggered()触发时,发出该信号

函数方法: 

restart()重新启动
start()开启
stop()暂停

设置单次的定时器:

Text{
        id:text1
        x:100
        y:100
        text:"null"
        font.pixelSize: 20//设置字体大小
    }

    Timer{
       interval: 2000//2秒
       running: true//开启
       onTriggered:text1.text=Date().toString();//设置时间
   }

初始状态:                                            两秒后:

 

持久的定时器:

Text{
        id:text1
        x:100
        y:100
        text:"null"
        font.pixelSize: 20//设置字体大小
    }

    Timer{
       interval: 2000//2秒
       running: true//开启
       repeat: true //开启以指定的间隔重复触发计时器
       onTriggered:text1.text=Date().toString();
   }

 triggeredOnStart的使用:

简单的讲:

  • 如果开启triggeredOnStart,则开始时会先执行一次,然后等待时间间隔
  • 如果关闭triggeredOnStart,则需要先等待时间间隔,然后才会执行

 不开启triggeredOnStart:

Text{
        id:text1
        x:100
        y:100
        text:"null"
        font.pixelSize: 20//设置字体大小
    }

    Timer{
       interval: 1000//1秒
       running: true//开启
       repeat: true //开启以指定的间隔重复触发计时器
       onTriggered:text1.text=Date().toString();
   }

初始状态为:

开启triggeredOnStart:

Text{
        id:text1
        x:100
        y:100
        text:"null"
        font.pixelSize: 20//设置字体大小
    }

    Timer{
       interval: 1000//1秒
       running: true//开启
       repeat: true //开启以指定的间隔重复触发计时器
       triggeredOnStart: true
       onTriggered:text1.text=Date().toString();
   }

初始状态为:

函数的使用:

 创建3个Rectangle,分别对应 开启定时器,暂停定时器,重启定时器,鼠标点击分别执行相应的函数。

Text{
        id:text1
        x:100
        y:100
        text:"null"
        font.pixelSize: 20//设置字体大小
    }

    Timer{
        id:time1
       interval: 1000//1秒
       repeat: true //开启以指定的间隔重复触发计时器
       triggeredOnStart: true
       onTriggered:text1.text=Date().toString();
   }
    Row{
        y:200
        Rectangle{
            id:rect1
            width: 100;height: 100
            MouseArea{
                anchors.fill:parent
                onPressed:{
                    time1.start()//开启定时器
                }
            }
            Text{
                anchors.fill:parent
                text: "开启定时器"
                font.bold: true
            }

        }
        Rectangle{
            id:rect2
            width: 100;height: 100
            MouseArea{
                anchors.fill:parent
                onPressed:{
                    time1.stop()//暂停定时器
                }
            }
            Text{
                anchors.fill:parent
                text: "暂停定时器"
                font.bold: true
            }

        }
        Rectangle{
            id:rect3
            width: 100;height: 100
            MouseArea{
                anchors.fill:parent
                onPressed:{
                    time1.restart()//重新开启定时器
                }
            }
            Text{
                anchors.fill:parent
                text: "重新开启定时器"
                font.bold: true
            }

        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值