Fixed takeWidget() function and fixed setWidget() function to handle case if there is already a content widget

This commit is contained in:
Uwe Kindler 2020-04-09 21:44:21 +02:00
parent 9fe1dd6a88
commit 7a17aba42d
2 changed files with 30 additions and 4 deletions

View File

@ -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());

View File

@ -257,6 +257,11 @@ void CDockWidget::setToggleViewActionChecked(bool Checked)
//============================================================================
void CDockWidget::setWidget(QWidget* widget, eInsertMode InsertMode)
{
if (d->Widget)
{
takeWidget();
}
QScrollArea* ScrollAreaWidget = qobject_cast<QScrollArea*>(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;
}