Qt 4.8
Public Functions | Static Public Functions | Properties | Friends | List of all members
QTextDocumentFragment Class Reference

The QTextDocumentFragment class represents a piece of formatted text from a QTextDocument. More...

#include <qtextdocumentfragment.h>

Public Functions

bool isEmpty () const
 Returns true if the fragment is empty; otherwise returns false. More...
 
QTextDocumentFragmentoperator= (const QTextDocumentFragment &rhs)
 Assigns the other fragment to this fragment. More...
 
 QTextDocumentFragment ()
 Constructs an empty QTextDocumentFragment. More...
 
 QTextDocumentFragment (const QTextDocument *document)
 Converts the given document into a QTextDocumentFragment. More...
 
 QTextDocumentFragment (const QTextCursor &range)
 Creates a QTextDocumentFragment from the {cursor}'s selection. More...
 
 QTextDocumentFragment (const QTextDocumentFragment &rhs)
 Copy constructor. More...
 
QString toHtml () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
QString toHtml (const QByteArray &encoding) const
 Returns the contents of the document fragment as HTML, using the specified encoding (e. More...
 
QString toPlainText () const
 Returns the document fragment's text as plain text (i.e. More...
 
 ~QTextDocumentFragment ()
 Destroys the document fragment. More...
 

Static Public Functions

static QTextDocumentFragment fromHtml (const QString &html)
 Returns a QTextDocumentFragment based on the arbitrary piece of HTML in the given text. More...
 
static QTextDocumentFragment fromHtml (const QString &html, const QTextDocument *resourceProvider)
 
static QTextDocumentFragment fromPlainText (const QString &plainText)
 Returns a document fragment that contains the given plainText. More...
 

Properties

QTextDocumentFragmentPrivated
 

Friends

class QTextCursor
 
class QTextDocumentWriter
 

Detailed Description

The QTextDocumentFragment class represents a piece of formatted text from a QTextDocument.

Note
This class or function is reentrant.

A QTextDocumentFragment is a fragment of rich text, that can be inserted into a QTextDocument. A document fragment can be created from a QTextDocument, from a QTextCursor's selection, or from another document fragment. Document fragments can also be created by the static functions, fromPlainText() and fromHtml().

The contents of a document fragment can be obtained as plain text by using the toPlainText() function, or it can be obtained as HTML with toHtml().

Definition at line 58 of file qtextdocumentfragment.h.

Constructors and Destructors

◆ QTextDocumentFragment() [1/4]

QTextDocumentFragment::QTextDocumentFragment ( )

Constructs an empty QTextDocumentFragment.

See also
isEmpty()

Definition at line 284 of file qtextdocumentfragment.cpp.

285  : d(0)
286 {
287 }
QTextDocumentFragmentPrivate * d

◆ QTextDocumentFragment() [2/4]

QTextDocumentFragment::QTextDocumentFragment ( const QTextDocument document)
explicit

Converts the given document into a QTextDocumentFragment.

Note that the QTextDocumentFragment only stores the document contents, not meta information like the document's title.

Definition at line 294 of file qtextdocumentfragment.cpp.

295  : d(0)
296 {
297  if (!document)
298  return;
299 
300  QTextCursor cursor(const_cast<QTextDocument *>(document));
301  cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
302  d = new QTextDocumentFragmentPrivate(cursor);
303 }
QTextDocumentFragmentPrivate * d
The QTextCursor class offers an API to access and modify QTextDocuments.
Definition: qtextcursor.h:70

◆ QTextDocumentFragment() [3/4]

QTextDocumentFragment::QTextDocumentFragment ( const QTextCursor cursor)
explicit

Creates a QTextDocumentFragment from the {cursor}'s selection.

If the cursor doesn't have a selection, the created fragment is empty.

See also
isEmpty() QTextCursor::selection()

Definition at line 311 of file qtextdocumentfragment.cpp.

312  : d(0)
313 {
314  if (!cursor.hasSelection())
315  return;
316 
317  d = new QTextDocumentFragmentPrivate(cursor);
318 }
QTextDocumentFragmentPrivate * d
bool hasSelection() const
Returns true if the cursor contains a selection; otherwise returns false.

◆ QTextDocumentFragment() [4/4]

QTextDocumentFragment::QTextDocumentFragment ( const QTextDocumentFragment other)

Copy constructor.

Creates a copy of the other fragment.

Definition at line 328 of file qtextdocumentfragment.cpp.

329  : d(rhs.d)
330 {
331  if (d)
332  d->ref.ref();
333 }
QTextDocumentFragmentPrivate * d
bool ref()
Atomically increments the value of this QAtomicInt.

◆ ~QTextDocumentFragment()

QTextDocumentFragment::~QTextDocumentFragment ( )

Destroys the document fragment.

Definition at line 356 of file qtextdocumentfragment.cpp.

357 {
358  if (d && !d->ref.deref())
359  delete d;
360 }
QTextDocumentFragmentPrivate * d
bool deref()
Atomically decrements the value of this QAtomicInt.

Functions

◆ fromHtml() [1/2]

QTextDocumentFragment QTextDocumentFragment::fromHtml ( const QString text)
static

Returns a QTextDocumentFragment based on the arbitrary piece of HTML in the given text.

The formatting is preserved as much as possible; for example, "<b>bold</b>" will become a document fragment with the text "bold" with a bold character format.

Definition at line 1215 of file qtextdocumentfragment.cpp.

Referenced by QTextControl::insertFromMimeData(), and QTextCursor::insertHtml().

1216 {
1217  return fromHtml(html, 0);
1218 }
static QTextDocumentFragment fromHtml(const QString &html)
Returns a QTextDocumentFragment based on the arbitrary piece of HTML in the given text...

◆ fromHtml() [2/2]

QTextDocumentFragment QTextDocumentFragment::fromHtml ( const QString text,
const QTextDocument resourceProvider 
)
static
Since
4.2

Returns a QTextDocumentFragment based on the arbitrary piece of HTML in the given text. The formatting is preserved as much as possible; for example, "<b>bold</b>" will become a document fragment with the text "bold" with a bold character format.

If the provided HTML contains references to external resources such as imported style sheets, then they will be loaded through the resourceProvider.

Definition at line 1233 of file qtextdocumentfragment.cpp.

1234 {
1236  res.d = new QTextDocumentFragmentPrivate;
1237 
1238  QTextHtmlImporter importer(res.d->doc, html, QTextHtmlImporter::ImportToFragment, resourceProvider);
1239  importer.import();
1240  return res;
1241 }
QTextDocumentFragmentPrivate * d
The QTextDocumentFragment class represents a piece of formatted text from a QTextDocument.

◆ fromPlainText()

QTextDocumentFragment QTextDocumentFragment::fromPlainText ( const QString plainText)
static

Returns a document fragment that contains the given plainText.

When inserting such a fragment into a QTextDocument the current char format of the QTextCursor used for insertion is used as format for the text.

Definition at line 423 of file qtextdocumentfragment.cpp.

Referenced by QTextControl::insertFromMimeData().

424 {
426 
428  res.d->importedFromPlainText = true;
429  QTextCursor cursor(res.d->doc);
430  cursor.insertText(plainText);
431  return res;
432 }
QTextDocumentFragmentPrivate * d
The QTextCursor class offers an API to access and modify QTextDocuments.
Definition: qtextcursor.h:70
The QTextDocumentFragment class represents a piece of formatted text from a QTextDocument.

◆ isEmpty()

bool QTextDocumentFragment::isEmpty ( ) const

Returns true if the fragment is empty; otherwise returns false.

Definition at line 365 of file qtextdocumentfragment.cpp.

Referenced by QTextCursor::insertFragment().

366 {
367  return !d || !d->doc || d->doc->docHandle()->length() <= 1;
368 }
QTextDocumentFragmentPrivate * d
QTextDocumentPrivate * docHandle() const
So that not all classes have to be friends of each other.

◆ operator=()

QTextDocumentFragment & QTextDocumentFragment::operator= ( const QTextDocumentFragment rhs)

Assigns the other fragment to this fragment.

Definition at line 343 of file qtextdocumentfragment.cpp.

344 {
345  if (rhs.d)
346  rhs.d->ref.ref();
347  if (d && !d->ref.deref())
348  delete d;
349  d = rhs.d;
350  return *this;
351 }
QTextDocumentFragmentPrivate * d
bool ref()
Atomically increments the value of this QAtomicInt.
bool deref()
Atomically decrements the value of this QAtomicInt.

◆ toHtml() [1/2]

QString QTextDocumentFragment::toHtml ( ) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 391 of file qtextdocumentfragment.cpp.

392 {
393  return toHtml(QByteArray());
394 }
QString toHtml() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135

◆ toHtml() [2/2]

QString QTextDocumentFragment::toHtml ( const QByteArray encoding) const

Returns the contents of the document fragment as HTML, using the specified encoding (e.

Since
4.2

g., "UTF-8", "ISO 8859-1").

\sa toPlainText(), QTextDocument::toHtml(), QTextCodec

Definition at line 407 of file qtextdocumentfragment.cpp.

408 {
409  if (!d)
410  return QString();
411 
413 }
QTextDocumentFragmentPrivate * d
The QString class provides a Unicode character string.
Definition: qstring.h:83
QString toHtml(const QByteArray &encoding, ExportMode mode=ExportEntireDocument)
Returns the document in HTML format.

◆ toPlainText()

QString QTextDocumentFragment::toPlainText ( ) const

Returns the document fragment's text as plain text (i.e.

with no formatting information).

See also
toHtml()

Definition at line 376 of file qtextdocumentfragment.cpp.

377 {
378  if (!d)
379  return QString();
380 
381  return d->doc->toPlainText();
382 }
QTextDocumentFragmentPrivate * d
The QString class provides a Unicode character string.
Definition: qstring.h:83
QString toPlainText() const
Returns the plain text contained in the document.

Friends and Related Functions

◆ QTextCursor

friend class QTextCursor
friend

Definition at line 84 of file qtextdocumentfragment.h.

◆ QTextDocumentWriter

friend class QTextDocumentWriter
friend

Definition at line 85 of file qtextdocumentfragment.h.

Properties

◆ d

QTextDocumentFragmentPrivate* QTextDocumentFragment::d
private

The documentation for this class was generated from the following files: