QML的Rectangle组件,描绘一个矩形,一个可视化的对象。
外加设置属性来达到我们想要的效果。常用的有矩形的颜色,边框颜色,圆角等设置。
Rectangle{ x:10//这里的坐标是相对于它的父窗口,也就是Window y:10 width: 100; height: 100;//一定要指定出大小才能显示 visible: true antialiasing: true;//抗锯齿,默认就是开启的 border.width: 10; border.color: "red";//边框的一些属性 color: "blue"//内部的颜色 radius: 5//圆角半径 gradient: Gradient{//颜色渐变 GradientStop { position: 0.0; color: "lightsteelblue" } GradientStop { position: 1.0; color: "blue" } } clip:true//这一属性设置表示如果他的子类超出了范围,那么就剪切掉,不让他显示和起作用 Rectangle{ id:son x:50; //基于父窗口的位置 y:50; width: 300; height: 100; color: "gray"; } }
Rectangle { color: "blue" width: 100; height: 100 Rectangle { color: "green" width: 25; height: 25 } Rectangle { color: "red" x: 25; y: 25; width: 25; height: 25 scale: 1.5 //放大1.5倍 transformOrigin: "TopLeft" //改变元素的原点位置 } }
Row { Rectangle { width: 100; height: 100 color: "blue" transform: Translate { y: 40 }//下移 } Rectangle { width: 100; height: 100 color: "red" transform: Translate { y: -40 }//上移 } }
Rectangle { width: 100; height: 100 color: "red" PropertyAnimation on x { to: 50; duration: 1000; loops: Animation.Infinite } //从(0,0)坐标移动到(50。50), PropertyAnimation on y { to: 50; duration: 1000; loops: Animation.Infinite }//1000表示一秒时间, duration 越大移动越慢 } Rectangle { width: 100; height: 100 color: "red" PropertyAnimation on x { to: 50; duration: 1000; easing.type: Easing.OutBounce } PropertyAnimation on y { to: 50; duration: 1000; easing.type: Easing.OutBounce }//移动到50,50 ,OutBounce 动画效果 } Rectangle { width: 100; height: 100; anchors.centerIn: parent color: "red" RotationAnimation on rotation { to: 90; direction: RotationAnimation.Clockwise } //旋转90° } Rectangle { width: 100; height: 100 ColorAnimation on color { from: "red"; to: "yellow"; duration: 1000 } //在1s时间内 颜色变化 }
// Rectangle 跟随鼠标移动 写法一 Item { width: 100; height: 100 Rectangle { id: rect width: 100; height: 100 color: "red" Behavior on x { PropertyAnimation { duration: 500 } } Behavior on y { PropertyAnimation { duration: 500 } } } MouseArea { anchors.fill: parent onClicked: { rect.x = mouse.x; rect.y = mouse.y } } } // Rectangle 跟随鼠标移动 写法二 Rectangle { id: rect width: 100; height: 100 color: "red" PropertyAnimation { id: animation target: rect properties: "x,y" duration: 1000 } MouseArea { anchors.fill: parent onClicked: { animation.to = 50; animation.running = true; } } } // Rectangle 跟随鼠标移动 写法三 Rectangle { id: rect width: 100; height: 100 color: "red" MouseArea { anchors.fill: parent onClicked: rect.state = "moved" } states: State { name: "moved" PropertyChanges { target: rect; x: 50; y: 50 } } transitions: Transition { PropertyAnimation { properties: "x,y"; duration: 1000 } } }
// 自由落体动画效果 Rectangle { id: rect width: 120; height: 200 Image { id: img source: "colors.png" anchors.horizontalCenter: parent.horizontalCenter y: 0 SequentialAnimation on y { loops: Animation.Infinite NumberAnimation { to: rect.height - img.height; easing.type: Easing.OutBounce; duration: 2000 } PauseAnimation { duration: 1000 } NumberAnimation { to: 0; easing.type: Easing.OutQuad; duration: 1000 } } } }