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. How to stop() Color Animation?
Forum Updated to NodeBB v4.3 + New Features

How to stop() Color Animation?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 627 Views
  • 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.
  • J Offline
    J Offline
    JasmineSethi
    wrote on last edited by JasmineSethi
    #1

    Following my code snippet:

    import QtQuick 2.9
    import QtQuick.Window 2.3

    Window
    {
    id:root
    visible: true
    width: 500
    height: 300
    color:"red"

    //to stop
    Rectangle
    {
        id: r1
        height: 100
        width: 100
        color: "green"
    
        MouseArea
        {
            anchors.fill: parent
            onClicked:
            {
                t1.running=false
                t1.stop()
            }
    
        }
    }
    
    //to start
    Rectangle
    {
        id: r2
        height: 100
        width: 100
        color: "black"
        anchors.top: r1.bottom
    
        MouseArea
        {
            anchors.fill: parent
            onClicked:
            {
                t1.running=true
                t1.start()
            }
    
        }
    }
    
    ColorAnimation on color { id:a; to: "yellow"; duration: 1000; onStopped: b.start(); running: false }
    ColorAnimation on color { id:b; to: "green"; duration: 1000; onStopped: a.start();running: false }
    
    
    Timer
    {
        id: t1
        running: false
        onTriggered:
            a.start()
    }
    

    }

    I want to stop the animation but using stop(), i am unable to stop the animation?
    I want to show blink of two colors and when stop is pressed it should stop and when start is pressed it should start.
    Can anyone suggest any idea?

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by dheerendra
      #2

      When you run this what is happening ? Is the timer really starting ? You are specifying just one second time for Animation. Just blink of an eye, your animation is complete. Can you debug the app by placing console.log and see what is happening ? Logs will help you to see what happening everywhere. Your issue is actually starting from timer & short animation interval.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • mranger90M Offline
        mranger90M Offline
        mranger90
        wrote on last edited by
        #3

        The problem is in your animation. Every time one stops, the other will start, and you are depending on the timer state, which is incorrect. You need to depend on some other state variable.
        Here is a modified version:

        import QtQuick 2.9
        import QtQuick.Window 2.2
        
        Window {
            id:root
            visible: true
            width: 500
            height: 300
            color:"red"
            property bool colorRun;
            colorRun: true
        
            //to stop
            Rectangle
            {
                id: r1
                height: 100
                width: 100
                color: "green"
        
        
                MouseArea
                {
                    anchors.fill: parent
                    onClicked:
                    {
                        console.log("Green area clicked");
                        t1.running = false
                        t1.stop()
                        a.stop()
                        b.stop()
                        colorRun = false;
                    }
        
                }
            }
        
            //to start
            Rectangle
            {
                id: r2
                height: 100
                width: 100
                color: "black"
                anchors.top: r1.bottom
        
        
                MouseArea
                {
                    anchors.fill: parent
                    onClicked:
                    {
                        t1.running = true
                        t1.start()
                    }
        
                }
            }
        
            ColorAnimation on color { id:a; to: "yellow"; duration: 1000; onStopped: colorRun ? b.start() : b.stop(); running: false }
            ColorAnimation on color { id:b; to: "green"; duration: 1000; onStopped: colorRun ? a.start() : a.stop(); running: false }
        
        
            Timer
            {
                id: t1
                running: false
                onTriggered: {
                    console.log("Timer Trigger fired");
                    a.start()
                    colorRun = true
                }
            }
        }
        
        
        J 1 Reply Last reply
        2
        • mranger90M mranger90

          The problem is in your animation. Every time one stops, the other will start, and you are depending on the timer state, which is incorrect. You need to depend on some other state variable.
          Here is a modified version:

          import QtQuick 2.9
          import QtQuick.Window 2.2
          
          Window {
              id:root
              visible: true
              width: 500
              height: 300
              color:"red"
              property bool colorRun;
              colorRun: true
          
              //to stop
              Rectangle
              {
                  id: r1
                  height: 100
                  width: 100
                  color: "green"
          
          
                  MouseArea
                  {
                      anchors.fill: parent
                      onClicked:
                      {
                          console.log("Green area clicked");
                          t1.running = false
                          t1.stop()
                          a.stop()
                          b.stop()
                          colorRun = false;
                      }
          
                  }
              }
          
              //to start
              Rectangle
              {
                  id: r2
                  height: 100
                  width: 100
                  color: "black"
                  anchors.top: r1.bottom
          
          
                  MouseArea
                  {
                      anchors.fill: parent
                      onClicked:
                      {
                          t1.running = true
                          t1.start()
                      }
          
                  }
              }
          
              ColorAnimation on color { id:a; to: "yellow"; duration: 1000; onStopped: colorRun ? b.start() : b.stop(); running: false }
              ColorAnimation on color { id:b; to: "green"; duration: 1000; onStopped: colorRun ? a.start() : a.stop(); running: false }
          
          
              Timer
              {
                  id: t1
                  running: false
                  onTriggered: {
                      console.log("Timer Trigger fired");
                      a.start()
                      colorRun = true
                  }
              }
          }
          
          
          J Offline
          J Offline
          JasmineSethi
          wrote on last edited by
          #4

          @mranger90 Thank u very much. Problem solved

          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