From a03c88f2918039e767db6003cad7a65fa756a450 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Sun, 12 Oct 2025 15:50:31 +0800 Subject: [PATCH] fix: tab close button does not work --- tabwidget.cpp | 62 ++++++++++++++++++++++++++++++++------------------- tabwidget.h | 1 - 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/tabwidget.cpp b/tabwidget.cpp index 97a21ee..09da450 100644 --- a/tabwidget.cpp +++ b/tabwidget.cpp @@ -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 newTabToDocumentId; - QHash 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 newTabToDocumentId; + QHash 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); } diff --git a/tabwidget.h b/tabwidget.h index 398016f..96fad21 100644 --- a/tabwidget.h +++ b/tabwidget.h @@ -35,7 +35,6 @@ public: signals: void currentEditorChanged(SciEdit *editor); - void tabCloseRequested(int index); private slots: void onCurrentChanged(int index);