QML TabView:获取特定选项卡的tabIndex

时间:2018-02-01 17:28:49

标签: qt qml tabview

有没有办法通过使用title或id字段从tabview获取特定选项卡的索引? 类似于:tabviewComponent.indexOf(redTab)

TabView {
     id: tabviewComponent
     Tab {
        id: redTab
        title: "Red"
     }
     Tab {
         id: blueTab
         title: "Blue"
     }
     Tab {
         id: greenTab
         title: "Green"
     }
 }

1 个答案:

答案 0 :(得分:3)

There is no such function by default but you can create that function by iterating:

TabView {
    id: tabviewComponent
    Tab {
        id: redTab
        title: "Red"
    }
    Tab {
        id: blueTab
        title: "Blue"
    }
    Tab {
        id: greenTab
        title: "Green"
    }

    function indexOf_by_id(id){
        for(var i=0; i < count; i++){
            if(id === getTab(i))
                return i
        }
        return -1
    }

    function indexOf_by_title(title){
        for(var i=0; i < count; i++){
            if(title === getTab(i).title)
                return i
        }
        return -1
    }
}

Use:

tabviewComponent.indexOf_by_id(redTab)
tabviewComponent.indexOf_by_title("Green")