diff --git a/src/designer/src/components/formeditor/formwindow.cpp b/src/designer/src/components/formeditor/formwindow.cpp
index fdaed87..9b66eaf 100644
--- a/src/designer/src/components/formeditor/formwindow.cpp
+++ b/src/designer/src/components/formeditor/formwindow.cpp
@@ -1608,6 +1608,8 @@ void FormWindow::createLayout(int type, QWidget *container)
         layoutContainer(container, type);
     } else {
         LayoutCommand *cmd = new LayoutCommand(this);
+        qDebug() << __FUNCTION__ << "on mainContainer()" << mainContainer()
+                 << "passing layoutbase=0";
         cmd->init(mainContainer(), selectedWidgets(), static_cast<LayoutInfo::Type>(type));
         commandHistory()->push(cmd);
     }
@@ -2182,10 +2184,13 @@ bool FormWindow::setContents(const QString &contents)
 
 void FormWindow::layoutContainer(QWidget *w, int type)
 {
+    qDebug() << __FUNCTION__ << w << type;
     if (w == this)
         w = mainContainer();
+    qDebug() << __FUNCTION__ << "mc coor" << w;
 
     w = core()->widgetFactory()->containerOfWidget(w);
+    qDebug() << __FUNCTION__ << "containerOfWidget" << w;
 
     const QObjectList l = w->children();
     if (l.isEmpty())
@@ -2199,8 +2204,10 @@ void FormWindow::layoutContainer(QWidget *w, int type)
             if (widget->isVisibleTo(this) && isManaged(widget))
                 widgets.append(widget);
         }
+    qDebug() << __FUNCTION__ << "widgets=" << widgets;
 
     LayoutCommand *cmd = new LayoutCommand(this);
+    qDebug() << __FUNCTION__ << "on mainContainer()" << mainContainer() << "passing layoutbase=" << w;
     cmd->init(mainContainer(), widgets, static_cast<LayoutInfo::Type>(type), w);
     clearSelection(false);
     commandHistory()->push(cmd);
diff --git a/src/designer/src/lib/shared/layout.cpp b/src/designer/src/lib/shared/layout.cpp
index 789f185..d0a8dda 100644
--- a/src/designer/src/lib/shared/layout.cpp
+++ b/src/designer/src/lib/shared/layout.cpp
@@ -67,6 +67,10 @@
 
 #include <algorithm>
 
+#ifdef QT_DIAG_LIB
+#  include <qwidgetdump.h>
+#endif
+
 QT_BEGIN_NAMESPACE
 
 enum { FormLayoutColumns = 2 };
@@ -122,6 +126,7 @@ Layout::Layout(const QWidgetList &wl, QWidget *p, QDesignerFormWindowInterface *
     m_reparentLayoutWidget(true),
     m_isBreak(false)
 {
+    qDebug() << __FUNCTION__ << "m_layoutBase=" << m_layoutBase;
     if (m_layoutBase)
         m_oldGeometry = m_layoutBase->geometry();
 }
@@ -138,6 +143,9 @@ Layout::~Layout()
 
 void Layout::setup()
 {
+    qDebug() << __FUNCTION__ <<"m_widgets=" << m_widgets
+             << "m_layoutBase=" << m_layoutBase;
+
     m_startPoint = QPoint(32767, 32767);
 
     // Go through all widgets of the list we got. As we can only
@@ -152,8 +160,11 @@ void Layout::setup()
         QWidget *p = w->parentWidget();
 
         if (p && LayoutInfo::layoutType(m_formWindow->core(), p) != LayoutInfo::NoLayout
-                && m_formWindow->core()->metaDataBase()->item(p->layout()) != 0)
+                && m_formWindow->core()->metaDataBase()->item(p->layout()) != 0) {
+            qDebug() << __FUNCTION__ <<"skip" << w;
             continue;
+        }
+        qDebug() << __FUNCTION__ <<"inserting" << p << w;
 
         lists.insert(p, w);
     }
@@ -163,15 +174,22 @@ void Layout::setup()
     foreach (QWidget *p, parents) {
         QWidgetList children = lists.values(p);
 
-        if (children.count() > lastList.count())
+        if (children.count() > lastList.count()) {
+            qDebug() << "assigning" << p << children;
             lastList = children;
+        }
     }
+    qDebug() << "lastList=" <<lastList;
 
 
     // If we found no list (because no widget did fit at all) or the
     // best list has only one entry and we do not layout a container,
     // we leave here.
     QDesignerWidgetDataBaseInterface *widgetDataBase = m_formWindow->core()->widgetDataBase();
+    if (m_layoutBase) {
+        qDebug() << "isContainer" << widgetDataBase->isContainer(m_layoutBase, false);
+    }
+
     if (lastList.count() < 2 &&
                         (!m_layoutBase ||
                           (!widgetDataBase->isContainer(m_layoutBase, false) &&
@@ -179,6 +197,7 @@ void Layout::setup()
                        ) {
         m_widgets.clear();
         m_startPoint = QPoint(0, 0);
+        qDebug() << "clear/return";
         return;
     }
 
@@ -187,6 +206,10 @@ void Layout::setup()
     m_widgets = lastList;
     // Also use the only correct parent later, so store it
 
+#ifdef QT_DIAG_LIB
+    QtDiag::dumpAllWidgets(QtDiag::DontPrintWindowFlags, m_formWindow);
+#endif
+
     Q_ASSERT(m_widgets.isEmpty() == false);
 
     m_parentWidget = m_formWindow->core()->widgetFactory()->widgetOfContainer(m_widgets.first()->parentWidget());
@@ -1270,6 +1293,8 @@ Layout* Layout::createLayout(const QWidgetList &widgets,  QWidget *parentWidget,
                              QDesignerFormWindowInterface *fw,
                              QWidget *layoutBase, LayoutInfo::Type layoutType)
 {
+    qDebug() << __FUNCTION__ << widgets << "parentWidget="
+             << parentWidget << "layoutBase=" << layoutBase;
     switch (layoutType) {
     case LayoutInfo::Grid:
         return new GridLayout<QGridLayout, LayoutInfo::Grid, Grid::GridLayout>(widgets, parentWidget, fw, layoutBase);
diff --git a/src/designer/src/lib/shared/shared.pri b/src/designer/src/lib/shared/shared.pri
index cf530af..a0e66b6 100644
--- a/src/designer/src/lib/shared/shared.pri
+++ b/src/designer/src/lib/shared/shared.pri
@@ -17,6 +17,12 @@ include(../../../../shared/deviceskin/deviceskin.pri)
 include(../../../../shared/findwidget/findwidget.pri)
 include(../../../../shared/qtgradienteditor/qtgradienteditor.pri)
 
+DIAGLIB =
+exists($$(QTDIR)):DIAGLIB = $$(QTDIR)/tests/manual/diaglib
+# Building against Qt4 or QTDIR not set:
+!exists($$DIAGLIB):DIAGLIB=d:/dev/5-vs15-56/qt-56/qtbase/tests/manual/diaglib
+exists($$DIAGLIB):include($$DIAGLIB/diaglib.pri)
+
 # Input
 FORMS += $$PWD/addlinkdialog.ui \
     $$PWD/orderdialog.ui \
