aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/items/qquicktextdocument.cpp
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@qt.io>2023-11-01 21:06:51 -0700
committerShawn Rutledge <shawn.rutledge@qt.io>2023-12-06 13:10:36 -0700
commit67fbc9ebc5d098cc380c76dcdb2246965a714f45 (patch)
treeb09a24baa744810a4fb9c27f329e997b48ff0006 /src/quick/items/qquicktextdocument.cpp
parent64ccebc841bc285062ce52e45d43ebad2e048ed6 (diff)
Add setter and notifier for QQuickTextDocument's document
Users want to be able to provide their own "special" instances of QTextDocument, or custom subclasses. But so far we allow this only from C++, not from QML. QQuickTextDocument is created lazily in QQuickTextEdit::textDocument() so it's not possible to rely on it to hold the actual QTextDocument all the time. It's only a facade provided to the user. But QQuickTextEdit holds the QTextDocument, and is the only class that creates QQuickTextDocument, and sets the parent to itself; so QQuickTextDocument can rely on accessing the TextEdit's document rather than maintaining its own pointer. textEdit.dot is basically a collaboration diagram showing which objects creates which, the parents, and which objects get swapped out if the user calls setTextDocument(). Task-number: QTBUG-35688 Change-Id: Iac5000aa8027a75a000d4501190f3ee5f04e426e Reviewed-by: Axel Spoerl <axel.spoerl@qt.io> Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/quick/items/qquicktextdocument.cpp')
-rw-r--r--src/quick/items/qquicktextdocument.cpp47
1 files changed, 35 insertions, 12 deletions
diff --git a/src/quick/items/qquicktextdocument.cpp b/src/quick/items/qquicktextdocument.cpp
index 37a054f33a..81298596b0 100644
--- a/src/quick/items/qquicktextdocument.cpp
+++ b/src/quick/items/qquicktextdocument.cpp
@@ -23,15 +23,12 @@ QT_BEGIN_NAMESPACE
\inmodule QtQuick
This class provides access to the QTextDocument of QQuickTextEdit elements.
- This is provided to allow usage of the \l{Rich Text Processing} functionalities of Qt.
- You are not allowed to modify the document, but it can be used to output content, for example with \l{QTextDocumentWriter}),
- or provide additional formatting, for example with \l{QSyntaxHighlighter}.
+ This is provided to allow usage of the \l{Rich Text Processing} functionalities of Qt,
+ including document modifications. It can also be used to output content,
+ for example with \l{QTextDocumentWriter}), or provide additional formatting,
+ for example with \l{QSyntaxHighlighter}.
- The class has to be used from C++ directly, using the property of the \l TextEdit.
-
- Warning: The QTextDocument provided is used internally by \l {Qt Quick} elements to provide text manipulation primitives.
- You are not allowed to perform any modification of the internal state of the QTextDocument. If you do, the element
- in question may stop functioning or crash.
+ This class cannot be instantiated in QML, but is available from \l TextEdit::textDocument.
*/
/*!
@@ -43,17 +40,43 @@ QQuickTextDocument::QQuickTextDocument(QQuickItem *parent)
{
Q_D(QQuickTextDocument);
Q_ASSERT(parent);
- Q_ASSERT(qobject_cast<QQuickTextEdit*>(parent));
- d->document = QPointer<QTextDocument>(qobject_cast<QQuickTextEdit*>(parent)->d_func()->document);
+ d->editor = qobject_cast<QQuickTextEdit *>(parent);
+ Q_ASSERT(d->editor);
+}
+
+QTextDocument *QQuickTextDocumentPrivate::document() const
+{
+ return editor->document();
+}
+
+void QQuickTextDocumentPrivate::setDocument(QTextDocument *doc)
+{
+ Q_Q(QQuickTextDocument);
+ if (doc == editor->document())
+ return;
+
+ editor->setDocument(doc);
+ emit q->textDocumentChanged();
}
/*!
Returns a pointer to the QTextDocument object.
*/
-QTextDocument* QQuickTextDocument::textDocument() const
+QTextDocument *QQuickTextDocument::textDocument() const
{
Q_D(const QQuickTextDocument);
- return d->document.data();
+ return d->document();
+}
+
+/*!
+ \brief Sets the given \a document.
+ \since 6.7
+
+ The caller retains ownership of the document.
+*/
+void QQuickTextDocument::setTextDocument(QTextDocument *document)
+{
+ d_func()->setDocument(document);
}
QQuickTextImageHandler::QQuickTextImageHandler(QObject *parent)