fix: tab close button does not work

This commit is contained in:
2025-10-12 15:50:31 +08:00
parent 7626508462
commit a03c88f291
2 changed files with 39 additions and 24 deletions

View File

@ -140,12 +140,18 @@ void TabWidget::closeCurrentTab()
bool TabWidget::closeTab(int index)
{
qDebug() << "closeTab called with index:" << index << "count:" << count();
if (index < 0 || index >= count()) {
qDebug() << "Invalid index, returning false";
return false;
}
int docId = m_tabToDocumentId.value(index, -1);
qDebug() << "Document ID for index" << index << "is" << docId;
if (docId == -1) {
qDebug() << "No document ID found for index, returning false";
return false;
}
@ -185,29 +191,7 @@ bool TabWidget::closeTab(int index)
}
}
// 移除标签页
QWidget *widget = this->widget(index);
removeTab(index);
// 清理映射关系
m_tabToDocumentId.remove(index);
m_documentIdToTab.remove(docId);
// 更新其他标签页的索引映射
QHash<int, int> newTabToDocumentId;
QHash<int, int> newDocumentIdToTab;
for (int i = 0; i < count(); ++i) {
int oldIndex = (i >= index) ? i + 1 : i;
int oldDocId = m_tabToDocumentId.value(oldIndex, -1);
if (oldDocId != -1) {
newTabToDocumentId[i] = oldDocId;
newDocumentIdToTab[oldDocId] = i;
}
}
m_tabToDocumentId = newTabToDocumentId;
m_documentIdToTab = newDocumentIdToTab;
qDebug() << "Proceeding to close tab";
// 清理编辑器和文档
if (m_editors.contains(docId)) {
@ -218,6 +202,36 @@ bool TabWidget::closeTab(int index)
m_documentManager->closeDocument(docId);
// 移除标签页
QWidget *widget = this->widget(index);
removeTab(index);
// 清理和更新映射关系
m_tabToDocumentId.remove(index);
m_documentIdToTab.remove(docId);
// 更新后续标签页的索引映射索引减1
QHash<int, int> newTabToDocumentId;
QHash<int, int> newDocumentIdToTab;
for (auto it = m_tabToDocumentId.begin(); it != m_tabToDocumentId.end(); ++it) {
int tabIndex = it.key();
int documentId = it.value();
if (tabIndex > index) {
// 后续标签页的索引需要减1
newTabToDocumentId[tabIndex - 1] = documentId;
newDocumentIdToTab[documentId] = tabIndex - 1;
} else {
// 前面的标签页索引不变
newTabToDocumentId[tabIndex] = documentId;
newDocumentIdToTab[documentId] = tabIndex;
}
}
m_tabToDocumentId = newTabToDocumentId;
m_documentIdToTab = newDocumentIdToTab;
if (widget) {
widget->deleteLater();
}
@ -227,6 +241,7 @@ bool TabWidget::closeTab(int index)
newDocument();
}
qDebug() << "Tab closed successfully";
return true;
}
@ -280,6 +295,7 @@ void TabWidget::onCurrentChanged(int index)
void TabWidget::onTabCloseRequested(int index)
{
qDebug() << "Tab close requested for index:" << index;
closeTab(index);
}

View File

@ -35,7 +35,6 @@ public:
signals:
void currentEditorChanged(SciEdit *editor);
void tabCloseRequested(int index);
private slots:
void onCurrentChanged(int index);