QQmlApplicationEngine.loadData freezing on a source that QQmlApplicationEngine.load runs ok
Solved
Qt for Python
-
I'm having a baffling issue with QQmlApplicationEngine.loadData, in which it freezes on the following code:
# loads and translates the QML source qmlPath = applicationPath.with_suffix('.qml') qmlSource = qmlOpenSource(qmlPath) qmlSource = qmlTranslateAllMenus(qmlSource, applicationPath, platform=PLATFORM_MENUS) with open('debug_source.qml', 'wt', encoding='utf-8') as debugSorce: print(qmlSource, file=debugSorce) # loads the QML engine qmlSource = qmlSource.encode() qmlPathStr = str(qmlPath) engine.loadData(qmlSource, url=qmlPathStr) # engine.load('debug_source.qml')
If I comment the loadData (next-to-last line) and uncomment the last line, the code runs perfectly! Any idea on what might be happening?
P.S.: I should have mentioned that I ran some toy tests with the above code that went totally okay, but when I try it on a larger source it freezes.
-
I found out what the problem is. A filesystem path does not count as a URL for loadData. It must be a well-formatted URI, otherwise the Engine freezes without any error message. The fix below solves the problem:
# loads the QML engine qmlSource = qmlSource.encode() qmlPathStr = qmlPath.as_uri() # converts Path object into string containing the URI engine.loadData(qmlSource, url=qmlPathStr)