【问题标题】:Animating height动画高度
【发布时间】:2015-09-11 04:45:16
【问题描述】:

我想通过修改 height 来显示/隐藏一个元素。这是显示我的问题的示例代码:

import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.4

Window {
    id: win
    width: 300
    height: 300
    visible: true

    ColumnLayout {
        width: parent ? parent.width : 200


        Switch {
            id: someswitch
            Layout.alignment: Qt.AlignCenter
        }

        Label {
            id: myText
            text: "dummy"
            height: 0

            wrapMode: Text.WordWrap
            clip: true
            Layout.fillWidth: true
            Layout.alignment: Qt.AlignCenter

            states: State {
                name: "visible"
                when: someswitch.checked
                PropertyChanges { target: myText; height: implicitHeight }
            }

            Behavior on height {
                NumberAnimation { duration: 100 }
            }
        }
    }
}

我还没有添加Transition/Animation,但是这个阶段的行为已经是错误的。 someswitch 默认未选中,但会显示文本。另一方面,在检查开关后,文本隐藏并且不再出现。

我应该如何处理?我希望文本“滑出”。我不想更改它的opacity

【问题讨论】:

    标签: qt qml qtquick2


    【解决方案1】:

    一般来说,应该保证States的一致性,以保证Transitions正常工作。可以通过以下任一方式实现一致性:

    • 通过定义一个一致默认状态
    • 通过定义 all 必要的States,就像提出的其他不错的答案一样。

    开始说,应该注意Layout 的存在在这里起着关键作用Layout 在某种程度上取代Itemsheight 设置及其minimumHeight 属性。在这种情况下,States 定义在height 之上确实 真正影响Label。显而易见的解决方案是强制States 保持一致但超过Layout.preferredHeight,即定义默认的State 以及"invisible",为Layout.preferredHeight 而不是height 使用不同的值。重新访问的代码版本可能如下所示:

    Label {
        id: myText
        text: "dummy"
        wrapMode: Text.WordWrap
        clip: true
        Layout.fillWidth: true
        Layout.alignment: Qt.AlignCenter
        Layout.preferredHeight: implicitHeight   //visible --> layout height = text implicit height
    
        states: State {
                name: "invisible"
                when: !someswitch.checked
                PropertyChanges { target: myText; Layout.preferredHeight: 0 }  // invisible --> layout item height forced to zero
            }
    
        Behavior on Layout.preferredHeight {
            NumberAnimation { duration: 100 }
        }
    }
    

    可以在here 找到一个完整的工作示例(而可以在here 找到具有“可见”状态的版本)。

    【讨论】:

      【解决方案2】:

      在您的情况下,您应该使用 StatesTransition。例如:

      import QtQuick 2.4
      import QtQuick.Window 2.0
      import QtQuick.Controls 1.2
      
      Window {
          id: mainWindow
          width: 600
          height: 600
          visible: true
      
          CheckBox {
              text: "hide/show"
              id: someswitch
              x: 10
              y: 10
          }
      
          Rectangle {
              id: mytext
              width: parent.width
              anchors.centerIn: parent
              color: "orange"
              state: "shown"
              states: [
                  State {
                      name: "shown"
                      when: !someswitch.checked
                      PropertyChanges { target: mytext; height: 300 }
                  },
                  State {
                      name: "hidden"
                      when: someswitch.checked
                      PropertyChanges { target: mytext; height: 0 }
                  }
              ]
              transitions: Transition {
                   PropertyAnimation { property: "height"; duration: 500; easing.type: Easing.InOutQuad }
              }
          }
      }
      

      【讨论】:

      • 即使someswitch默认没有被选中,文本也会显示。检查和取消选中它后,按预期工作。我的代码:paste.ubuntu.com/12313265
      猜你喜欢
      • 2011-06-03
      • 1970-01-01
      • 1970-01-01
      • 2016-08-25
      • 2012-03-01
      • 2023-03-14
      • 2019-04-05
      • 1970-01-01
      • 2012-04-20
      相关资源
      最近更新 更多