diff --git a/src/tools/qml/qmlviewer/jsdebuggeragent.cpp b/src/tools/qml/qmlviewer/jsdebuggeragent.cpp
index 450a3837a4a02a73c259fb5381820b2b44a1e6c2..6e2553cb84f65f059eb5cdb9f2dd79faf70cdf50 100644
--- a/src/tools/qml/qmlviewer/jsdebuggeragent.cpp
+++ b/src/tools/qml/qmlviewer/jsdebuggeragent.cpp
@@ -130,12 +130,16 @@ void JSDebuggerAgent::positionChange(qint64 scriptId,
     // check breakpoints
     if (!breakpointList.isEmpty()) {
         QHash<qint64, QString>::const_iterator it = filenames.constFind(scriptId);
-        if (it != filenames.constEnd()) {
-            QPair<QString, qint32> key = qMakePair(*it, lineNumber);
-            if (breakpointList.contains(key)) {
-                stopped();
-                return;
-            }
+        if (it == filenames.constEnd()) {
+            // It is possible that the scripts are loaded before the agent is attached
+            QString filename = QUrl(QScriptContextInfo(engine()->currentContext()).fileName()).toLocalFile();
+            QPair<QString, qint32> key = qMakePair(filename, lineNumber);
+            it = filenames.insert(scriptId, filename);
+        }
+        QPair<QString, qint32> key = qMakePair(*it, lineNumber);
+        if (breakpointList.contains(key)) {
+            stopped();
+            return;
         }
     }