Доступ к свойствам contentData - PullRequest
1 голос
/ 09 мая 2019

Вот 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)
                    }
                }
            }
        }
    }

Я хотел бы получить доступ к просматриваемому элементу (в swipeView). Я пытаюсь сделать это с заголовком окна, но это не работает. Как правильно обращаться к просматриваемым в данный момент свойствам объекта?

Ответы [ 2 ]

0 голосов
/ 09 мая 2019

Самое близкое решение, которое я придумал, но оно кажется очень грязным и «обходным»:

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();
                }
            }
        }
    }
}
0 голосов
/ 09 мая 2019

Сохраните индекс в новом свойстве и используйте swipeView.currentItem для доступа к нему.

Например:

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)
                }
            }
        }
    }
}

Вы должны создать новый пользовательский элемент для встраивания вашего Loader, чтобы быть чище (т. Е. Явно указывать на то, что вы можете получить доступ к несуществующему свойству в Loader).

...