QT开发中文网

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

要实现这个效果,您可以使用QML中的动画和状态来处理。下面是一个示例代码,演示了如何创建一个白字红底的按钮,并在点击后开始闪烁一分钟,期间禁止再次点击,一分钟后恢复正常。

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtGraphicalEffects 1.0

Item {
    width: 200
    height: 60

    property bool isFlashing: false
    
    Button {
        id: btn
        anchors.fill: parent
        text: "Click Me"
        
        // 定义按钮样式为白字红底
        background: Rectangle {
            color: "red"
            opacity: btn.enabled ? 1 : 0.5
            
            Text {
                text: btn.text
                font.bold: true
                font.pointSize: 16
                
                color: "white"
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignHCenter
                
                anchors.fill: parent
                
                // 当按钮不可用时降低文字透明度以提示禁用状态
                opacity: btn.enabled ? 1 : 0.7 
            }
        }
        
        onClicked: {
            if (!isFlashing) {
                isFlashing = true;
                
                // 禁用按钮点击事件并开始闪烁动画(1秒变亮+1秒变暗循环)
                btn.enabled = false;
                
                var flashAnimation = SequentialAnimation {
                    running:true
                    
                    NumberAnimation { target: rect; property:"color.alpha"; to:-0.8; duration : 1000 }
                    NumberAnimation { target: rect; property:"color.alpha"; to:0.8; duration : 1000 }
                }
                
                flashAnimation.repeatCount = -1
                flashAnimation.running = true
                
                Timer {
                    interval: 60000 // 闪烁1分钟
                    onTriggered: {
                        flashAnimation.stop();
                        isFlashing = false;
                        btn.enabled = true;
                    }
                }.start()
            }
        }
    }

}

在这个示例中,我们创建了一个按钮,并定义了其背景为红色的矩形,并在其中添加白色文字。当点击按钮时,通过使用动画实现闪烁效果,并设置按钮为不可用状态。一分钟后,闪烁结束,按钮恢复可点击状态。

请注意,此代码仅提供了一个基本的实现示例,您可以根据需要进行修改和扩展。


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?