<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to clear TextEdit&#x27;s textDocument.source?]]></title><description><![CDATA[<p dir="auto">Hello. I am making a recreation of Window 7's notepad in QT Quick and Python. As my main text editing body, i am using... The TextEdit QML Type. When the user creates a new file, I would like the QQuickTextDocument's source to be reset (it makes sense, right?). However, no matter what I try, nothing works.<br />
<code>TextEdit.textDocument.source.clear()</code> does not exist.<br />
<code>TextEdit.textDocument.source = ""</code> provides a <code>QML ... (parent or ancestor of TextDocument): does not exist</code><br />
<code>TextEdit.textDocument.source = undefined</code> provides me a <code>Error: Cannot assign [undefined] to QUrl</code><br />
<code>TextEdit.textDocument.source = new URL()</code> provides me a <code>Error: Invalid amount of arguments</code><br />
<code>TextEdit.clear()</code> clears everything... besides <code>textDocument.source</code><br />
I am all calling these with JS in the QML file.</p>
<p dir="auto">I need something, anything! Is it even possible?!</p>
]]></description><link>https://forum.qt.io/topic/164807/how-to-clear-textedit-s-textdocument.source</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Jul 2026 06:24:18 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/164807.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 17 Jun 2026 21:10:38 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to clear TextEdit&#x27;s textDocument.source? on Sat, 20 Jun 2026 20:03:29 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jayrickaby">@<bdi>jayrickaby</bdi></a> Regarding textDocument.source reset in QT Quick:<br />
I can see you're frustrated — this is a genuinely tricky QQuickTextDocument limitation!<br />
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.<br />
The real solution — handle it from Python backend:<br />
pythonfrom PySide6.QtCore import QObject, Slot, Signal</p>
<p dir="auto">class Backend(QObject):<br />
fileCleared = Signal()<br />
requestSaveAs = Signal()</p>
<pre><code>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
</code></pre>
<p dir="auto">qmlTextEdit {<br />
id: textEdit<br />
}</p>
<p dir="auto">Connections {<br />
target: backend<br />
function onFileCleared() {<br />
textEdit.clear()<br />
// Don't touch textDocument.source at all<br />
}<br />
}<br />
Why your attempts failed:</p>
<p dir="auto">source = "" — Qt tries to resolve empty string as a URL, document loses context<br />
source = undefined — QUrl binding is strict, doesn't accept undefined<br />
new URL() — wrong argument count for QUrl constructor<br />
textEdit.clear() — works but leaves source stale (which is fine!)</p>
<p dir="auto">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.</p>
<p dir="auto"><em>Edit: removed spam content</em></p>
]]></description><link>https://forum.qt.io/post/838832</link><guid isPermaLink="true">https://forum.qt.io/post/838832</guid><dc:creator><![CDATA[David warnor]]></dc:creator><pubDate>Sat, 20 Jun 2026 20:03:29 GMT</pubDate></item><item><title><![CDATA[Reply to How to clear TextEdit&#x27;s textDocument.source? on Fri, 19 Jun 2026 19:03:41 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jayrickaby">@<bdi>jayrickaby</bdi></a> said in <a href="/post/838801">How to clear TextEdit's textDocument.source?</a>:</p>
<blockquote>
<p dir="auto">When the user creates a new file, I would like the QQuickTextDocument's source to be reset (it makes sense, right?).</p>
</blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jayrickaby">@<bdi>jayrickaby</bdi></a> said in <a href="/post/838812">How to clear TextEdit's textDocument.source?</a>:</p>
<blockquote>
<p dir="auto">Despite the QML ... (parent or ancestor of TextDocument): does not exist error, textDocument.source = Qt.resolvedUrl("") IS working how I need it to (even if it doing it in a broken way).<br />
I hope proper support can be added</p>
</blockquote>
<p dir="auto">The described program structure isn't clear to me. Creating a new <code>TextEdit</code> should create a new <code>TextDocument</code> with an empty <code>source</code>. Is this program instead overwriting the <code>text</code> of an existing <code>TextEdit</code>?</p>
<p dir="auto">The program below works as I expect with Qt 6.8.7, based on the documentation. When <code>textDocument.modified</code> == false, <code>textDocument.source</code> can be set to an empty string, but that results in a loading error.</p>
<pre><code>import QtQuick
import QtQuick.Layouts
import QtQuick.Controls

Window {
    visible: true
    ColumnLayout {
        anchors.fill: parent
        TextInput {
            id: source
            focus: true
            Layout.fillWidth: true
            onEditingFinished: edit.textDocument.source = text
            Rectangle {
                border.width: 1
                border.color: "black"
                anchors.fill: parent
                z: -1
            }
        }
        CheckBox {
            id: cb
            text: "modified"
            checkState: edit.textDocument.modified ? Qt.Checked : Qt.Unchecked
            onClicked: if (Qt.Unchecked == checkState) edit.textDocument.modified = false;
        }
        Text {
            text: "source: " + edit.textDocument.source + "\nstatus: " + edit.textDocument.status
        }
        TextEdit {
            id: edit
            Layout.fillWidth: true
            Layout.fillHeight: true
            Rectangle {
                border.width: 1
                border.color: "black"
                anchors.fill: parent
                z: -1
            }
        }
    }
}
</code></pre>
]]></description><link>https://forum.qt.io/post/838831</link><guid isPermaLink="true">https://forum.qt.io/post/838831</guid><dc:creator><![CDATA[jeremy_k]]></dc:creator><pubDate>Fri, 19 Jun 2026 19:03:41 GMT</pubDate></item><item><title><![CDATA[Reply to How to clear TextEdit&#x27;s textDocument.source? on Thu, 18 Jun 2026 19:05:17 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">Did you set <code>modified</code> to false before change <code>source</code>'s value as per the <a href="https://doc.qt.io/qt-6/qml-qtquick-textdocument.html#source-prop" target="_blank" rel="noopener noreferrer nofollow ugc">property documentation</a> ? (Just an idea, I haven't used that type)</p>
]]></description><link>https://forum.qt.io/post/838817</link><guid isPermaLink="true">https://forum.qt.io/post/838817</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 18 Jun 2026 19:05:17 GMT</pubDate></item><item><title><![CDATA[Reply to How to clear TextEdit&#x27;s textDocument.source? on Thu, 18 Jun 2026 08:56:43 GMT]]></title><description><![CDATA[<p dir="auto">I am on Qt 6.11.1. I doubt it would let me use anything on QQuickTextDocument if I was &lt; Qt 6.7.<br />
When I tried the <code>new URL()</code> approach, I tried with parameters <code>""</code> and <code>undefined</code>. Neither were valid urls (as blurted by console)</p>
<p dir="auto">When you try to change the source when it is flagged as modified, a message is outputted console to notify why the source didnt change because of this. This isn't happening here as there is no message.</p>
<p dir="auto">I tried to adapt that little script to print functions; I couldn't get it to work.</p>
<p dir="auto">Despite the <code>QML ... (parent or ancestor of TextDocument):  does not exist</code> error, <code>textDocument.source = Qt.resolvedUrl("")</code> IS working how I need it to (even if it doing it in a broken way).<br />
I hope proper support can be added</p>
]]></description><link>https://forum.qt.io/post/838812</link><guid isPermaLink="true">https://forum.qt.io/post/838812</guid><dc:creator><![CDATA[jayrickaby]]></dc:creator><pubDate>Thu, 18 Jun 2026 08:56:43 GMT</pubDate></item><item><title><![CDATA[Reply to How to clear TextEdit&#x27;s textDocument.source? on Thu, 18 Jun 2026 07:56:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jayrickaby">@<bdi>jayrickaby</bdi></a><br />
<a href="https://doc.qt.io/qt-6/qml-qtquick-textdocument.html#source-prop" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/qml-qtquick-textdocument.html#source-prop</a><br />
Not my area, just trying to help.  Says it's "preliminary" and requires Qt 6.7+ --- are you at least using that?  Then:</p>
<blockquote>
<p dir="auto">The source property cannot be changed while the document's modified state is true. If the user has modified the document contents, you should prompt the user whether to save(), or else discard changes by setting modified = false before setting the source property to a different URL.</p>
</blockquote>
<p dir="auto">Is it worth checking/clearing the <code>modified</code> property?  Otherwise you might check what is actually in <code>source</code> and what methods it has --- Googling <code>javascript show all functions of an object</code>  gives something like <a href="https://stackoverflow.com/questions/2257993/how-to-display-all-methods-of-an-object" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/2257993/how-to-display-all-methods-of-an-object</a></p>
<pre><code>var methods = [];
for (var m in obj) {
    if (typeof obj[m] == "function") {
        methods.push(m);
    }
}
alert(methods.join(","));
</code></pre>
<p dir="auto">or just print out each <code>m</code> regardless of type.</p>
<p dir="auto">Also <a href="https://stackoverflow.com/questions/73830468/modify-a-qtextdocument-of-a-qml-textarea-element" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/73830468/modify-a-qtextdocument-of-a-qml-textarea-element</a> says</p>
<blockquote>
<p dir="auto">I read in the documentation that the QQuickTextDocument::textDocument() is read-only and that I can not modify its internal state</p>
</blockquote>
<p dir="auto">so that might be a problem?</p>
<p dir="auto">Finally you might retry <code>TextEdit.textDocument.source = new URL()</code> with some parameter acceptable to to <code>new URL()</code>?</p>
]]></description><link>https://forum.qt.io/post/838810</link><guid isPermaLink="true">https://forum.qt.io/post/838810</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 18 Jun 2026 07:56:13 GMT</pubDate></item><item><title><![CDATA[Reply to How to clear TextEdit&#x27;s textDocument.source? on Thu, 18 Jun 2026 06:44:43 GMT]]></title><description><![CDATA[<p dir="auto">That's what I thought too, yet...<br />
<img src="https://ddgobkiprc33d.cloudfront.net/b269255e-2e98-419e-9cde-d1f0c006ac37.png" alt="image.png" class=" img-fluid img-markdown" /><br />
<code>.../NotepadDocument.qml:38: TypeError: Property 'clear' of object  is not a function</code></p>
]]></description><link>https://forum.qt.io/post/838809</link><guid isPermaLink="true">https://forum.qt.io/post/838809</guid><dc:creator><![CDATA[jayrickaby]]></dc:creator><pubDate>Thu, 18 Jun 2026 06:44:43 GMT</pubDate></item><item><title><![CDATA[Reply to How to clear TextEdit&#x27;s textDocument.source? on Thu, 18 Jun 2026 05:32:39 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jayrickaby">@<bdi>jayrickaby</bdi></a> said in <a href="/post/838801">How to clear TextEdit's textDocument.source?</a>:</p>
<blockquote>
<p dir="auto">TextEdit.textDocument.source.clear()</p>
</blockquote>
<p dir="auto">This should actually work.<br />
source is a QUrl, which has <a href="https://doc.qt.io/qt-6/qurl.html#clear" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/qurl.html#clear</a> method.</p>
]]></description><link>https://forum.qt.io/post/838806</link><guid isPermaLink="true">https://forum.qt.io/post/838806</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Thu, 18 Jun 2026 05:32:39 GMT</pubDate></item></channel></rss>