Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QML: How to make nice transition effect between image sources (one fades into the other)?

Tags:

qt

qml

I wonder how to make smooth transitions betwen image sources in QML, I try

import QtQuick 1.1
Image {
   id: rect
   source:  "quit.png"
   smooth: true
   MouseArea {
       id: mouseArea
       anchors.fill: parent
       anchors.margins: -10
       hoverEnabled: true         //this line will enable mouseArea.containsMouse
       onClicked: Qt.quit()
   }

    states: State {
        name: "mouse-over"; when: mouseArea.containsMouse
        PropertyChanges { target: rect; scale: 0.8; source :"quit2.png" }
    }

    transitions: Transition {
        NumberAnimation { properties: "scale, source"; easing.type: Easing.InOutQuad; duration: 1000  }
    }
}

But It does not work on source as a transition just as final state change.. so I wonder how to make one image source fade into andothe and back?

like image 906
myWallJSON Avatar asked Apr 19 '12 08:04

myWallJSON


1 Answers

You want the first image to fade out into the other? How about if you place two Image objects on top of each other, then animate the opacity property?

EDIT: This worked for me (I'm using QtQuick 1.0 because my Qt Creator installation is a bit outdated):

import QtQuick 1.0
Rectangle {
Image {
   id: rect
   source:  "quit.png"
   smooth: true
   opacity: 1
   MouseArea {
       id: mouseArea
       anchors.fill: parent
       anchors.margins: -10
       hoverEnabled: true         //this line will enable mouseArea.containsMouse
       onClicked: Qt.quit()
   }


    states: State {
        name: "mouse-over"; when: mouseArea.containsMouse
        PropertyChanges { target: rect; scale: 0.8; opacity: 0}
        PropertyChanges { target: rect2; scale: 0.8; opacity: 1}
    }

    transitions: Transition {
        NumberAnimation { properties: "scale, opacity"; easing.type: Easing.InOutQuad; duration: 1000  }
    }
}

Image {
   id: rect2
   source:  "quit2.png"
   smooth: true
   opacity: 0
   anchors.fill: rect

  }
}

To the question in your comment: you can place the image exactly on top of the other by copying the anchors thru anchors.fill: rect

like image 94
teukkam Avatar answered Oct 01 '22 21:10

teukkam