QML::Rectangle組件


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 }
            }
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM