Results 1 to 3 of 3

Thread: Custom QML Button Gradient animation - HOWTO

  1. #1
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Question Custom QML Button Gradient animation - HOWTO

    Dear Sirs and Madams!

    I have following custom QML Button, named UeButton (in file UeButton.qml):
    Qt Code:
    1. import QtQuick.Controls 1.4
    2. import QtQuick.Controls.Styles 1.4
    3. import QtQuick 2.5
    4.  
    5. Button
    6. {
    7. id: ueButton
    8.  
    9. property string ueText
    10.  
    11. signal ueSignalButtonClicked
    12.  
    13. text: ueText
    14.  
    15. style: ButtonStyle
    16. {
    17. background: Rectangle
    18. {
    19. antialiasing: true
    20. smooth: true
    21. gradient: Gradient
    22. {
    23. GradientStop
    24. {
    25. id: ueButtonGradientStopPosition0
    26.  
    27. position: 0
    28. color: "#ffffff"
    29. } // GradientStop
    30.  
    31. GradientStop
    32. {
    33. id: ueButtonGradientStopPosition1
    34.  
    35. position: 0.418
    36. color: "#000000"
    37. } // GradientStop
    38. } // Gradient
    39.  
    40. border.color: "steelblue"
    41. border.width: 1
    42. radius: 4
    43. } // background
    44.  
    45. label: Text
    46. {
    47. color: "#ffffff"
    48. font.bold: true
    49. verticalAlignment: Text.AlignVCenter
    50. horizontalAlignment: Text.AlignHCenter
    51. font.pointSize: 16
    52.  
    53. text: control.text
    54. } // label
    55. } // ButtonStyle
    56.  
    57. MouseArea
    58. {
    59. id: ueButtonMouseArea
    60.  
    61. anchors.fill: parent
    62.  
    63. onClicked:
    64. {
    65. parent.clicked()
    66. ueButton.ueSignalButtonClicked()
    67. }
    68. } // MouseArea
    69. } // ueButton
    To copy to clipboard, switch view to plain text mode 
    Now, what I want is when user clicks on button, the GradientStop points gets reversed. How do I achieve this?

    Sincerely,
    Marko
    Qt 5.3 Opensource & Creator 3.1.2

  2. #2
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Custom QML Button Gradient animation - HOWTO

    I've managed to animate gradient with following code chunk (from this example):
    Qt Code:
    1. SequentialAnimation on color
    2. {
    3. loops: Animation.Infinite
    4.  
    5. ColorAnimation
    6. {
    7. from: "#636363"
    8. to: "#303030"
    9. duration: 250
    10. }
    11. ColorAnimation
    12. {
    13. from: "#303030"
    14. to: "#636363"
    15. duration: 250
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 
    But, how do I trigger this animation once the user clicks on UeButton?
    Qt 5.3 Opensource & Creator 3.1.2

  3. #3
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default [SOLVED]Re: Custom QML Button Gradient animation - HOWTO

    I've managed to animate gradient with following code of UeButton:
    Qt Code:
    1. import QtQuick.Controls 1.4
    2. import QtQuick.Controls.Styles 1.4
    3. import QtQuick 2.5
    4.  
    5. Button
    6. {
    7. id: ueButton
    8.  
    9. property string ueText
    10.  
    11. signal ueSignalButtonClicked
    12.  
    13. text: ueText
    14.  
    15. style: ButtonStyle
    16. {
    17. background: Rectangle
    18. {
    19. antialiasing: true
    20. smooth: true
    21. gradient: Gradient
    22. {
    23. GradientStop
    24. {
    25. id: ueButtonAnimatedGradientStopPosition0
    26.  
    27. position: 0
    28.  
    29. color: "#ffffff"
    30.  
    31. ParallelAnimation on color
    32. {
    33. loops: 1
    34. running: ueButtonMouseArea.pressed
    35.  
    36. ColorAnimation
    37. {
    38. from: "#ffffff"
    39. to: "#000000"
    40. duration: 25
    41. } // ColorAnimation
    42.  
    43. ColorAnimation
    44. {
    45. from: "#000000"
    46. to: "#ffffff"
    47. duration: 25
    48. } // ColorAnimation
    49. } // ParallelAnimation
    50. } // GradientStop
    51.  
    52. GradientStop
    53. {
    54. id: ueButtonAnimatedGradientStopPosition1
    55.  
    56. position: 0.418
    57.  
    58. color: "#000000"
    59.  
    60. ParallelAnimation on color
    61. {
    62. loops: 1
    63. running: ueButtonMouseArea.pressed
    64.  
    65. ColorAnimation
    66. {
    67. from: "#000000"
    68. to: "#ffffff"
    69. duration: 25
    70. } // ColorAnimation
    71.  
    72. ColorAnimation
    73. {
    74. from: "#ffffff"
    75. to: "#000000"
    76. duration: 25
    77. } // ColorAnimation
    78. } // ParallelAnimation
    79. } // GradientStop
    80. } // Gradient
    81.  
    82. border.color: "steelblue"
    83. border.width: 1
    84. radius: 4
    85. } // background
    86.  
    87. label: Text
    88. {
    89. color: "#ffffff"
    90. font.bold: true
    91. verticalAlignment: Text.AlignVCenter
    92. horizontalAlignment: Text.AlignHCenter
    93. font.pointSize: 16
    94.  
    95. text: control.text
    96. } // label
    97. } // ButtonStyle
    98.  
    99. MouseArea
    100. {
    101. id: ueButtonMouseArea
    102.  
    103. anchors.fill: parent
    104.  
    105. onClicked:
    106. {
    107. parent.clicked()
    108. ueButton.ueSignalButtonClicked()
    109. }
    110. } // MouseArea
    111. } // ueButton
    To copy to clipboard, switch view to plain text mode 
    Since I did this for the first time, are there any optimizations available?

    Sincerely,
    Marko
    Qt 5.3 Opensource & Creator 3.1.2

Similar Threads

  1. Replies: 1
    Last Post: 12th December 2013, 08:25
  2. Replies: 2
    Last Post: 21st February 2012, 11:13
  3. Replies: 2
    Last Post: 13th November 2011, 02:30
  4. Howto create custom slot with Designer
    By gkhrapunovich in forum Qt Tools
    Replies: 6
    Last Post: 27th January 2011, 16:31
  5. Example HowTo create custom view
    By dexjam in forum Newbie
    Replies: 6
    Last Post: 12th July 2006, 11:06

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
  •  
Qt is a trademark of The Qt Company.