TextDocument QML Type

TextEdit 的支持进行包装QTextDocument更多

Import Statement: import QtQuick
In C++: QQuickTextDocument
Status: Preliminary

此类型正在开发中,可能会有更改。

属性

方法

  • void save() (preliminary)
  • void saveAs(url url) (preliminary)

详细说明

要将文本加载到文档中,请设置source 属性。如果用户随后修改了文本,并希望保存同一文档,可调用save() 将其再次保存到同一源文件(仅当it's a local file 时)。或者调用saveAs() 保存到不同的文件。

该类不能在 QML 中实例化,但可从TextEdit::textDocument 获取。

注意: 目前所有加载和保存都是同步进行的。如果source 是一个慢速网络驱动器,这可能会阻塞用户界面。这可能会在未来的 Qt 版本中得到改进。

注意: 此 API 被视为技术预览版,在未来的 Qt 版本中可能会有所改变。

属性文档

errorString : string [read-only, preliminary]

此属性正在开发中,可能会有所更改。

该属性包含一个人可读取的字符串,用于描述加载或保存过程中发生的错误(如果有);否则为空字符串。

此属性在 Qt 6.7 中引入。

另请参阅 status,source,save() 和saveAs() 。


modified : bool [preliminary]

此属性正在开发中,可能会更改。

自上次加载或保存文档以来,用户是否修改了该文档。默认情况下,该属性为false

QTextDocument::modified 一样,您可以设置已修改属性:例如,将其设置为false ,以允许将source 属性设置为不同的 URL(从而丢弃用户的修改)。

该属性在 Qt 6.7 中引入。

另请参阅 QTextDocument::modified


source : url [preliminary]

此属性正在开发中,可能会有更改。

QQuickTextDocument 可以处理 Qt 支持的任何文本格式,并从 Qt 支持的任何 URL 方案加载。

URL 可以是绝对的,也可以是相对于组件 URL 的。

当文档的modified 状态为true 时,不能更改source 属性。如果用户修改了文档内容,应提示用户是否save() ,否则应在将源属性设置为不同的 URL 之前,通过设置modified = false 来放弃更改。

此属性在 Qt 6.7 中引入。

另请参阅 QTextDocumentWriter::supportedDocumentFormats()。


status : enumeration [read-only, preliminary]

此属性正在开发中,可能会发生变化。

此属性保存文档加载或保存的状态。它可以是

常量说明
TextDocument.Null未加载文件
TextDocument.Loading已开始从source 读取
TextDocument.Loaded读取已成功完成
TextDocument.Savingsave() 或saveAs() 之后开始写入文件
TextDocument.Saved写入已成功完成
TextDocument.ReadError从以下文件读取时发生错误source
TextDocument.WriteErrorsave() 或saveAs() 中发生错误
TextDocument.NonLocalFileErrorsaveAs调用 () 时,URL 指向的是远程资源而不是本地文件

使用此状态提供更新或以某种方式响应状态变化。例如,您可以

  • 触发状态更改:
    State {
        name: 'loaded'
        when: textEdit.textDocument.status == textEdit.textDocument.Loaded
    }
  • 执行onStatusChanged 信号处理器:
    TextEdit {
        onStatusChanged: {
            if (textDocument.status === textDocument.Loaded)
                console.log('Loaded')
        }
    }
  • 绑定状态值:
    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
                }
        }
    }

此属性在 Qt 6.7 中引入。

另请参阅 errorString,source,save() 和saveAs() 。


方法文档

[preliminary] void save()

此方法正在开发中,可能会有更改。

将内容保存到source 指定的相同文件和格式。

注意: 只能保存到file on a mounted filesystem

此方法在 Qt 6.7 中引入。

另请参阅 sourcesaveAs()。


[preliminary] void saveAs(url url)

此方法正在开发中,可能会有更改。

将内容保存到url 指定的文件和格式。

url 中的文件扩展名指定了文件格式(由QMimeDatabase::mimeTypeForUrl() 决定)。

注意: 只能保存到file on a mounted filesystem

此方法在 Qt 6.7 中引入。

另请参阅 sourcesave()。


© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.