Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-122737

QTextDocument: find style runs

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Unresolved
    • Not Evaluated
    • None
    • None
    • GUI: Text handling
    • 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.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            esabraha Eskil Abrahamsen Blomfeldt
            nightrider Peter Schäfer
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes