Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Fix crash if layout factory failed to create view from XML element #866

Merged
Merged
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
29 changes: 29 additions & 0 deletions Libs/Widgets/Testing/Cpp/ctkLayoutManagerTest1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ QString nestedLayout(
" <item><view name=\"tab3\"/></item>"
"</layout>");

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

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -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()));
Expand Down
15 changes: 13 additions & 2 deletions Libs/Widgets/ctkLayoutManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -475,6 +479,10 @@ QWidgetItem* ctkLayoutManager::widgetItemFromXML(QDomElement viewElement)
{
//Q_ASSERT(viewElement.tagName() == "view");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can remove this comment

QWidget* view = this->viewFromXML(viewElement);
if (!view)
{
return 0;
}
this->setupView(viewElement, view);
return new QWidgetItem(view);
}
Expand All @@ -484,7 +492,10 @@ void ctkLayoutManager::setupView(QDomElement viewElement, QWidget* view)
{
Q_UNUSED(viewElement);
Q_D(ctkLayoutManager);
Q_ASSERT(view);
if (!view)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we add a warning message somewhere if view creation fails?

{
return;
}
view->setVisible(true);
d->Views.insert(view);
}
Expand Down