如何关闭所有选项卡,但在Qt4(QTabWidget)中处于活动状态

问题描述 投票:2回答:2

当我知道一个活动的标签索引和标签数时,如何关闭所有在QTabWidget中处于活动状态的标签?关闭所有标签页的功能是tabwidget->clear();

c++ c qt qt4
2个回答
2
投票

您尝试过这个吗?

// 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);
}

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);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.