@jayrickaby Regarding textDocument.source reset in QT Quick:
I can see you're frustrated — this is a genuinely tricky QQuickTextDocument limitation!
The short answer: You cannot directly "unset" textDocument.source once it's been set via QML/JS. Qt doesn't expose a clean reset method for it.
The real solution — handle it from Python backend:
pythonfrom PySide6.QtCore import QObject, Slot, Signal
class Backend(QObject):
fileCleared = Signal()
requestSaveAs = Signal()
def __init__(self):
super().__init__()
self.current_path = None # None = unsaved new file
@Slot()
def new_file(self):
self.current_path = None
self.fileCleared.emit() # Tell QML to clear the text
@Slot(str)
def save_file(self, text):
if self.current_path is None:
self.requestSaveAs.emit()
else:
with open(self.current_path, 'w') as f:
f.write(text)
@Slot(str)
def open_file(self, path):
self.current_path = path
qmlTextEdit {
id: textEdit
}
Connections {
target: backend
function onFileCleared() {
textEdit.clear()
// Don't touch textDocument.source at all
}
}
Why your attempts failed:
source = "" — Qt tries to resolve empty string as a URL, document loses context
source = undefined — QUrl binding is strict, doesn't accept undefined
new URL() — wrong argument count for QUrl constructor
textEdit.clear() — works but leaves source stale (which is fine!)
The key insight: textDocument.source is only meant for loading files. For a "New File" action, just track the file path in Python as None, call textEdit.clear() in QML, and never touch textDocument.source until the user actually opens or saves a real file. The stale source does not affect editing at all.
Edit: removed spam content