【问题标题】:QML TabView: Get tabIndex for a specific tabQML TabView:获取特定选项卡的 tabIndex
【发布时间】:2018-07-12 02:51:51
【问题描述】:

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

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

【问题讨论】:

    标签: qt qml tabview


    【解决方案1】:

    默认情况下没有这样的函数,但你可以通过迭代来创建该函数:

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

    用途:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-18
      • 2020-07-12
      • 1970-01-01
      相关资源
      最近更新 更多