在选项卡更改时,两个选项卡的可见性会更改,即离开选项卡(从false
到true
)和进入选项卡(从true
到false
)。这些事件并不总是序列化的。我不得不补充:
onVisibleChanged: {
if (!this.activeFocus){
// ...
}
}
但是,如果您实际上没有单击选项卡,而是通过将选项卡设置active
为 来激活它true
,则上述解决方案是不够的。您必须使用TabView.currentIndex
它与静态(即声明性选项卡索引)进行比较。如果onVisibleChanged
为新的活动选项卡调用,则索引应该相等。
以下是示例:
function refreshButtons(tabIndexStatic){
if (tabStepsView.currentIndex !== tabIndexStatic)
return;
buttonPrev.visible = tabIndexStatic !== 0
buttonNext.visible = tabIndexStatic !== tabStepsView.count - 1
buttonConvert.visible = tabIndexStatic === tabStepsView.count - 1
}
Tab {
title: qsTr("Tab 0")
onVisibleChanged: parent.refreshButtons(0)
// ...
}
Tab {
title: qsTr("Tab 1")
onVisibleChanged: parent.refreshButtons(1);
// ...
}
Tab {
title: "Tab 2"
onVisibleChanged: tabStepsView.refreshButtons(2)
// ...
}
}