- Qt 6.9
- Qt Quick
- QML Types
- TextDocument
- Qt 6.9.3 Reference Documentation

# TextDocument QML Type

A wrapper around TextEdit's backing QTextDocument. More...

Import Statement: | `import QtQuick`
Status: | Preliminary

This type is under development and is subject to change.

- List of all members, including inherited members

## Properties

- errorString : string `(preliminary)`
- modified : bool `(preliminary)`
- source : url `(preliminary)`
- status : enumeration `(preliminary)`

## Methods

- void save() `(preliminary)`
- void saveAs(url url) `(preliminary)`

## Detailed Description

To load text into the document, set the source property. If the user then modifies the text and wants to save the same document, call save() to save it to the same source again (only if it's a local file). Or call saveAs() to save it to a different file.

This class cannot be instantiated in QML, but is available from TextEdit::textDocument.

Note: All loading and saving is done synchronously for now. This may block the UI if the source is a slow network drive. This may be improved in future versions of Qt.

Note: This API is considered tech preview and may change in future versions of Qt.

## Property Documentation

errorString : string `[read-only, preliminary]`

This property is under development and is subject to change.

This property holds a human-readable string describing the error that occurred during loading or saving, if any; otherwise, an empty string.

This property was introduced in Qt 6.7.

See also status, source, save(), and saveAs().

modified : bool `[preliminary]`

This property is under development and is subject to change.

This property holds whether the document has been modified by the user since the last time it was loaded or saved. By default, this property is `false`.

As with QTextDocument::modified, you can set the modified property: for example, set it to `false` to allow setting the source property to a different URL (thus discarding the user's changes).

This property was introduced in Qt 6.7.

See also QTextDocument::modified.

source : url `[preliminary]`

This property is under development and is subject to change.

QQuickTextDocument can handle any text format supported by Qt, loaded from any URL scheme supported by Qt.

The URL may be absolute, or relative to the URL of the component.

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.

This property was introduced in Qt 6.7.

See also QTextDocumentWriter::supportedDocumentFormats().

status : enumeration `[read-only, preliminary]`

This property is under development and is subject to change.

This property holds the status of document loading or saving. It can be one of:

Constant | Description
--- | ---
`TextDocument.Null` | No file has been loaded
`TextDocument.Loading` | Reading from source has begun
`TextDocument.Loaded` | Reading has successfully finished
`TextDocument.Saving` | File writing has begun after save() or saveAs()
`TextDocument.Saved` | Writing has successfully finished
`TextDocument.ReadError` | An error occurred while reading from source
`TextDocument.WriteError` | An error occurred in save() or saveAs()
`TextDocument.NonLocalFileError` | saveAs() was called with a URL pointing to a remote resource rather than a local file

Use this status to provide an update or respond to the status change in some way. For example, you could:

- Trigger a state change:

       State {
           name: 'loaded'
           when: textEdit.textDocument.status == textEdit.textDocument.Loaded
       }

- Implement an `onStatusChanged` signal handler:

       TextEdit {
           onStatusChanged: {
               if (textDocument.status === textDocument.Loaded)
                   console.log('Loaded')
           }
       }

- Bind to the status value:

       TextEdit {
           id: edit
           width: 300
           height: 200
           textFormat: TextEdit.MarkdownText
           textDocument.source: "example.md"
           wrapMode: TextEdit.WordWrap

           Text {
               anchors {
                   bottom: parent.bottom
                   right: parent.right
               }
               color: edit.textDocument.status === TextDocument.Loaded ? "darkolivegreen" : "tomato"
               text:
                   switch (edit.textDocument.status) {
                   case TextDocument.Loading:
                       return qsTr("Loading ") + edit.textDocument.source
                   case TextDocument.Loaded:
                       return qsTr("Loaded ") + edit.textDocument.source
                   default:
                       return edit.textDocument.errorString
                   }
           }
       }

This property was introduced in Qt 6.7.

See also errorString, source, save(), and saveAs().

## Method Documentation

`[preliminary]` void save()

This method is under development and is subject to change.

Saves the contents to the same file and format specified by source.

Note: You can save only to a file on a mounted filesystem.

This method was introduced in Qt 6.7.

See also source and saveAs().

`[preliminary]` void saveAs(url url)

This method is under development and is subject to change.

Saves the contents to the file and format specified by url.

The file extension in url specifies the file format (as determined by QMimeDatabase::mimeTypeForUrl()).

Note: You can save only to a file on a mounted filesystem.

This method was introduced in Qt 6.7.

See also source and save().