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. Set exact height for QPlainTextEdit (according to height)
Forum Updated to NodeBB v4.3 + New Features

Set exact height for QPlainTextEdit (according to height)

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 6.4k Views 1 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.
  • Q Offline
    Q Offline
    QuentinV
    wrote on last edited by
    #1

    I am trying to set the fixed height of a QPlainTextEdit to encompass exactly its text content, so that no vertical scrollbar is necessary and no space is wasted after the text.

    I have until now failed to find a way to do this properly in code. I have tried several ways to do this, including :

    QFontMetrics m(_ui->pendingCommand->fontMetrics());
    int lineSpacing = m.lineSpacing();
    _ui->pendingCommand->setFixedHeight(lineSpacing*_ui->pendingCommand->blockCount()*lineSpacing);
    

    and

    QFontMetrics m(_ui->pendingCommand->fontMetrics());
    _ui->pendingCommand->setFixedHeight(m.boundingRect(_ui->pendingCommand->toPlainText()).height());
    

    There are no contentMargins (all set to 0), and there is no horizontal scrollbar

    I always have to add some "magic number" (like +8 pixels) to get this working as expected. This is nasty, and not robust to font changes.

    How can I make the height of the widget match exactly the size of the text ?

    raven-worxR 1 Reply Last reply
    0
    • Q QuentinV

      I am trying to set the fixed height of a QPlainTextEdit to encompass exactly its text content, so that no vertical scrollbar is necessary and no space is wasted after the text.

      I have until now failed to find a way to do this properly in code. I have tried several ways to do this, including :

      QFontMetrics m(_ui->pendingCommand->fontMetrics());
      int lineSpacing = m.lineSpacing();
      _ui->pendingCommand->setFixedHeight(lineSpacing*_ui->pendingCommand->blockCount()*lineSpacing);
      

      and

      QFontMetrics m(_ui->pendingCommand->fontMetrics());
      _ui->pendingCommand->setFixedHeight(m.boundingRect(_ui->pendingCommand->toPlainText()).height());
      

      There are no contentMargins (all set to 0), and there is no horizontal scrollbar

      I always have to add some "magic number" (like +8 pixels) to get this working as expected. This is nasty, and not robust to font changes.

      How can I make the height of the widget match exactly the size of the text ?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @QuentinV said in Set exact height for QPlainTextEdit (according to height):

      I always have to add some "magic number" (like +8 pixels) to get this working as expected. This is nasty, and not robust to font changes.
      How can I make the height of the widget match exactly the size of the text ?

      The extra space comes from the QTextDocument's margin which by default is set to 4 pixels (=8px for top + bottom)

      QAbstractTextDocumentLayout* docLayout = textEdit->document()->documentLayout();
      const int docMargin = qCeil(doc->documentMargin());
      

      Although something like this should work:

      QMargins margins = textEdit->contentsMargins();
      QSizeF documentSize =  textEdit->document()->size();
      int height = documentSize.height() + margins.top() + contentsMargins.bottom() + 1;
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      Q 1 Reply Last reply
      3
      • raven-worxR raven-worx

        @QuentinV said in Set exact height for QPlainTextEdit (according to height):

        I always have to add some "magic number" (like +8 pixels) to get this working as expected. This is nasty, and not robust to font changes.
        How can I make the height of the widget match exactly the size of the text ?

        The extra space comes from the QTextDocument's margin which by default is set to 4 pixels (=8px for top + bottom)

        QAbstractTextDocumentLayout* docLayout = textEdit->document()->documentLayout();
        const int docMargin = qCeil(doc->documentMargin());
        

        Although something like this should work:

        QMargins margins = textEdit->contentsMargins();
        QSizeF documentSize =  textEdit->document()->size();
        int height = documentSize.height() + margins.top() + contentsMargins.bottom() + 1;
        
        Q Offline
        Q Offline
        QuentinV
        wrote on last edited by
        #3

        Thank you for your answer, but none of this work.

        The contentMargins() property is equal to (0,0,0,0), and the documentSize's height is equal to the number of blocks in the text.

        raven-worxR 1 Reply Last reply
        0
        • Q QuentinV

          Thank you for your answer, but none of this work.

          The contentMargins() property is equal to (0,0,0,0), and the documentSize's height is equal to the number of blocks in the text.

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @QuentinV said in Set exact height for QPlainTextEdit (according to height):

          Thank you for your answer, but none of this work.

          i pointed out your "magic number"?
          Simply add up all these properties (even if the contentMargins are 0) and you have the value you want in a pretty generic way. What you wanted in the first place?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          Q 1 Reply Last reply
          0
          • raven-worxR raven-worx

            @QuentinV said in Set exact height for QPlainTextEdit (according to height):

            Thank you for your answer, but none of this work.

            i pointed out your "magic number"?
            Simply add up all these properties (even if the contentMargins are 0) and you have the value you want in a pretty generic way. What you wanted in the first place?

            Q Offline
            Q Offline
            QuentinV
            wrote on last edited by QuentinV
            #5

            @raven-worx

            Well, it doesn't gives a height that fits exactly the text (which is my actual problem), and doesn't even add up to my magic number.

            So yes, it generic, but it doesn't solves my problem.

            raven-worxR 1 Reply Last reply
            0
            • Q QuentinV

              @raven-worx

              Well, it doesn't gives a height that fits exactly the text (which is my actual problem), and doesn't even add up to my magic number.

              So yes, it generic, but it doesn't solves my problem.

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by raven-worx
              #6

              @QuentinV
              ok i now checked my code again, since i knew it should definitely work.
              But it seems my code only works properly for QTextEdit but not for QPlainTextEdit. QPlainTextEdit returns a incorrect documentSize - which might be a bug IMO.

              This works correctly for QTextEdit:

              QMargins margins = textEdit->contentsMargins();
              QSizeF documentSize =  textEdit->document()->documentLayout()->documentSize();
              int height = documentSize.height() + margins.top() + margins.bottom() + 1;
              

              Is switching to QTextEdit an option for you?

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                QuentinV
                wrote on last edited by
                #7

                @raven-worx

                We have no strong performance issues, so we will switch to QTextEdit. Thank you for your help.

                I am new to Qt, how can I file a bug to report the problem of QPlainTextEdit ?

                raven-worxR 1 Reply Last reply
                0
                • Q QuentinV

                  @raven-worx

                  We have no strong performance issues, so we will switch to QTextEdit. Thank you for your help.

                  I am new to Qt, how can I file a bug to report the problem of QPlainTextEdit ?

                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  @QuentinV said in Set exact height for QPlainTextEdit (according to height):

                  I am new to Qt, how can I file a bug to report the problem of QPlainTextEdit ?

                  1. https://bugreports.qt.io
                  2. login with your forum account
                  3. press "create" button in the top toolbar

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  Q 1 Reply Last reply
                  1
                  • raven-worxR raven-worx

                    @QuentinV said in Set exact height for QPlainTextEdit (according to height):

                    I am new to Qt, how can I file a bug to report the problem of QPlainTextEdit ?

                    1. https://bugreports.qt.io
                    2. login with your forum account
                    3. press "create" button in the top toolbar
                    Q Offline
                    Q Offline
                    QuentinV
                    wrote on last edited by
                    #9

                    @raven-worx
                    Well, it is apparently not a bug. It has already been reported, and the QPlainTextDocumentLayout returns by design the number of blocks for its height.

                    1 Reply Last reply
                    2

                    • Login

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