In questa pagina

QTextFragment Class

La classe QTextFragment contiene un frammento di testo in un oggetto ` QTextDocument ` con un unico ` QTextCharFormat`. Per saperne di più...

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

Nota: tutte le funzioni di questa classe sono rientranti.

Funzioni pubbliche

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

Descrizione dettagliata

Un frammento di testo descrive una porzione di testo memorizzata con un unico formato di caratteri. Il testo in cui il formato dei caratteri cambia può essere rappresentato da sequenze di frammenti di testo con formati diversi.

Se l'utente modifica il testo in un frammento e introduce un formato di carattere diverso, il testo del frammento verrà suddiviso in ogni punto in cui il formato cambia e verranno creati nuovi frammenti. Ad esempio, la modifica dello stile di una parte di testo nel mezzo di una frase causerà la suddivisione del frammento in tre frammenti distinti: il primo e il terzo con lo stesso formato di prima, e il secondo con il nuovo stile. Il primo frammento conterrà il testo dall’inizio della frase, il secondo conterrà il testo dalla metà, mentre il terzo conterrà il testo dalla fine della frase.

Screenshot che mostra come un testo venga suddiviso in tre parti se la parte centrale viene messa in grassetto.

Il testo e il formato dei caratteri di un frammento possono essere ottenuti con le funzioni ` text()` e ` charFormat()`. La funzione ` length()` restituisce la lunghezza del testo nel frammento. ` position()` restituisce la posizione all’interno del documento in cui inizia il frammento. Per determinare se il frammento contiene una posizione specifica all’interno del documento, utilizzare la funzione ` contains()`.

Vedi anche QTextDocument e Struttura dei documenti in formato RTF.

Documentazione sulle funzioni membro

QTextFragment::QTextFragment()

Crea un nuovo frammento di testo vuoto.

QTextFragment::QTextFragment(const QTextFragment &other)

Copia il contenuto (testo e formattazione) del frammento di testo “ other ” in questo frammento di testo.

QTextCharFormat QTextFragment::charFormat() const

Restituisce il formato dei caratteri del frammento di testo.

Vedi anche text().

int QTextFragment::charFormatIndex() const

Restituisce un indice nell'elenco interno dei formati di carattere del documento corrispondente al formato di carattere del frammento di testo.

Vedi anche QTextDocument::allFormats().

bool QTextFragment::contains(int position) const

Restituisce ` true ` se il frammento di testo contiene il testo presente all'indirizzo ` position ` specificato nel documento; in caso contrario, restituisce ` false`.

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

Restituisce i glifi corrispondenti ai caratteri len di questo frammento di testo a partire dalla posizione pos. Le posizioni dei glifi sono relative alla posizione del layout di QTextBlock.

Se pos è inferiore a zero, il valore predefinito sarà l’inizio di QTextFragment. Se len è inferiore a zero, il valore predefinito sarà la lunghezza del frammento.

Vedi anche QGlyphRun, QTextBlock::layout(), QTextLayout::position() e QPainter::drawGlyphRun().

bool QTextFragment::isValid() const

Restituisce ` true ` se si tratta di un frammento di testo valido (ovvero se occupa una posizione valida all’interno di un documento); in caso contrario, restituisce ` false`.

int QTextFragment::length() const

Restituisce il numero di caratteri presenti nel frammento di testo.

Vedi anche text().

int QTextFragment::position() const

Restituisce la posizione di questo frammento di testo nel documento.

QString QTextFragment::text() const

Restituisce il frammento di testo come testo semplice.

Vedi anche length() e charFormat().

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

Restituisce " true " se questo frammento di testo è diverso (si trova in una posizione diversa) dal frammento di testo " other "; altrimenti restituisce false.

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

Restituisce ` true ` se questo frammento di testo compare nel documento prima del frammento di testo ` other `; altrimenti restituisce ` false`.

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

Assegna il contenuto (testo e formattazione) del frammento di testo “ other ” a questo frammento di testo.

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

Restituisce ` true ` se questo frammento di testo è uguale (nella stessa posizione) al frammento di testo ` other `; altrimenti restituisce ` 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.