Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 54 additions & 13 deletions src/DockAreaWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,42 @@ class CDockAreaLayout
return m_Widgets.count();
}

/**
* Removes the current content widget from the box layout by widget identity.
* Never use hardcoded takeAt(1): with TabsAtBottom the TabBar shares this
* layout and may sit at index 1, so takeAt(1) would orphan it as a top-level
* window (blank 200x44 popup, workspace tabs disappear).
*/
void removeCurrentWidgetFromBoxLayout()
{
if (!m_CurrentWidget)
return;

int idx = m_ParentLayout->indexOf(m_CurrentWidget);
if (idx < 0)
{
detachWidget(m_CurrentWidget);
return;
}

QLayoutItem* LayoutItem = m_ParentLayout->takeAt(idx);
if (!LayoutItem)
return;

QWidget* taken = LayoutItem->widget();
if (taken && (qobject_cast<CDockAreaTabBar*>(taken) || qobject_cast<CDockAreaTitleBar*>(taken)))
{
m_ParentLayout->insertWidget(idx, taken);
delete LayoutItem;
detachWidget(m_CurrentWidget);
return;
}

if (taken)
detachWidget(taken);
delete LayoutItem;
}

/**
* Inserts the widget at the given index position into the internal widget
* list
Expand Down Expand Up @@ -150,12 +186,7 @@ class CDockAreaLayout
{
if (currentWidget() == Widget)
{
auto LayoutItem = m_ParentLayout->takeAt(1);
if (LayoutItem)
{
detachWidget(LayoutItem->widget());
}
delete LayoutItem;
removeCurrentWidgetFromBoxLayout();
m_CurrentWidget = nullptr;
m_CurrentIndex = -1;
}
Expand Down Expand Up @@ -195,17 +226,23 @@ class CDockAreaLayout
parent->setUpdatesEnabled(false);
}

if (m_CurrentWidget)
removeCurrentWidgetFromBoxLayout();

int insertAt = 1;
if (m_ParentLayout->count() > 0)
{
auto LayoutItem = m_ParentLayout->takeAt(1);
if (LayoutItem)
for (int i = 0; i < m_ParentLayout->count(); ++i)
{
detachWidget(LayoutItem->widget());
QLayoutItem* it = m_ParentLayout->itemAt(i);
QWidget* w = it ? it->widget() : nullptr;
if (qobject_cast<CDockAreaTabBar*>(w))
{
insertAt = i;
break;
}
}
delete LayoutItem;
}

m_ParentLayout->insertWidget(1, next);
m_ParentLayout->insertWidget(insertAt, next);
if (prev)
{
prev->hide();
Expand Down Expand Up @@ -572,6 +609,10 @@ void CDockAreaWidget::insertDockWidget(int index, CDockWidget* DockWidget,
{
setCurrentIndex(index);
DockWidget->setClosedState(false); // Set current index can show the widget without changing the close state, added to keep the close state consistent
if (!DockWidget->isClosed())
{
TabWidget->setVisible(true);
}
}
// If this dock area is hidden, then we need to make it visible again
// by calling DockWidget->toggleViewInternal(true);
Expand Down
6 changes: 5 additions & 1 deletion src/DockWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,11 @@ QWidget* CDockWidget::takeWidget()

if (w)
{
w->setParent(nullptr);
// Native 子控件(如 OpenGL)不能 setParent(nullptr),否则会留下空白顶层 OS 窗口
if (w->internalWinId() && d->DockManager)
w->setParent(d->DockManager);
else
w->setParent(nullptr);
}
return w;
}
Expand Down