Timer still running when main QML is not loaded



  • @
    Timer {
    id: decrementTimer
    interval: 2000 // 5 minutes
    repeat: true
    running: true

           onTriggered: {
              // do your decrement code here
               Storage.setSetting("setting",hungertext.text)
               JScript.decrementcode();
    
           }
           Component.onCompleted: { Storage.initialize() }
        }@
    

    I have this Timer code, and this timer is only working when the QML containing the timer is loaded,
    I want to do is when i view other QML using Loader file the Timer is still running,

    How can i do that?


  • Moderators

    There are a number of ways to do it, depending on how you have your QML laid out. It ultimately comes down to something changing the running: property of the timer, either by binding it to something (as in @running: someItem.visible@ ) or by having some property change the property dynamically upon some action, or by having some set of actions call start() and stop() on the timer.

    If it's based on a loader (in this example, someloader) being active, you could say:
    @
    Timer {
    ...
    // This makes the timer active only if someloader does not have a source set.
    running: (someloader.source != "")
    ...
    }
    @

    or, if the loader's being controlled by some other actions:

    @
    SomeItem {
    ...
    onSomeActionHappens: {
    timer.stop()
    someloader.source = "someQmlFile.qml"
    }

    onSomeOtherActionHappens: {
    timer.start()
    someloader.source = ""
    }
    ...
    }
    @

    It all depends on the logic in your code.

    Really this is all just basic QML operation. Property management (binding and setting) is one of the main aspects of the language's functionality. Definitely an important area to study up on if you're unsure of how it all ties together.



  • I tried putting this code on my main QML but still when i load QML2 the timer is not running,

    @
    //This element is stated in my main QML
    Timer {
    ...
    // This makes the timer active only if someloader does not have a source set.
    running: (someloader.source != "QML2.qml")
    ...
    }@

    If I use the this code in my 2nd QML, do i nid to state the timer element in that QML ?


  • Moderators

    Is your Loader element really called "someloader"? That was just an example id.

    Oops. and I see an error in the code I gave you. That should have said
    @
    running: (someloader.source == "")
    @

    The timer should be in the same file as the Loader itself. Not in the QML that the Loader actually loads.

    It's hard to give a more detailed answer without knowing how you have your QML constructed. Do you have any sample code you could share of the section you're having trouble with?



  • yup i changed the someloader to mainLoader which is the id of my Loader element but still when i click the mousearea that triggers to load the 2nd QML, the timer stops


  • Moderators

    The timer isn't inside the code that the Loader is loading (and unloading), is it?



  • @ Timer {
    id: decrementTimer
    interval: 2000 // 2 seconds
    repeat: true
    running: (mainLoader.source != "home_aspin.qml")

           onTriggered: {
              // do your decrement code here
               Storage.setSetting("setting",hungertext.text)
               JScript.decrementcode();
    
           }
           Component.onCompleted: { Storage.initialize() }
        }@
    

    @ MouseArea {
    id: sleep2home
    x: 138
    y: 407
    width: 95
    height: 76
    onClicked: {
    decrementTimer.start();
    mainLoader.source = "QML2";}
    }@

    this is inside my main QML, and i dont put timer codes on my QML2


  • Moderators

    But the Timer and the MouseArea are in the same file, correct?

    Just trying to make sure I understand the layout fully.

    (If the Timer is in a piece of QML which is loaded by a Loader, and that QML is unloaded, then the timer will stop because it doesn't exist anymore. So the timer has to be persistent somewhere.)



  • yup the timer and mousearea is in the same file, ah ok so i nid to create a new timer in the loaded QML to start again the timer,

    thank you again sir


  • Moderators

    No, I don't believe the loaded QML needs to know about the timer at all, does it? I think I'm confused as to what exactly you're needing to do...

    When do you want the timer to start, and when do you want it to stop?



  • there is no stop, the timer needs to continue until the application is running,

    the problem is i am using Loader element, and i used loader in viewing multiple QML's, and when i load a new QML the timer stops decrementing numbers


  • Moderators

    Oh, if the timer needs to stay running, just make sure that "running:" is always true and that the Timer element never gets destroyed.



  • jr_jags, I might be able to help you if you let me see your code at school. :D



  • haha, cge, ill show my code to u at school ^ ^


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.