如何在动画运行后关闭弹出的 QML?

发布于 2025-01-16 08:54:30 字数 3299 浏览 3 评论 0原文

我的弹出组件中有两个动画,一个在打开弹出窗口时运行,另一个在关闭弹出窗口时运行。两个动画都正常工作,我当前的问题是动画在关闭弹出窗口后运行,因此在组件打开时它不可见。

有没有办法只在运行动画后关闭弹出窗口? 或者在一定时间后关闭点击弹出窗口的方法?

这是我的带有动画的弹出组件

import QtQuick 2.12
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15

Popup {
    id: popup
    width: 392
    height: 768
    
    parent: Overlay.overlay
    modal: true
    dim: true
    closePolicy: Popup.NoAutoClose
    
    x: Math.round((parent.width - popup.width))
    y: Math.round((parent.height - popup.height) / 2)
    
    Overlay.modal: Item {
        id: overlay
        width: popup.width
        height: popup.height
        
        Rectangle {
            id: opacityBackground
            anchors.fill: parent
            color: "#000000"
            opacity: 0
            
            PropertyAnimation on opacity {
                to: 0.4; duration: 3000;
            }
        }
    }
    
    background: Rectangle {
        id: backgroundRectangle
        implicitWidth: popup.width
        implicitHeight: popup.height
        color: "#0D0D0D"
    }
    
    Item {
        id: content
        anchors.fill: parent
        
        Item {
            id: header
            width: popup.width - 4
            height: 72
            anchors.top: parent.top
            anchors.left: parent.left
            anchors.topMargin: - 4
            anchors.leftMargin: - 4
            
            Item {
                width: 105
                height: 72
                anchors.left: parent.left
                anchors.top: parent.top
            }
            
            NbIconButton {
                iconSource: "/icons/cancel.svg"
                anchors.top: parent.top
                anchors.right: parent.right
                buttonColor: "#0D0D0D"
                
                onClicked: {
                    slideOut.start()
                    popup.close()
                }
            }
        }
        
        Item {
            id: descriptionContainer
            width: 285
            height: 88
            anchors.top: parent.top
            anchors.topMargin: 118
            anchors.horizontalCenter: parent.horizontalCenter
        }
        
        Item {
            id: tableContainer
            anchors.top: contentTitle.bottom
            anchors.topMargin: 24
        }
    }
    
    onOpened: {
        slideIn.start()
    }
    
    onClosed: {
        slideOut.start()
    }
    
    ParallelAnimation {
        id: slideIn
        
        PropertyAnimation {
            target: overlay
            property: "opacity"
            to: 0
            duration: 3000
        }
        
        NumberAnimation {
            target: popup
            property: "x"
            from: parent.width
            to: 632
            duration: 3000
            easing.type: Easing.OutCubic
        }
    }
    
    ParallelAnimation {
        id: slideOut
        
        PropertyAnimation {
            target: opacityBackground
            property: "opacity"
            to: 0
            duration: 3000
        }
        
        NumberAnimation {
            target: popup
            property: "x"
            from: 632
            to: parent.width
            duration: 3000
            easing.type: Easing.OutCubic
        }
    }
}

I have two animations in my popup component, one runs when I open the popup and the other should run when I close it. Both animations work correctly, my current problem is that the animation runs after closing the popup, so it is not visible while the component is open.

Is there any way to close the popup only after running the animation?
Or a way to close the popup on click after a certain time?

Here is my popup component with the animations

import QtQuick 2.12
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15

Popup {
    id: popup
    width: 392
    height: 768
    
    parent: Overlay.overlay
    modal: true
    dim: true
    closePolicy: Popup.NoAutoClose
    
    x: Math.round((parent.width - popup.width))
    y: Math.round((parent.height - popup.height) / 2)
    
    Overlay.modal: Item {
        id: overlay
        width: popup.width
        height: popup.height
        
        Rectangle {
            id: opacityBackground
            anchors.fill: parent
            color: "#000000"
            opacity: 0
            
            PropertyAnimation on opacity {
                to: 0.4; duration: 3000;
            }
        }
    }
    
    background: Rectangle {
        id: backgroundRectangle
        implicitWidth: popup.width
        implicitHeight: popup.height
        color: "#0D0D0D"
    }
    
    Item {
        id: content
        anchors.fill: parent
        
        Item {
            id: header
            width: popup.width - 4
            height: 72
            anchors.top: parent.top
            anchors.left: parent.left
            anchors.topMargin: - 4
            anchors.leftMargin: - 4
            
            Item {
                width: 105
                height: 72
                anchors.left: parent.left
                anchors.top: parent.top
            }
            
            NbIconButton {
                iconSource: "/icons/cancel.svg"
                anchors.top: parent.top
                anchors.right: parent.right
                buttonColor: "#0D0D0D"
                
                onClicked: {
                    slideOut.start()
                    popup.close()
                }
            }
        }
        
        Item {
            id: descriptionContainer
            width: 285
            height: 88
            anchors.top: parent.top
            anchors.topMargin: 118
            anchors.horizontalCenter: parent.horizontalCenter
        }
        
        Item {
            id: tableContainer
            anchors.top: contentTitle.bottom
            anchors.topMargin: 24
        }
    }
    
    onOpened: {
        slideIn.start()
    }
    
    onClosed: {
        slideOut.start()
    }
    
    ParallelAnimation {
        id: slideIn
        
        PropertyAnimation {
            target: overlay
            property: "opacity"
            to: 0
            duration: 3000
        }
        
        NumberAnimation {
            target: popup
            property: "x"
            from: parent.width
            to: 632
            duration: 3000
            easing.type: Easing.OutCubic
        }
    }
    
    ParallelAnimation {
        id: slideOut
        
        PropertyAnimation {
            target: opacityBackground
            property: "opacity"
            to: 0
            duration: 3000
        }
        
        NumberAnimation {
            target: popup
            property: "x"
            from: 632
            to: parent.width
            duration: 3000
            easing.type: Easing.OutCubic
        }
    }
}

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

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

发布评论

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

评论(1

请爱~陌生人 2025-01-23 08:54:30

我知道已经晚了,但更好的解决方案是使用 Popupenterexit 属性。我发帖是为了其他有需要的人。它会是这样的:

import QtQuick
import QtQuick.Controls.Basic
import QtQuick.Window

Window {
    id: root
    width: 640
    height: 480
    visible: true

    Button {
        onClicked: popup.open()
    }

    Popup {
        id: popup
        width: 200
        height: 400
        anchors.centerIn: Overlay.overlay
        modal: true
        focus: true
        closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside

        enter: Transition {
            ParallelAnimation {
                id: popIn
                PropertyAnimation {
                    target: popup
                    property: "scale"
                    from: 0.9
                    to: 1
                    duration: 50
                }
                PropertyAnimation {
                    target: popup
                    property: "opacity"
                    from: 0.9
                    to: 1
                    duration: 50
                }
            }
        }
        exit: Transition {
            ParallelAnimation {
                id: popOut
                PropertyAnimation {
                    target: popup
                    property: "scale"
                    from: 1
                    to: 0.9
                    duration: 50
                }
                PropertyAnimation {
                    target: popup
                    property: "opacity"
                    from: 1
                    to: 0.9
                    duration: 50
                }
            }
        }
    }
}

I know it's late but a better solution is using properties enter and exit of Popup. I am posting for other people who need it. It would be something like this:

import QtQuick
import QtQuick.Controls.Basic
import QtQuick.Window

Window {
    id: root
    width: 640
    height: 480
    visible: true

    Button {
        onClicked: popup.open()
    }

    Popup {
        id: popup
        width: 200
        height: 400
        anchors.centerIn: Overlay.overlay
        modal: true
        focus: true
        closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside

        enter: Transition {
            ParallelAnimation {
                id: popIn
                PropertyAnimation {
                    target: popup
                    property: "scale"
                    from: 0.9
                    to: 1
                    duration: 50
                }
                PropertyAnimation {
                    target: popup
                    property: "opacity"
                    from: 0.9
                    to: 1
                    duration: 50
                }
            }
        }
        exit: Transition {
            ParallelAnimation {
                id: popOut
                PropertyAnimation {
                    target: popup
                    property: "scale"
                    from: 1
                    to: 0.9
                    duration: 50
                }
                PropertyAnimation {
                    target: popup
                    property: "opacity"
                    from: 1
                    to: 0.9
                    duration: 50
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文