【问题标题】:QML components sequentially loading with fadeQML 组件使用淡入淡出顺序加载
【发布时间】:2013-12-05 09:17:14
【问题描述】:

我需要依次从数组中加载一个一个组件。 在将下一个组件加载到 Loader 之前,它应该是淡出、加载然后淡入。

上面的代码闪烁不正确并显示以下消息: “QML 状态:检测到属性“when”的绑定循环

我做错了什么?

谢谢

import QtQuick 2.0

Rectangle {
    id: screen
    width: 400
    height: 400
    color: "black"

    Rectangle {
        id: view
        anchors.fill: parent

        Loader {
            id: loader
            onLoaded: { view.state="fadeIn"; }
        }

        states: [
            State {
                name: "fadeOut";
                PropertyChanges { target: view; opacity: 0.1; }
            },
            State {
                name: "fadeIn";
                PropertyChanges { target: view; opacity: 1; }
            },
            State {
                name: "load"; when: view.opacity == 0;
                StateChangeScript { script: { loader.sourceComponent=com_array[index]; } }
            }
        ]

        transitions: [
            Transition {
                to: "fadeIn"
                NumberAnimation { properties: "opacity"; from: 0.0; to: 0.99; duration: 2000 }
            },
            Transition {
                to: "fadeOut"
                NumberAnimation { properties: "opacity"; from: 0.99; to: 0; duration: 2000 }
            }
        ]
    }

    Timer {
        id: timer

        interval: 3000; running: true; repeat: true
        onTriggered:  {
            ++index;
            if( index >= com_array.length ) {
                index = 0
            }

            view.state="fadeOut"
        }
    }

    // -------------------------------------------------------------------
    // Components

    Item {
        id: list

        property Component red_rect_com : Component {
            Rectangle {
                width: screen.width; height: screen.height

                Rectangle {
                    anchors.fill: parent; color: "red";
                }
            }
        }

        property Component blue_rect_com : Component {
            Rectangle {
                width: screen.width; height: screen.height

                Rectangle {
                    anchors.fill: parent; color: "blue";
                }
            }
        }
    }

    property int index: 0
    property var com_array: [ list.red_rect_com, list.blue_rect_com ]
    Component.onCompleted: { loader.sourceComponent = com_array[index]; }
}

UPD

这可能对其他完整的更正示例有用(感谢回答作者):

import QtQuick 2.0

Rectangle {
    id: screen
    width: 400
    height: 400
    color: "black"

    Rectangle {
        id: view
        anchors.fill: parent
        property var sourceComponent
        opacity: 0

        function loadComponent( component, is_first_start ) {
            sourceComponent = component;

            if( is_first_start ) {
                fadeOut.stop(); fadeIn.start();
            }
            else {
                fadeIn.stop(); fadeOut.start();
            }
        }

        Loader {
            id: loader
            onLoaded: {
                fadeOut.stop();
                fadeIn.start();
            }
        }

        NumberAnimation on opacity {
            id: fadeOut;
            to: 0.0;
            duration: 2000;
            onStopped: { loader.sourceComponent=view.sourceComponent; }
        }

        NumberAnimation on opacity {
            id: fadeIn;
            to: 1.0;
            duration: 2000
        }
    }

    Timer {
        id: timer

        interval: 4000; running: true; repeat: true
        onTriggered: {
            ++index;
            if( index >= com_array.length ) {
                index = 0
            }

            view.loadComponent( com_array[index], false );
        }
    }

    // -------------------------------------------------------------------
    // Components

    Item {
        id: list

        property Component red_rect_com : Component {
            Rectangle {
                width: screen.width; height: screen.height

                Rectangle {
                    anchors.fill: parent; color: "red";
                }
            }
        }

        property Component blue_rect_com : Component {
            Rectangle {
                width: screen.width; height: screen.height

                Rectangle {
                    anchors.fill: parent; color: "blue";
                }
            }
        }
    }

    property int index: 0
    property var com_array: [ list.red_rect_com, list.blue_rect_com ]
    Component.onCompleted: { view.loadComponent( com_array[index], true ); }
}

【问题讨论】:

    标签: qt qml qt5 qt-quick qtquick2


    【解决方案1】:

    它会给你错误,因为 QML 建立连接以在更改之间进行通信:

    onStateChange 由 PropertyChanges 连接,它将 opacity 更改为视图的 opacity 属性。 onOpacityChanged 信号将连接到 State 的 when 属性。 onWhenChanged 信号将连接到 state 属性,从而进行绑定循环。

    另一件事是当你在Transition中创建NumberAnimation时,你不需要指定from和to属性。 PropertyChanges 设置了正确的值。

    无论如何,您的状态没有意义,您使用错误。 您还应该等待fadeOut,因为它会加载得太快以至于您看不到效果。 只需制作两个动画:

    Rectangle {
        id: view
        anchors.fill: parent
        property var sourceComponent
        function loadComponent(component){
            fadeIn.stop(); fadeOut.start();
            sourceComponent = component;
        }
        Loader {
            id: loader
            onLoaded: { fadeOut.stop(); fadeIn.start(); }
        }
        NumberAnimation on opacity {
            id: fadeOut
            to: 0.0
            onStopped: { loader.sourceComponent= sourceComponent; }
        }
        NumberAnimation on opacity {
            id: fadeIn
            to: 1.0
        }
    }
    

    您还应该将加载更改为:

    property int index: 0
    onIndexChanged:  { view.loadComponent(com_array[index]); }
    

    但是有更好的解决方案:使用ShaderEffectSource在改变源之前取一帧Loader,在加载组件的同时用它淡出。

    【讨论】:

    • ShaderSource,你的意思是 ShaderEffectSource 吗?我在帮助中找不到它。
    • 哦,是的ShaderEffectSource,它可以单独使用,并且使用live:false,它会冻结。但是从磁盘加载组件通常非常快,您可能不需要它。
    • @Dmitry 我试图解释为什么会发生错误,但我不太确定。
    • Shader 对我来说是新的,如果您有想法如何将更新源示例转换为 ShaderEffectSource 方法,那就太好了!
    猜你喜欢
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多