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

QTextEdit::extraSelections do not work well on Linux/X11 when QTextEdit has explicitly set small font size

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 5.15.2, 6.2.4, 6.4.0
    • None
    • Ubuntu 22.04, Ubuntu 20.04
    • Linux/X11

    Description

      Here is the minimal reproducible example:

      #include <QApplication>
      #include <QTextEdit>
      
      void highlight(QTextEdit *textEdit)
      {
          QList<QTextEdit::ExtraSelection> highlights;
          QString word = "abcd";
          QString text = textEdit->toPlainText();
          int pos = text.indexOf(word);
      
          while (pos != -1)
          {
              int endPos = pos + word.length();
      
              auto highlight = QTextEdit::ExtraSelection();
              highlight.cursor = QTextCursor(textEdit->document());
              highlight.cursor.setPosition(pos);
              highlight.cursor.setPosition(endPos, QTextCursor::KeepAnchor);
              highlight.format.setForeground(QApplication::palette().color(QPalette::Link));
              highlights.append(highlight);
      
              pos = text.indexOf(word, endPos);
          }
      
          textEdit->setExtraSelections(highlights);
      }
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          QTextEdit w;
      
          // THE NEXT LINE!!!
          // If point size 12 or smaller is used, then every second
          // row in the text edit is not highlighted.
          // If commented out, then everything works fine.
          // If point size 13 or greater is used, then everything works fine.
          QFont f;
          f.setPointSize(12);
          w.setFont(f);
      
          w.setPlainText("abcd abcd abcd abcd abcd abcd abcd abcd "
                         "abcd abcd abcd abcd abcd abcd abcd abcd");
          highlight(&w);
          w.show();
          return a.exec();
      }
      

      The program highlights every occurrence of word "abcd" in the text edit. If the text edit has an explicitly set font size and the font is small (12 points or smaller on my machine - could be different on other machine), then the highlighting does not work in every second row, no text is highlighted in these rows.

      If the font is not explicitly set or the font size is big (13 points or greater), then the highlighting words well and all rows are highlighted.

      Similar problem seems to be with QPlainTextEdit. Note that Windows, macOS and Wayland seem to work well, as expected.

      Attachments

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

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            vladimir.kraus Vladimir Kraus
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes