Results 1 to 5 of 5

Thread: Crossfade two images in QML

  1. #1
    Join Date
    Sep 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Crossfade two images in QML

    Been trying to crossfade 2 images using states and transitions but can't seem to find my newbie error. Upon clicking the image fades correctly the 1st time then from then from then on fails. Here is the code. Anyone see my error(s)

    javascript Code:
    1. Item {
    2. anchors.fill: parent
    3.  
    4. states: [
    5. State { // this will fade in rect2 and fade out rect
    6. name: "fadeInRect2"
    7. PropertyChanges { target: rect; opacity: 0}
    8. PropertyChanges { target: rect2; opacity: 1}
    9. },
    10. State { // this will fade in rect and fade out rect2
    11. name:"fadeOutRect2"
    12. PropertyChanges { target: rect;opacity:1}
    13. PropertyChanges { target: rect2;opacity:0}
    14. }
    15. ]
    16.  
    17. transitions: [
    18. Transition {
    19. to: "fadeInRect2"
    20. ParallelAnimation {
    21. NumberAnimation { properties: "opacity"; from:1;to:0;easing.type: Easing.InOutQuad; duration: 2500 }
    22. NumberAnimation { properties: "opacity"; from:0;to:1;easing.type: Easing.InOutQuad; duration: 2500 }
    23. }
    24. onRunningChanged: {
    25. if(!running) busyIndicator.running=false;
    26. }
    27. },
    28. Transition {
    29. to: "fadeOutRect2"
    30. ParallelAnimation {
    31. NumberAnimation { properties: "opacity";from:0;to:1;easing.type: Easing.InOutQuad; duration: 2500 }
    32. NumberAnimation { properties: "opacity";from:1;to:0;easing.type:Easing.InOutQuad; duration:2500}
    33. }
    34. onRunningChanged: {
    35. if(!running) busyIndicator.running=false;
    36. }
    37. }
    38. ]
    39.  
    40. Image {
    41. id: rect2
    42. smooth: true
    43. opacity: 0
    44. anchors.fill: parent
    45. onStatusChanged: {
    46. if(rect2.status===Image.Ready) {
    47. console.log('rect2 img is ready');
    48. fader.state='fadeInRect2';
    49. console.log('doing fadeInRect2');
    50. }
    51. }
    52. }
    53.  
    54. Image {
    55. id: rect
    56. smooth: true
    57. anchors.fill: parent
    58. opacity: 1
    59.  
    60. onStatusChanged: {
    61. if(rect.status===Image.Ready) {
    62. console.log('rect img is ready');
    63. fader.state='fadeOutRect2';
    64. console.log('doing fadeOutRect2');
    65. }
    66. }
    67. }
    68.  
    69. function toggle() {
    70. busyIndicator.running=true;
    71.  
    72. if(!rect2.opacity) { // load to the rect that has 0 opacity
    73. rect2.source="../../assets/red.jpg"
    74. }
    75. else {
    76. rect.source="../../assets/green.jpg"
    77. }
    78. }
    79. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 9th September 2016 at 08:13. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Crossfade two images in QML

    This code doesn't make much sense, if you ask me. I can't even get it to work.

    If you want to fade two images using states then this works:

    javascript Code:
    1. import QtQuick 2.4
    2.  
    3. Item {
    4. anchors.fill: parent
    5.  
    6. states: [
    7. State { // this will fade in rect2 and fade out rect
    8. name: "fadeInRect2"
    9. PropertyChanges { target: rect; opacity: 0}
    10. PropertyChanges { target: rect2; opacity: 1}
    11. },
    12. State { // this will fade in rect and fade out rect2
    13. name:"fadeOutRect2"
    14. PropertyChanges { target: rect;opacity:1}
    15. PropertyChanges { target: rect2;opacity:0}
    16. }
    17. ]
    18.  
    19. transitions: [
    20. Transition {
    21. NumberAnimation { property: "opacity"; easing.type: Easing.InOutQuad; duration: 2500 }
    22. }
    23. ]
    24.  
    25. Image {
    26. id: rect2
    27. smooth: true
    28. anchors.fill: parent
    29. source: "/usr/share/icons/oxygen/256x256/apps/yakuake.png"
    30. }
    31.  
    32. Image {
    33. id: rect
    34. smooth: true
    35. anchors.fill: parent
    36. source: "/usr/share/icons/oxygen/256x256/apps/showfoto.png"
    37. }
    38.  
    39. state: "fadeInRect2"
    40.  
    41. MouseArea {
    42. anchors.fill: parent
    43. onClicked: {
    44. parent.state = parent.state == "fadeInRect2" ? "fadeOutRect2" : "fadeInRect2"
    45. }
    46. }
    47. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Crossfade two images in QML

    Thanks for your reply but the image sources are from a external url and are not known till runtime. the images are for a background for a music player and change with every song. so what i wanted to to is crossfade at the end of every song.

    the code i posted was what i had been testing with the function toggler should have the url passed to it then it should load to either the rect or rect2 depending on which image was transparent . it works the 1st time then fails from then on. i can't see why the state only triggers the 1st time. the images appear to load though.
    Last edited by sysv; 9th September 2016 at 16:24.

  4. #4
    Join Date
    Sep 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Crossfade two images in QML

    ok so i made a few changes but the code is still not working correctly.

    click once fades correctly to green
    click 2nd time fades correctly to red

    click 3 or more times loading indicator comes on and stays on but no fade

    Main.qml

    import VPlayApps 1.0
    import QtQuick 2.5
    import QtQuick.Controls 2.0
    import "common"

    App {

    width: 640
    height: 480
    color: 'black'
    menuBarVPlayEnabled: false

    ButtonClicker{id:button}
    Fader{id:fader}
    BusyIndicator{id:busyIndicator}

    }


    BusyIndicator.qml

    import QtQuick 2.5
    import QtQuick.Controls 1.4
    import QtQuick.Controls.Styles 1.4

    BusyIndicator {
    running: false
    scale: .2
    anchors.centerIn: parent
    style: BusyIndicatorStyle {
    indicator: Image {
    visible: busyIndicator.running
    source: "../../assets/loader.gif"
    RotationAnimator on rotation {
    running: busyIndicator.running
    loops: Animation.Infinite
    duration: 2000
    from: 0 ; to: 360
    alwaysRunToEnd: false
    }
    }
    }
    }


    ButtonClicker.qml

    import QtQuick 2.5
    import QtQuick.Controls 2.0

    Button {
    width:text.width
    height:40
    text: "Start Fader"
    z:3
    anchors.left: parent.left
    anchors.toparent.top
    anchors.topMargin: 5
    anchors.leftMargin: 5
    onClicked: {
    fader.toggle();
    }
    }


    Fader.qml

    import QtQuick 2.5
    import QtQuick.Controls 2.0

    Item {
    anchors.fill: parent

    states: [
    State { // this will fade in rect2 and fade out rect
    name: "fadeInRect2"
    PropertyChanges { target: rect; opacity: 0}
    PropertyChanges { target: rect2; opacity: 1}
    },
    State { // this will fade in rect and fade out rect2
    name:"fadeOutRect2"
    PropertyChanges { target: rect;opacity:1}
    PropertyChanges { target: rect2;opacity:0}
    }
    ]

    transitions: [
    Transition {
    NumberAnimation { properties: "opacity";easing.type: Easing.InOutQuad; duration: 2500 }
    onRunningChanged: {
    if(!running) busyIndicator.running=false; // stop the loading indicator
    }
    }
    ]

    Image {
    id: rect2
    smooth: true
    opacity: 0
    anchors.fill: parent

    onStatusChanged: {
    if(rect2.status===Image.Ready) {
    console.log('rect2 img is ready');
    fader.state='fadeInRect2';
    console.log('doing fadeInRect2');
    }
    }
    }

    Image {
    id: rect
    smooth: true
    opacity: 1
    anchors.fill: parent

    onStatusChanged: {
    if(rect.status===Image.Ready) { // start the fade when img is ready
    console.log('rect img is ready');
    fader.state='fadeOutRect2';
    console.log('doing fadeOutRect2');
    }
    }
    }

    function toggle() {
    busyIndicator.running=true; // start the loading indicator

    if(!rect2.opacity) { // load to the rect that has 0 opacity
    rect2.source="https://placehold.it/640x480/008000"; //green rect
    }
    else {
    rect.source="https://placehold.it/640x480/ff0000"; //red rect
    }
    }
    }


    Added after 1 25 minutes:


    ok finally i fixed it problem was in the toggle function like bellow

    function toggle() {
    busyIndicator.running=true; // start the loading indicator

    if(!rect2.opacity) { // load to the rect that has 0 opacity
    rect2.source=""; // clr old
    rect2.source="https://placehold.it/640x480/008000"; //green rect
    }
    else {
    rect.source=""; // clr old
    rect.source="https://placehold.it/640x480/ff0000"; //red rect
    }
    }
    Last edited by sysv; 9th September 2016 at 21:54.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Crossfade two images in QML

    Quote Originally Posted by sysv View Post
    Thanks for your reply but the image sources are from a external url and are not known till runtime.
    It completely doesn't matter. You can put any image urls you want, just make sure you assign them to the rect and rect2.

    Your current code is very ugly - it is very imperative and the problem is easy to solve without any imperative code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 1
    Last Post: 17th February 2016, 15:44
  2. Replies: 1
    Last Post: 20th January 2011, 10:26
  3. Replies: 1
    Last Post: 16th November 2010, 17:45
  4. Images
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 15th December 2009, 15:22
  5. Replies: 4
    Last Post: 27th July 2009, 15:45

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.