简体   繁体   English

文本更改时的动画按钮宽度

[英]Animate Button width on text change

I have a Button which text changes and when its text changes, its width also changes.我有一个按钮,它的文本会发生变化,当它的文本发生变化时,它的宽度也会发生变化。 I want to animate this change in width when I change the text.我想在更改文本时为宽度变化设置动画。 How can I do that ?我怎样才能做到这一点 ?

I tried following but its not working我尝试关注但它不起作用

import QtQuick.Window 2.15
import QtQuick 2.15
import QtQuick.Controls 2.15
 
Window {
    width: 500
    height: 600

    visible: true

    Button {
        property bool t: false
        text : t ? "very more text hahaha haha hehe" : "less text"
        onClicked: t = !t

        Behavior on width {
            NumberAnimation {
                duration: 1000
            }
        }
    }
}


The change of the Button width is a byproduct of its implicitWidth being changed here. Button 宽度的变化是其implicitWidth 在此处更改的副产品。

By changing the Behavior to trigger on implicitWidth instead of width you will have your expected behavior.通过将行为更改为在implicitWidth宽度而不是width上触发,您将获得预期的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM