En esta página

QTextFragment Class

La clase QTextFragment contiene un fragmento de texto en un QTextDocument con un solo QTextCharFormat. Más...

Cabecera: #include <QTextFragment>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui

Nota: Todas las funciones de esta clase son reentrantes.

Funciones públicas

QTextFragment()
QTextFragment(const QTextFragment &other)
QTextCharFormat charFormat() const
int charFormatIndex() const
bool contains(int position) const
QList<QGlyphRun> glyphRuns(int pos = -1, int len = -1) const
bool isValid() const
int length() const
int position() const
QString text() const
bool operator!=(const QTextFragment &other) const
bool operator<(const QTextFragment &other) const
QTextFragment &operator=(const QTextFragment &other)
bool operator==(const QTextFragment &other) const

Descripción detallada

Un fragmento de texto describe un fragmento de texto que se almacena con un único formato de caracteres. Los textos cuyo formato de caracteres cambia pueden representarse mediante secuencias de fragmentos de texto con distintos formatos.

Si el usuario edita el texto de un fragmento e introduce un formato de caracteres diferente, el texto del fragmento se dividirá en cada punto en el que cambie el formato y se crearán nuevos fragmentos. Por ejemplo, cambiar el estilo de un texto en mitad de una frase hará que el fragmento se divida en tres fragmentos separados: el primero y el tercero con el mismo formato que antes, y el segundo con el nuevo estilo. El primer fragmento contendrá el texto del principio de la frase, el segundo contendrá el texto del medio y el tercero tomará el texto del final de la frase.

Captura de pantalla que muestra cómo un texto se separa en tres fragmentos si el texto central se pone en negrita.

El texto y el formato de caracteres de un fragmento pueden obtenerse con las funciones text() y charFormat(). La función length() da la longitud del texto del fragmento. position() da la posición en el documento del inicio del fragmento. Para determinar si el fragmento contiene una posición determinada dentro del documento, utilice la función contains().

Véase también QTextDocument y Estructura del documento de texto enriquecido.

Documentación de las funciones miembro

QTextFragment::QTextFragment()

Crea un nuevo fragmento de texto vacío.

QTextFragment::QTextFragment(const QTextFragment &other)

Copia el contenido (texto y formato) del fragmento de texto other en este fragmento de texto.

QTextCharFormat QTextFragment::charFormat() const

Devuelve el formato de caracteres del fragmento de texto.

Véase también text().

int QTextFragment::charFormatIndex() const

Devuelve un índice en la lista interna de formatos de caracteres del documento para el formato de caracteres del fragmento de texto.

Véase también QTextDocument::allFormats().

bool QTextFragment::contains(int position) const

Devuelve true si el fragmento de texto contiene el texto en el position dado en el documento; en caso contrario devuelve false.

QList<QGlyphRun> QTextFragment::glyphRuns(int pos = -1, int len = -1) const

Devuelve los glifos correspondientes a len caracteres de este fragmento de texto a partir de la posición pos. Las posiciones de los glifos son relativas a la posición del QTextBlock's layout.

Si pos es menor que cero, se tomará por defecto el inicio de QTextFragment. Si len es menor que cero, se tomará por defecto la longitud del fragmento.

Véase también QGlyphRun, QTextBlock::layout(), QTextLayout::position() y QPainter::drawGlyphRun().

bool QTextFragment::isValid() const

Devuelve true si se trata de un fragmento de texto válido (es decir, tiene una posición válida en un documento); en caso contrario, devuelve false.

int QTextFragment::length() const

Devuelve el número de caracteres del fragmento de texto.

Véase también text().

int QTextFragment::position() const

Devuelve la posición de este fragmento de texto en el documento.

QString QTextFragment::text() const

Devuelve el fragmento de texto como texto sin formato.

Véase también length() y charFormat().

bool QTextFragment::operator!=(const QTextFragment &other) const

Devuelve true si este fragmento de texto es diferente (en una posición diferente) del fragmento de texto other; en caso contrario, devuelve false.

bool QTextFragment::operator<(const QTextFragment &other) const

Devuelve true si este fragmento de texto aparece antes en el documento que el fragmento de texto other; en caso contrario devuelve false.

QTextFragment &QTextFragment::operator=(const QTextFragment &other)

Asigna el contenido (texto y formato) del fragmento de texto other a este fragmento de texto.

bool QTextFragment::operator==(const QTextFragment &other) const

Devuelve true si este fragmento de texto es el mismo (en la misma posición) que el fragmento de texto other; en caso contrario devuelve false.

© 2026 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.