Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTextEdit document()->size is invalid
Forum Updated to NodeBB v4.3 + New Features

QTextEdit document()->size is invalid

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 331 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    whatabout
    wrote on last edited by
    #1

    As I edit a document, I check what the QTextEdit's text size is, because I want to adjust the containing widget's size to match the text height.

    However I am finding that edit->document()->size() always has a bogus height e.g. 2 pixels.

    What can be done to force QTextEdit to give an accurate text size?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Can you provide a minimal compilable example that shows this behavior ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      W 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        Can you provide a minimal compilable example that shows this behavior ?

        W Offline
        W Offline
        whatabout
        wrote on last edited by
        #3

        @SGaist

        In my QPlainTextEdit, whenever a key is typed, I do this:

        document()->setTextWidth(size().width());
        QSizeF s = document()->size();
        int h = s->height();

        The h value is often tiny e.g. 2.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          CPPUIX
          wrote on last edited by
          #4

          You probably want to ask, "How do I get a line's height in QTextEdit?", because a document's height is usually measured by the number of lines, and not the height of its bounding rectangle, for example, or the sum of their heights.

          You can test that to confirm, or just check the source code:

          QSizeF QPlainTextDocumentLayout::documentSize() const
          {
              Q_D(const QPlainTextDocumentLayout);
              return QSizeF(d->maximumWidth, document()->lineCount());
          }
          

          So, you have to do that manually. Use QTextBlock's bounding rect or something similar.

          W 1 Reply Last reply
          0
          • C CPPUIX

            You probably want to ask, "How do I get a line's height in QTextEdit?", because a document's height is usually measured by the number of lines, and not the height of its bounding rectangle, for example, or the sum of their heights.

            You can test that to confirm, or just check the source code:

            QSizeF QPlainTextDocumentLayout::documentSize() const
            {
                Q_D(const QPlainTextDocumentLayout);
                return QSizeF(d->maximumWidth, document()->lineCount());
            }
            

            So, you have to do that manually. Use QTextBlock's bounding rect or something similar.

            W Offline
            W Offline
            whatabout
            wrote on last edited by
            #5

            @Abderrahmene_Rayene
            There's got to be a way to do this without resorting to a text block.
            I just want the containing view to automatically resize to match the needed height of the text edit.
            The containing view is a table widget cell.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved