Showing posts with label QTimer. Show all posts
Showing posts with label QTimer. Show all posts

Sunday, December 23, 2012

Using Timer with BB10 Cascades QML code

It is possible to use code Qt and QML API for BB10 development. But to get more native look and feel, I started exploring BB10 Cascades API.

Soon I started working with cascade API, I realized Timer QML element is now no longer available with Cascade QML API.

However you can export QTimer to cascade QML and use QTimer with your QML code.

Following example shows how that can be done.

First export QTimer to QML, by registering it with metasystem. You can put this code in main.cpp.

qmlRegisterType<QTimer>("my.library", 1, 0, "QTimer");
Then in QML file where you want to use QTimer import namespace where QTimer is exported, like below.
import my.library 1.0

Then define QTimer as attached object in QML.
attachedObjects: [
        QTimer{
            id: timer
            //set singleshot property if requireds
            singleShot: true
            //set interval
            interval: 5000
            
            onTimeout:{
                //do some stuff
            }
        }
    ]

You can start and stop timer by using it's id and then calling appropriate slots.
    function startTimer(){
        timer.restart();
    }
    
    function stopTimer() {
        timer.stop();
    }