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

Cursor size doesn't match QTextCharFormat font size if document is empty

    XMLWordPrintable

Details

    Description

      If you change the font size of an empty QTextDocument then the height of the cursor (in a QML TextEdit for example) won't match this font size. This is a bad experience for users of rich text editors, as they'd expect the cursor size to match their selected font size, even if they haven't typed anything yet.

      I've had a little look around the Qt internals (with QQuickTextControlPrivate::rectForPosition being the main area of interest) and it seems that the height of the cursor comes from the height of the line at whatever position the cursor is at. However, if there are no lines in the cursor's block's layout, then a height of 10 is returned.

      For some reason, the line count of the initial block in a QTextDocument has 0 lines, even though the document and the block itself have 1 line.

      An example of the cursor being the wrong height

      main.qml

      main.cpp

      documentcontroller.h

      documentcontroller.cpp

      If you change the font size before typing, you'll see that the cursor actually shrinks, but once you start typing it'll become the right size. Look in the console and you'll see that before typing there are no lines.

      An example showing the line counts

      main.cpp

      Other examples

      My example is a QML application but I've also observed similar behaviour in Qt Widgets applications. For example, in Calligra Words if you change increase font size the cursor won't increase in size until you start typing. And if you backspace until the start of the block then the cursor will return to the default height, even though the larger font size is still set.

      I've set the affected version as 6.2.4, since that's what I used to create the first example, but I suspect that this problem exists in newer versions of Qt as well.

      Attachments

        1. documentcontroller.cpp
          1.0 kB
        2. documentcontroller.h
          0.6 kB
        3. main.cpp
          0.7 kB
        4. main.qml
          0.8 kB
        5. main-1.cpp
          0.5 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            esabraha Eskil Abrahamsen Blomfeldt
            camilo Dave Erified
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes