Index: /trunk/src/gui/kernel/qwidget_pm.cpp
===================================================================
--- /trunk/src/gui/kernel/qwidget_pm.cpp	(revision 105)
+++ /trunk/src/gui/kernel/qwidget_pm.cpp	(revision 106)
@@ -107,7 +107,8 @@
 }
 
-// returns the human readable widget id in the form "name/class".
-// used for debugging.
-static QString widgetName(HWND hwnd)
+// Returns a QWidget pointer or 0 if there is no widget corresponding to the
+// given HWND. As opposed to QWidget::find(), correctly handles WC_FRAME windows
+// created for top level widgets. Used for debugging.
+static QWidget *widgetFromId(HWND hwnd)
 {
     char buf[10];
@@ -115,11 +116,18 @@
         if (!strcmp(buf, "#1")) // WC_FRAME
             hwnd = WinWindowFromID(hwnd, FID_CLIENT);
-        QWidget *w = QWidget::find(hwnd);
-        if (w)
-            return QString().sprintf("%s/%s",
-                                     w->metaObject()->className(),
-                                     w->objectName().toLocal8Bit().constData());
-    }
-    return QLatin1String("<non-Qt-widget>");
+        return QWidget::find(hwnd);
+    }
+    return 0;
+}
+
+// Returns a human readable widget name in the form "class/name".
+// Used for debugging.
+static QByteArray widgetName(QWidget *w)
+{
+    if (w)
+        return QString()
+            .sprintf("%s/%s", w->metaObject()->className(),
+                     w->objectName().toLocal8Bit().constData()).toLocal8Bit();
+    return QByteArray("<no-widget>");
 }
 
@@ -160,7 +168,7 @@
 {
 #if defined(QT_DEBUGWIDGETMASK)
-    qDebug("qt_WinInvalidateRegionEx: hwnd=%08lX (%s) "
+    qDebug("qt_WinInvalidateRegionEx: hwnd=%08lX [%s] "
            "hwndFrom=%08lX hwndTo=%08lX", hwnd,
-           widgetName(hwnd).toLocal8Bit().constData(),
+           widgetName(widgetFromId(hwnd)).constData(),
            hwndFrom, hwndTo);
 #endif
@@ -191,6 +199,6 @@
         WinQueryWindowPos(child, &swp);
 #if defined(QT_DEBUGWIDGETMASK)
-        qDebug(" child=%08lX [fl=%08lX] (%s)", child, swp.fl,
-               widgetName(child).toLocal8Bit().constData());
+        qDebug(" child=%08lX [fl=%08lX] [%s]", child, swp.fl,
+               widgetName(widgetFromId(child)).constData());
 #endif
         // proceed only if not hidden
@@ -276,7 +284,7 @@
 
 #if defined(QT_DEBUGWIDGETMASK)
-    qDebug("qt_WinProcessWindowObstacles: hwnd=%08lX (%s), prcl=%p "
+    qDebug("qt_WinProcessWindowObstacles: hwnd=%08lX [%s], prcl=%p "
            "hrgn=%08lX, op=%ld flags=%08lX", hwnd,
-           widgetName(hwnd).toLocal8Bit().constData(),
+           widgetName(widgetFromId(hwnd)).constData(),
            prcl, hrgn, op, flags);
 #endif
@@ -330,6 +338,6 @@
                 WinQueryWindowPos(relative, &swp);
 #if defined(QT_DEBUGWIDGETMASK)
-                qDebug(" child=%08lX [fl=%08lX] (%s)", relative, swp.fl,
-                       widgetName(relative).toLocal8Bit().constData());
+                qDebug(" child=%08lX [fl=%08lX] [%s]", relative, swp.fl,
+                       widgetName(widgetFromId(relative)).constData());
 #endif
                 // skip if hidden
@@ -370,6 +378,6 @@
             WinQueryWindowPos(relative, &swp);
 #if defined(QT_DEBUGWIDGETMASK)
-            qDebug(" sibling=%08lX [fl=%08lX] (%s)", relative, swp.fl,
-                   widgetName(relative).toLocal8Bit().constData());
+            qDebug(" sibling=%08lX [fl=%08lX] [%s]", relative, swp.fl,
+                   widgetName(widgetFromId(relative)).constData());
 #endif
             // skip if hidden
@@ -420,6 +428,6 @@
             WinQueryWindowPos(parent, &swp);
 #if defined(QT_DEBUGWIDGETMASK)
-            qDebug(" parent=%08lX [fl=%08lX] (%s)", parent, swp.fl,
-                   widgetName(parent).toLocal8Bit().constData());
+            qDebug(" parent=%08lX [fl=%08lX] [%s]", parent, swp.fl,
+                   widgetName(widgetFromId(parent)).constData());
 #endif
             delta.x += swp.x;
@@ -429,6 +437,6 @@
                 WinQueryWindowPos(relative, &swp);
 #if defined(QT_DEBUGWIDGETMASK)
-                qDebug(" ancestor=%08lX [fl=%08lX] (%s)", relative, swp.fl,
-                       widgetName(parent).toLocal8Bit().constData());
+                qDebug(" ancestor=%08lX [fl=%08lX] [%s]", relative, swp.fl,
+                       widgetName(widgetFromId(parent)).constData());
 #endif
                 // skip if hidden
@@ -480,6 +488,6 @@
 {
 #if defined(QT_DEBUGWIDGETMASK)
-    qDebug("qt_WinSetWindowPos: hwnd=%08lX (%s) fl=%08lX", hwnd,
-           widgetName(hwnd).toLocal8Bit().constData(), fl);
+    qDebug("qt_WinSetWindowPos: hwnd=%08lX [%s] fl=%08lX", hwnd,
+           widgetName(widgetFromId(hwnd)).constData(), fl);
 #endif
 
@@ -568,8 +576,8 @@
 #if defined(QT_DEBUGWIDGETMASK)
         qDebug(" moving up? %ld", up);
-        qDebug(" hwndFrom=%08lX (%s)", hwndFrom,
-               widgetName(hwndFrom).toLocal8Bit().constData());
-        qDebug(" hwndTo=%08lX (%s)", hwndTo,
-               widgetName(hwndTo).toLocal8Bit().constData());
+        qDebug(" hwndFrom=%08lX [%s]", hwndFrom,
+               widgetName(widgetFromId(hwndFrom)).constData());
+        qDebug(" hwndTo=%08lX [%s]", hwndTo,
+               widgetName(widgetFromId(hwndTo)).constData());
 #endif
 
@@ -584,6 +592,6 @@
                 WinQueryWindowPos(sibling, &swp);
 #if defined(QT_DEBUGWIDGETMASK)
-                qDebug(" sibling=%08lX [fl=%08lX] (%s)", sibling, swp.fl,
-                       widgetName(sibling).toLocal8Bit().constData());
+                qDebug(" sibling=%08lX [fl=%08lX] [%s]", sibling, swp.fl,
+                       widgetName(widgetFromId(sibling)).constData());
 #endif
                 // proceed only if not hidden
@@ -615,6 +623,6 @@
                 WinQueryWindowPos(sibling, &swp);
 #if defined(QT_DEBUGWIDGETMASK)
-                qDebug(" sibling=%08lX [fl=%08lX] (%s)", sibling, swp.fl,
-                       widgetName(sibling).toLocal8Bit().constData());
+                qDebug(" sibling=%08lX [fl=%08lX] [%s]", sibling, swp.fl,
+                       widgetName(widgetFromId(sibling)).constData());
 #endif
                 // proceed only if not hidden
@@ -889,12 +897,11 @@
             }
 #if defined(QT_DEBUGWINCREATEDESTROY)
-            qDebug("|Creating top level window (frame) [%s/%s]:\n"
+            qDebug("|Creating top level window (frame) [%s]:\n"
                    "|  owner = %08lX\n"
                    "|  title = '%s'\n"
                    "|  style = %08lX\n"
                    "|  fcFlags = %08lX",
-                   q->objectName().toLocal8Bit().constData(),
-                   q->metaObject()->className(),
-                   ownerw, title.constData(), fStyle, fcFlags);
+                   widgetName(q).constData(), ownerw, title.constData(), fStyle,
+                   fcFlags);
 #endif
             fId = WinCreateWindow(HWND_DESKTOP, WC_FRAME, title, fStyle,
@@ -918,12 +925,11 @@
             // create client window
 #if defined(QT_DEBUGWINCREATEDESTROY)
-            qDebug("|Creating top level window (client) [%s/%s]:\n"
+            qDebug("|Creating top level window (client) [%s]:\n"
                    "|  owner & parent = %08lX\n"
                    "|  class = '%s'\n"
                    "|  title = '%s'\n"
                    "|  style = %08lX",
-                   q->objectName().toLocal8Bit().constData(),
-                   q->metaObject()->className(),
-                   fId, className.constData(), title.constData(), style);
+                   widgetName(q).constData(), fId, className.constData(),
+                   title.constData(), style);
 #endif
             // note that we place the client on top (HWND_TOP) to exclude other
@@ -933,11 +939,10 @@
         } else {
 #if defined(QT_DEBUGWINCREATEDESTROY)
-            qDebug("|Creating top level window (popup) [%s/%s]:\n"
+            qDebug("|Creating top level window (popup) [%s]:\n"
                    "|  class = '%s'\n"
                    "|  title = '%s'\n"
                    "|  style = %08lX",
-                   q->objectName().toLocal8Bit().constData(),
-                   q->metaObject()->className(),
-                   className.constData(), title.constData(), style);
+                   widgetName(q).constData(), className.constData(),
+                   title.constData(), style);
 #endif
             id = WinCreateWindow(HWND_DESKTOP, className, title, style,
@@ -1047,12 +1052,11 @@
 
 #if defined(QT_DEBUGWINCREATEDESTROY)
-        qDebug("|Creating child window [%s/%s]:\n"
+        qDebug("|Creating child window [%s]:\n"
                "|  owner & parent = %08lX\n"
                "|  class = '%s'\n"
                "|  title = '%s'\n"
                "|  style = %08lX",
-               q->objectName().toLocal8Bit().constData(),
-               q->metaObject()->className(),
-               parentw, className.constData(), title.constData(), style);
+               widgetName(q).constData(), parentw, className.constData(),
+               title.constData(), style);
 #endif
         id = WinCreateWindow(parentw, className, title, style,
@@ -1126,8 +1130,6 @@
             }
 #if defined(QT_DEBUGWINCREATEDESTROY)
-            qDebug("|Destroying window [%s/%s]:\n"
-                   "|  hwnd = %08lX",
-                   objectName().toLocal8Bit().constData(),
-                   metaObject()->className(), id);
+            qDebug("|Destroying window [%s]:\n"
+                   "|  hwnd = %08lX", widgetName(this).constData(), id);
 #endif
 
