简体   繁体   English

按钮悬停故障的不透明度变化 QML

[英]Opacity change on button hovered glitch QML

I have created a custom virtual keyboard where the components are buttons, whose attributes I have defined in the code below.我创建了一个自定义虚拟键盘,其中的组件是按钮,我在下面的代码中定义了它们的属性。 When I rapidly move the mouse across the buttons, I find that there is a 'flashing' effect whereby the button opacity appears to momentarily drop to 0 and then return.当我在按钮上快速移动鼠标时,我发现有一种“闪烁”效果,按钮不透明度似乎暂时下降到 0 然后返回。 How can I prevent this flashing effect?我怎样才能防止这种闪烁效果?

import QtQuick 2.15
import QtQuick.Controls 2.12
import QtGraphicalEffects 1.0
import "definitions.js" as Defs

Button {
    id: keyboardButton
    property bool specialButton: false

    contentItem: Text {
        text: keyboardButton.text
        font.family: Defs.font
        font.pointSize: Defs.defaultTextSize
        color: Defs.ivory
        horizontalAlignment: Text.AlignHCenter
        verticalAlignment: Text.AlignVCenter
        elide: Text.ElideRight
    }

    background: Rectangle {        
        id: background
        property double op: specialButton ? 0.7 : 0.5
        color: Defs.b_blue
        opacity: op
        radius: 0

        OpacityAnimator on opacity{
                        running: keyboardButton.hovered
                        from: background.opacity
                        to: background.op + 0.05
                        duration: 100

        }
        OpacityAnimator on opacity{
                        running: !keyboardButton.hovered
                        from: background.op + 0.05
                        to: background.op
                        duration: 100
        }
    }
}




Using one animator fixes it for me:使用一个动画师为我修复它:

import QtQuick 2.15
import QtQuick.Controls 2.15

ApplicationWindow {
    width: 640
    height: 480
    visible: true

    Flow {
        anchors.fill: parent

        Repeater {
            model: 100

            Button {
                id: keyboardButton

                property bool specialButton: false

                contentItem: Text {
                    text: keyboardButton.text
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    elide: Text.ElideRight
                }

                background: Rectangle {
                    color: "steelblue"
                    opacity: hovered ? baseOpacity + 0.15 : baseOpacity
                    radius: 0
                    
                    property double baseOpacity: keyboardButton.specialButton ? 0.7 : 0.5

                    Behavior on opacity {
                        OpacityAnimator {
                            duration: 100
                        }
                    }
                }
            }
        }
    }
}

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

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