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

发布于 2025-02-09 11:54:47 字数 506 浏览 6 评论 0原文

我有一个按钮,该按钮会更改文本,其文本更改时,其宽度也会改变。当我更改文本时,我想对宽度进行动画变化。我该怎么做?

我尝试关注但不起作用

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


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


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抱猫软卧 2025-02-16 11:54:47

按钮宽度的更改是其隐式宽度在这里更改的副产品。

通过将行为更改为触发indimitwidth而不是width,您将具有预期的行为。

The change of the Button width is a byproduct of its implicitWidth being changed here.

By changing the Behavior to trigger on implicitWidth instead of width you will have your expected behavior.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文