访问contentData属性

时间:2019-05-09 12:07:40

标签: qt qml swipeview

这是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 :(得分:0)

将索引存储在新属性中,并使用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中不存在的属性)。

答案 1 :(得分:0)

我想出的最接近的解决方案,但它似乎非常肮脏且“变通”:

  public authenticate(): void {
    this.spinner.show();
    this.authService.authenticate(this.userName, this.pwd).add(response => {
        if (response.success){
            this.router.navigate([ROUTE_NAME, EVENTUAL_PARAMETER]); // route name would be admin, for example
        } else {
            // display a message, log or do something with the result
        }
  }