要实现这个效果,您可以使用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助手提供,问题来源于学员提问