QT开发中文网

QML Button白字红底,点击都背景颜色开始闪烁1分钟且不能再点击,一分钟后闪烁结束并可以再点击

你可以通过QML的动画和状态来实现这个效果。首先,设置按钮的文本颜色为白色,背景颜色为红色。然后,在按钮被点击时启动一个闪烁的动画,并禁用按钮的交互能力。

以下是一个示例代码:

import QtQuick 2.0
import QtQuick.Controls 2.4

Item {
    width: 200
    height: 40
    
    Button {
        id: button
        anchors.centerIn: parent
        text: "Click Me"
        
        // 初始样式:白字红底
        color: "white"
        background: Rectangle {
            color: "red"
        }
        
        // 动画定义
        PropertyAnimation {
            id: flashAnimation
            target: button.background
            property: "color"
            
            from: "red"
            to: "transparent"
            duration: 500 // 每次闪烁0.5秒
            
            loops: Animation.Infinite // 循环播放闪烁动画
            running: false // 默认不运行动画
        }
        
        MouseArea {
            anchors.fill: parent
            
            onClicked: {
                button.enabled = false; // 禁用按钮
                
                flashAnimation.running = true; // 启动闪烁动画
                
                Timer {
                    interval: 60000; // 设置1分钟(60秒)
                    onTriggered:{
                        flashAnimation.running = false; // 停止闪烁动画
                        
                        button.enabled = true; // 启用按钮
                    }
                    repeat:false;
                    running:true;
                }
            }
            
            // 按钮状态切换
            states: State {
                name: "disabled"
                when: !button.enabled
                
                PropertyChanges {
                    target: button
                    color: "gray" // 禁用状态字体颜色为灰色
                }
            }
            
            transitions: Transition {
                from: ""
                to: "disabled"
                
                PropertyAnimation {
                    target: button
                    property: "color"
                    duration: 200 // 状态切换动画时间0.2秒
                }
            }
        }
    }
}

在上面的代码中,当按钮被点击时,闪烁动画启动并禁用按钮交互。一分钟后,闪烁动画停止并启用按钮交互。通过使用Timer计时器来控制闪烁结束时间。

请注意,这只是一个简单示例,你可以根据实际需求进行适当修改和调整。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=9442

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?