Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved] States and timer not behaving as expected
Forum Updated to NodeBB v4.3 + New Features

[Solved] States and timer not behaving as expected

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kwhitefoot
    wrote on last edited by
    #1

    A program I am building didn't work the way I expected so I tore it to pieces and made it much more modular. Unfortunately it still doesn't work but now it is more rationally organized I can extract the offending part and ask you for help.

    The UI is simply a text item with a mouse area and a controller. The controller is defined in a separate file and is simply a timer and three states.

    The desired behaviour is that it starts in the idle state waiting for a mouse click. When the user clicks the press me text it enters initDelay and sets the time interval and starts the timer. When the timer times out it enters the interval state sets the interval to a different value and enables the timer again. This last state should continue forever.

    What actually happens is that it all works untill the thrid state which it claims to enter but the next thing that happens is that the timer running property changes to false. The odd thing is that there is no obvious state transition to go with it.

    I'm quite new to this so I'm quite prepared to believe that I have missed something obvious, but what?

    Edit: I had forgotten the repeat property!
    Here is the code:
    ----Controller.qml
    @import QtQuick 1.1

    Item {
    state: "idle"

    function start(){
        state="initDelay"
    }
    onStateChanged: {
        console.log("State changed to " + state)
    }
    
    Timer{id:timer
        repeat: true // this was missing in the original, now it works as intended.
        onTriggered: {
            console.log("triggered")
            state="interval"
        }
        onRunningChanged: {
            console.log("running " + running)
        }
        onIntervalChanged: {
            console.log("interval " + interval)
        }
    }
    
    states: [
        State{
            name:"idle"
            PropertyChanges {
                target: timer
                running: false
            }
            StateChangeScript{
                name:"s1"
                script:{
                    console.log("entering " + name)
                }
            }
    
        },
        State{
            name:"initDelay"
            PropertyChanges {
                target: timer
                interval:4000
                running: true
            }
            StateChangeScript{
                name:"s2"
                script:{
                    console.log("entering " + name)
                }
            }
        },
        State{
            name:"interval"
            PropertyChanges {
                target: timer
                interval:1000
                running: true
            }
            StateChangeScript{
                name:"s3"
                script:{
                    console.log("entering " + name)
                }
            }
        }
    ]
    

    }
    @

    ----View.qml
    @
    import QtQuick 1.1

    Rectangle {
    width: 360
    height: 360
    Text {
    text: "Click me."
    MouseArea {
    anchors.fill: parent
    onClicked: {
    controller.start();
    }
    }
    }
    Controller{
    id:controller
    }
    }
    @

    1 Reply Last reply
    0
    • E Offline
      E Offline
      elpuri
      wrote on last edited by
      #2

      The running property of the Timer element changes to false, because it simply stops running after getting triggered. If you want to have the Timer element to trigger continuously until explicitly told to stop you need to set the "repeating" property of the Timer to true.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kwhitefoot
        wrote on last edited by
        #3

        Damn! I actually did set repeating in the actual project, just forgot it in the code I posted.

        Now my cut down example works so I have to look for another bug in the real code.

        Thanks for the reminder. Having another pair of eyes look at a bug is much more effective than just staring at it again and again.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved