From 7a17aba42d42a633c9b4635effededfd9c3a6d07 Mon Sep 17 00:00:00 2001 From: Uwe Kindler Date: Thu, 9 Apr 2020 21:44:21 +0200 Subject: [PATCH] Fixed takeWidget() function and fixed setWidget() function to handle case if there is already a content widget --- demo/MainWindow.cpp | 5 +++++ src/DockWidget.cpp | 29 +++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/demo/MainWindow.cpp b/demo/MainWindow.cpp index 5459ab3..5650540 100644 --- a/demo/MainWindow.cpp +++ b/demo/MainWindow.cpp @@ -169,7 +169,12 @@ static ads::CDockWidget* createCalendarDockWidget(QMenu* ViewMenu) static int CalendarCount = 0; QCalendarWidget* w = new QCalendarWidget(); ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Calendar %1").arg(CalendarCount++)); + // The following lines are for testing the setWidget() and takeWidget() + // functionality DockWidget->setWidget(w); + DockWidget->setWidget(w); // what happens if we set a widget if a widget is already set + DockWidget->takeWidget(); // we remove the widget + DockWidget->setWidget(w); // and set the widget again - there should be no error DockWidget->setToggleViewActionMode(ads::CDockWidget::ActionModeShow); DockWidget->setIcon(svgIcon(":/adsdemo/images/date_range.svg")); ViewMenu->addAction(DockWidget->toggleViewAction()); diff --git a/src/DockWidget.cpp b/src/DockWidget.cpp index 9de729d..8099f21 100644 --- a/src/DockWidget.cpp +++ b/src/DockWidget.cpp @@ -257,6 +257,11 @@ void CDockWidget::setToggleViewActionChecked(bool Checked) //============================================================================ void CDockWidget::setWidget(QWidget* widget, eInsertMode InsertMode) { + if (d->Widget) + { + takeWidget(); + } + QScrollArea* ScrollAreaWidget = qobject_cast(widget); if (ScrollAreaWidget || ForceNoScrollArea == InsertMode) { @@ -280,10 +285,26 @@ void CDockWidget::setWidget(QWidget* widget, eInsertMode InsertMode) //============================================================================ QWidget* CDockWidget::takeWidget() { - d->ScrollArea->takeWidget(); - d->Layout->removeWidget(d->Widget); - d->Widget->setParent(nullptr); - return d->Widget; + QWidget* w = nullptr; + if (d->ScrollArea) + { + d->Layout->removeWidget(d->ScrollArea); + w = d->ScrollArea->takeWidget(); + delete d->ScrollArea; + d->ScrollArea = nullptr; + } + else if (d->Widget) + { + d->Layout->removeWidget(d->Widget); + w = d->Widget; + d->Widget = nullptr; + } + + if (w) + { + w->setParent(nullptr); + } + return w; }