Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QML 1.37 KB | None | 0 0
  1. import QtQuick 2.0
  2.  
  3. Item {
  4.     id: root
  5.  
  6.     property bool isChecked: _switcher.state !== "off"
  7.  
  8.     Rectangle {
  9.         id: _background
  10.         anchors.fill: root
  11.         color: "lightgreen"
  12.     }
  13.  
  14.     Rectangle {
  15.         id: _switcher
  16.  
  17.         width: root.width / 2
  18.  
  19.         anchors {
  20.             top: root.top
  21.             bottom: root.bottom
  22.             left: root.left
  23.  
  24.             margins: 20
  25.         }
  26.  
  27.         color: "blue"
  28.         opacity: 0.5
  29.  
  30.         states: [
  31.             State {
  32.                 name: "off"
  33.                 AnchorChanges {
  34.                     target: _switcher
  35.                     anchors.left: _background.left
  36.                     anchors.right: undefined
  37.                 }
  38.             },
  39.             State {
  40.                 name: "on"
  41.                 AnchorChanges {
  42.                     target: _switcher
  43.                     anchors.right: _background.right
  44.                     anchors.left: undefined
  45.                 }
  46.             }
  47.         ]
  48.  
  49.         transitions: Transition {
  50.             AnchorAnimation { duration: 500 }
  51.         }
  52.  
  53.         Component.onCompleted: {
  54.             state = "off"
  55.         }
  56.     }
  57.  
  58.     MouseArea {
  59.         anchors.fill: root
  60.         onClicked: {
  61.             _switcher.state = isChecked ? "off" : "on";
  62.             console.log("Now isChecked = " + isChecked);
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement