可以通过遍历QTab中所有的Tab,判断当前需要查找的Tab是否已经存在。
以下是用C++实现代码:
int findTabIndex(QTabWidget *tabWidget, QString tabName) {
int tabIndex = -1;
for (int i = 0; i < tabWidget->count(); i++) {
if (tabWidget->tabText(i) == tabName) {
tabIndex = i;
break;
}
}
return tabIndex;
}
// 判断某个Tab是否已经存在
bool isTabExist(QTabWidget *tabWidget, QString tabName) {
return findTabIndex(tabWidget, tabName) != -1;
}
使用方法:
QTabWidget *tabWidget = new QTabWidget(this);
QString tabName = "Tab1";
if (isTabExist(tabWidget, tabName)) {
// Tab已经存在
} else {
// 创建新的Tab
QWidget *newTab = new QWidget();
tabWidget->addTab(newTab, tabName);
}