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. Simple Screen Saver

Simple Screen Saver

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 3.8k Views 2 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.
  • G Offline
    G Offline
    granitepeak99
    wrote on last edited by
    #1

    Re: [SOLVED] Is this a reasonable way to do continuous sprite animation in QML?

    Here is one implementation using property animations that produces the "bouncing ball" effect like a screen saver. The basic idea is to animate the x and y properties of an image across the parent item with different animation durations, resulting in a random path.

        Image {
                id: logo
                x: Math.random() * parent.width
                y: Math.random() * parent.height
                source: "qrc:/images/logo.png"
                sourceSize.width: 600
    
                SequentialAnimation on x {
                    id: xAnimation
                    loops: Animation.Infinite
                    property int from: 0
                    property int to: parent.width - logo.width
                    property int duration: 15000
                    PropertyAnimation {
                        from: xAnimation.from
                        to: xAnimation.to
                        duration: xAnimation.duration
                        easing.type: Easing.Linear
                    }
                    PropertyAnimation {
                        from: xAnimation.to
                        to: xAnimation.from
                        duration: xAnimation.duration
                        easing.type: Easing.Linear
                    }
                }
    
                SequentialAnimation on y {
                    id: yAnimation
                    loops: Animation.Infinite
                    property int from: 0
                    property int to: parent.height - logo.height
                    property int duration: 9000
                    PropertyAnimation {
                        from: yAnimation.from
                        to: yAnimation.to
                        duration: yAnimation.duration
                        easing.type: Easing.Linear
                    }
                    PropertyAnimation {
                        from: yAnimation.to
                        to: yAnimation.from
                        duration: yAnimation.duration
                        easing.type: Easing.Linear
                    }
                }
            }
    

    If anyone has any better ideas on how to implement this more optimally, please let me know.

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @granitepeak99 Thank you for sharing. It would be more helpful for users if you provide a complete working example.
      Also how about using states and transitions instead of 2 sequential PropertyAnimation? It has reversible property.

      157

      1 Reply Last reply
      1
      • G Offline
        G Offline
        granitepeak99
        wrote on last edited by
        #3

        @p3c0 Thanks for the ideas! Here is a complete working example:

        import QtQuick 2.7
        import QtQuick.Window 2.2
        
        Window {
            visible: true
            width: 640
            height: 480
            title: qsTr("ScreenSaver")
        
            Text {
                anchors.centerIn: parent
                font.pixelSize: 15
                text: "Wait 10 seconds..."
            }
        
            Timer {
                id: screenSaverTimer
                interval: 10000
                running: true
                onTriggered: screenSaver.visible = true
            }
        
            // Reset screensaver timer when clicks are received
            MouseArea {
                anchors.fill: parent
        
                // Pass mouse events through
                propagateComposedEvents: true
                onClicked: { screenSaverTimer.restart(); mouse.accepted = false }
            }
        
            Rectangle {
                id: screenSaver
                anchors.fill: parent
                visible: false
                color: "black"
        
                Text {
                    id: screenSaverText
                    x: 0
                    y: 0
                    text: "Hello World"
                    color: "blue"
                    font.pixelSize: 50
        
                    SequentialAnimation on x {
                        id: xAnimation
                        loops: Animation.Infinite
                        property int from: 0
                        property int to: screenSaver.width - screenSaverText.width
                        property int duration: 15000
                        running: screenSaver.visible
                        PropertyAnimation {
                            from: xAnimation.from
                            to: xAnimation.to
                            duration: xAnimation.duration
                            easing.type: Easing.Linear
                        }
                        PropertyAnimation {
                            from: xAnimation.to
                            to: xAnimation.from
                            duration: xAnimation.duration
                            easing.type: Easing.Linear
                        }
                    }
        
                    SequentialAnimation on y {
                        id: yAnimation
                        loops: Animation.Infinite
                        property int from: 0
                        property int to: screenSaver.height - screenSaverText.height
                        property int duration: 9000
                        running: screenSaver.visible
                        PropertyAnimation {
                            from: yAnimation.from
                            to: yAnimation.to
                            duration: yAnimation.duration
                            easing.type: Easing.Linear
                        }
                        PropertyAnimation {
                            from: yAnimation.to
                            to: yAnimation.from
                            duration: yAnimation.duration
                            easing.type: Easing.Linear
                        }
                    }
                }
        
                MouseArea {
                    anchors.fill: parent
                    onClicked: { screenSaver.visible = false; screenSaverTimer.restart() }
                }
            }
        }
        
        1 Reply Last reply
        1

        • Login

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