Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to access Swipeview.view attached property in a Loaded object?



  • I am trying to load pages into a SwipeView using a Loader. Per the examples given here I would have expected a Loader source defined in a separate QML file to be able to access the SwipeView attached properties. However I get TypeError: Cannot read property 'currentIndex' of null when I try to access SwipeView.view.currentIndex for instance.

       SwipeView {
            id: view
    
            currentIndex: 1
    
            Item {
                id: firstPage
            }
            Item {
                id: secondPage
                Loader {
                    source: "Step1.qml"
                }
            }
            Item {
                id: thirdPage
            }
    }
    

    In Step1.qml:

    Item {
    
            // visual elements here...
    
            Component.onCompleted: {
                console.log("cindex = " + SwipeView.view.currentIndex) // <-- Cannot read property 'currentIndex' of null
            }
    }
    

    Other attached props like SwipeView.index, SwipeView.isCurrentItem etc can be accessed fine. So why is Swipeview.view not valid within the loaded object?

    I am able to workaround by setting the SwipeView as a property of the Loader; then the SwipeView properties can be accessed as desired:

    Loader {
        property SwipeView sv: view
        source: "Step1.qml"   // Now SV props can be accessed via "sv" in Step1.qml
    }
    

    But considering that SwipeView.view is specified in the documentation as an attached property, I thought I should've been able to use that directly?


Log in to reply