- Qt 6.12
- Qt Quick
- QML Types
- TextDocument
- Qt 6.12.0 Reference Documentation

# TextDocument QML Type (Uncreatable)

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

Import Statement: | `import QtQuick`
In C++: | QQuickTextDocument
Status: | Technology preview

This type is in technology preview and is subject to change.

Note: This is an uncreatable type. It cannot be instantiated in QML.

- List of all members, including inherited members

## Properties

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

## Methods

- void save() `(technology preview)`
- void saveAs(url url) `(technology preview)`

## 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, technology preview]`

This property is in technology preview 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 `[technology preview]`

This property is in technology preview 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 `[technology preview]`

This property is in technology preview 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, technology preview]`

This property is in technology preview 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

### `[technology preview]` void save()

This method is in technology preview 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().

### `[technology preview]` void saveAs(url url)

This method is in technology preview 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().