From b07acdd2b6321689da52cf16cfba5d99103f1303 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Thu, 25 Apr 2019 10:37:46 -0400 Subject: [PATCH] BUG: Fix crash if layout factory failed to create view from XML element --- .../Testing/Cpp/ctkLayoutManagerTest1.cpp | 29 +++++++++++++++++++ Libs/Widgets/ctkLayoutManager.cpp | 15 ++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/Libs/Widgets/Testing/Cpp/ctkLayoutManagerTest1.cpp b/Libs/Widgets/Testing/Cpp/ctkLayoutManagerTest1.cpp index 497187421d..393669becb 100644 --- a/Libs/Widgets/Testing/Cpp/ctkLayoutManagerTest1.cpp +++ b/Libs/Widgets/Testing/Cpp/ctkLayoutManagerTest1.cpp @@ -77,6 +77,22 @@ QString nestedLayout( " " ""); +namespace +{ +class ctkNullViewFactory: public ctkLayoutViewFactory +{ +public: + ctkNullViewFactory(QObject* parent = 0) : ctkLayoutViewFactory(parent) + { + this->setUseCachedViews(true); + } + virtual QWidget* createViewFromXML(QDomElement layoutElement){ + Q_UNUSED(layoutElement); + return 0; + } +}; +} + /// \ingroup Widgets //----------------------------------------------------------------------------- @@ -287,6 +303,19 @@ int ctkLayoutManagerTest1(int argc, char * argv [] ) return EXIT_FAILURE; } + // Test that factories failing to instantiate view do not lead to a "crash" + ctkNullViewFactory* nullViewInstanciator = new ctkNullViewFactory(&viewport); + QWidget nullView; + nullView.setWindowTitle("Layout with Invalid View"); + ctkLayoutFactory nullViewLayoutManager; + nullViewLayoutManager.registerViewFactory(nullViewInstanciator); + nullViewLayoutManager.setLayout(simpleLayoutDoc); + nullViewLayoutManager.setViewport(&nullView); + nestedToTab.show(); + + QTimer::singleShot(200, &app, SLOT(quit())); + app.exec(); + if (argc < 2 || QString(argv[1]) != "-I" ) { QTimer::singleShot(200, &app, SLOT(quit())); diff --git a/Libs/Widgets/ctkLayoutManager.cpp b/Libs/Widgets/ctkLayoutManager.cpp index f072b8e867..93e14b411a 100644 --- a/Libs/Widgets/ctkLayoutManager.cpp +++ b/Libs/Widgets/ctkLayoutManager.cpp @@ -415,7 +415,11 @@ void ctkLayoutManager::processItemElement(QDomElement itemElement, QLayoutItem* } else { - childrenItem << this->processElement(itemElement.firstChild().toElement()); + QLayoutItem* layoutItem = this->processElement(itemElement.firstChild().toElement()); + if (layoutItem) + { + childrenItem << layoutItem; + } } foreach(QLayoutItem* item, childrenItem) { @@ -475,6 +479,10 @@ QWidgetItem* ctkLayoutManager::widgetItemFromXML(QDomElement viewElement) { //Q_ASSERT(viewElement.tagName() == "view"); QWidget* view = this->viewFromXML(viewElement); + if (!view) + { + return 0; + } this->setupView(viewElement, view); return new QWidgetItem(view); } @@ -484,7 +492,10 @@ void ctkLayoutManager::setupView(QDomElement viewElement, QWidget* view) { Q_UNUSED(viewElement); Q_D(ctkLayoutManager); - Q_ASSERT(view); + if (!view) + { + return; + } view->setVisible(true); d->Views.insert(view); }