当我知道一个活动的标签索引和标签数时,如何关闭所有在QTabWidget
中处于活动状态的标签?关闭所有标签页的功能是tabwidget->clear();
您尝试过这个吗?
// remove all tabs after current
for (int i = tabWidget.count() - 1; i > tabWidget.currentIndex(); --i) {
tabWidget.removeTab(i);
}
// current tab is now the last, therefore remove all but the last
for (int i = tabWidget.count(); i > 1; --i) {
tabWidget.removeTab(0);
}
有效的解决方案,该类来自于继承QTabWidget:
void closeOtherTabsRequested(int index)
{
auto selectedWidget = widget(index);
auto tabCount = mTabBar->count();
for(auto i = tabCount - 1; i >= 0; i--) {
auto currentWidget = widget(i);
if(currentWidget != selectedWidget) {
tabCloseRequested(i);
}
}
}