2

Here is a MWE:

import QtQuick 2.12
import QtQuick.Controls 2.5

    ApplicationWindow
    {
        visible: true
        width: 640
        height: 480
        title: swipeView.contentData[swipeView.currentIndex].text; // doesnt work

        SwipeView
        {
            id: swipeView
            anchors.fill: parent

            Repeater
            {
                id: itemRepeater;
                model: 6

                Loader
                {
                    active: SwipeView.isCurrentItem || SwipeView.isNextItem || SwipeView.isPreviousItem

                    sourceComponent: Text
                    {
                        text: index
                        Component.onCompleted: console.log("created:", index)
                        Component.onDestruction: console.log("destroyed:", index)
                    }
                }
            }
        }
    }

I would like to have access to the currently viewed item (in the swipeView). I am trying to do that with the window title, but it doesn't work. What is the proper way of accessing the currently viewed object properties?

Łukasz Przeniosło
  • 2,725
  • 5
  • 38
  • 74

2 Answers2

1

Store the index in a new property and use swipeView.currentItem to access to it.

For example:

ApplicationWindow
{
    visible: true
    width: 640
    height: 480
    title: swipeView.currentItem.myIndex

    SwipeView
    {
        id: swipeView
        anchors.fill: parent
        Repeater
        {
            id: itemRepeater;
            model: 6

            Loader
            {
                property int myIndex: index;
                active: SwipeView.isCurrentItem || SwipeView.isNextItem || SwipeView.isPreviousItem

                sourceComponent: Text
                {
                    text: myIndex
                    Component.onCompleted: console.log("created:", index)
                    Component.onDestruction: console.log("destroyed:", index)
                }
            }
        }
    }
}

You should create a new custom element to embed your Loader, in order to be cleaner (i.e. being explicit on the fact that you can access to a inexistant property in Loader).

Dimitry Ernot
  • 6,256
  • 2
  • 25
  • 37
  • But the thing is, I need the current property of the `sourceComponent`. It is a `Text` in here just for simplification. Assuming the `sourceComponent` is a complicated type with many properties, how can I acces them, ie. the `text` property? – Łukasz Przeniosło May 09 '19 at 12:29
  • 1
    In my example, the `text` property in `sourceComponent` will take the same the property `myIndex`. You should expose the properties that you want and not trying to access directly to it. See the [Law of Demeter](https://en.wikipedia.org/wiki/Law_of_Demeter). You can use a QML property alias, also. – Dimitry Ernot May 09 '19 at 12:37
  • Yes, alias seems more appropriate, but the problem is different: When creating a property (or alias) in the `Loader` scope, I have no access to the `sourceComponent` properties from within the `Loader`. Even if I give the `sourceComponent` an id, it doesnt work... – Łukasz Przeniosło May 09 '19 at 12:42
  • 2
    you can use the `item` property of the `Loader` to access the loaded item. In your case it will be: `title: swipeView.currentItem.item.text` – GrecKo May 09 '19 at 12:43
  • Yes, I think this is the best solution. Thanks. – Łukasz Przeniosło May 09 '19 at 12:46
1

The closest solution I came up with, but it seems extremely dirty and "workaroundish":

import QtQuick 2.12
import QtQuick.Controls 2.5

ApplicationWindow
{
    id: mainWindow;

    visible: true
    width: 640
    height: 480

    SwipeView
    {
        property bool ll: false;

        id: swipeView
        anchors.fill: parent

        onCurrentIndexChanged:
        {
            if (ll)
                loaded();
        }

        function loaded()
        {
            // access any property from the sourceComponent like this:
            mainWindow.title = contentData[currentIndex].item.someProperty +
                    contentData[currentIndex].item.text;
        }

        Repeater
        {
            id: itemRepeater;
            model: 6

            Loader
            {
                active: SwipeView.isCurrentItem || SwipeView.isNextItem ||
                        SwipeView.isPreviousItem

                sourceComponent: Text
                {
                    property string someProperty: "the property";
                    text: index;

                    Component.onCompleted: console.log("created:", index);
                    Component.onDestruction: console.log("destroyed:", index);
                }

                onLoaded:
                {
                    swipeView.ll = true;
                    swipeView.loaded();
                }
            }
        }
    }
}
Łukasz Przeniosło
  • 2,725
  • 5
  • 38
  • 74