How to create an animated, variable size accordion component in QtQuick / QML

后端 未结 1 491
自闭症患者
自闭症患者 2020-12-07 04:55

I want to create an animated accordion-like element that expands on click. Here\'s how it should work.

When the user clicks one of the red rectangles, the g

相关标签:
1条回答
  • 2020-12-07 05:26

    Here is a quick and simple example:

    // AccItem.qml
    Column {
      default property alias item: ld.sourceComponent
      Rectangle {
        width: 200
        height: 50
        color: "red"
        MouseArea {
          anchors.fill: parent
          onClicked: info.show = !info.show
        }
      }
      Rectangle {
        id: info
        width: 200
        height: show ? ld.height : 0
        property bool show : false
        color: "green"
        clip: true
        Loader {
          id: ld
          y: info.height - height
          anchors.horizontalCenter: info.horizontalCenter
        }
        Behavior on height {
          NumberAnimation { duration: 200; easing.type: Easing.InOutQuad }
        }
      }
    }
    
    // Acc.qml
    Column {
      spacing: 5
      AccItem {
        Rectangle {
          width: 50
          height: 50
          radius: 50
          color: "blue"
          anchors.centerIn: parent
        }
      }
      AccItem {
        Rectangle {
          width: 100
          height: 100
          radius: 50
          color: "yellow"
          anchors.centerIn: parent
        }
      }
      AccItem {
        Rectangle {
          width: 75
          height: 75
          radius: 50
          color: "cyan"
          anchors.centerIn: parent
        }
      }
    }
    

    You are needlessly over-complicating it with the anchors and the layouts. It doesn't seem the problem calls for any of those.

    Update: I slightly refined the implementation, compared to the initial one the content would actually slide out of the header as paper out of printer rather than simply being unveiled, and also removed the source of a false positive binding loop warning.

    0 讨论(0)
提交回复
热议问题