- Qt 6.7
- Qt Quick
- QML Types
- TextDocument
- Qt 6.7.0 Reference Documentation

# TextDocument QML Type

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

Import Statement: | import QtQuick
Instantiates: | QQuickTextDocument

- List of all members, including inherited members

## Properties

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

## Signals

- error(string message)

## Methods

- void save() `(since 6.7)`
- void saveAs(url url) `(since 6.7)`

## 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, since 6.7]`

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.

modified : bool `[since 6.7]`

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 `[since 6.7]`

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, since 6.7]`

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.

## Signal Documentation

error(string message)

This signal is emitted when an error message (translated string) should be presented to the user, for example with a MessageDialog.

Note: The corresponding handler is `onError`.

## Method Documentation

`[since 6.7]` void save()

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().

`[since 6.7]` void saveAs(url url)

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 saveAs().