Details
-
Suggestion
-
Resolution: Unresolved
-
Not Evaluated
-
None
-
None
-
None
Description
I want to identify style runs within a QTextDocument, i.e. consecutive sections of identical character formats. The only method for reading character formats is (afaik) QTextCursor::charFormat(), which gives me the format for only one character. Assembling consecutive ranges is tedious - and inefficient.
Fortunately, all necessary information is present in QTextDocumentPrivate and can be retrieved easily, like so:
struct StyleRun { int pos,len; QTextCharFormat format; }; StyleRun styleRun(QTextDocument* doc, size_t pos) { QTextDocumentPrivate* docp = QTextDocumentPrivate::get(doc); auto fragment_iterator = docp->find(pos); const QTextFragmentData* fragment = fragment_iterator.value(); return {fragment_iterator.position(), fragment_iterator.size(), docp->formatCollection()->charFormat(fragment->format) }; }
I suggest that you add this method (resp. a similar one) to the public API of QTextDocument.