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. [Solved] A QWidget like QTextEdit that wraps its height automatically to its contents?

[Solved] A QWidget like QTextEdit that wraps its height automatically to its contents?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 7.9k 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.
  • J Offline
    J Offline
    jesuisbenjamin
    wrote on last edited by
    #1

    I am creating a form with some QTextEdit widgets.

    The default height of the QTextEdit exceeds a single line of text and as the contents' height exceeds the QTextEdit's height, it creates a scroll-bar to scroll the content.

    I would like to override this behaviour to create a QTextEdit that would rather wrap its height to its contents. This means that the default height would be one line and that on wrapping or entering a new line, the QTextEdit would increase its height automatically. Whenever the contents height exceeds the QTextEdit's height, the latter should not create a scroll bar but simply increase in height.

    How can I go about doing this? Thanks.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      Some ideas, nothing tested:

      • connect to the signal contentsChanged of the QTextDocument attached to the text edit
      • grab the viewport widget of the QAbstractScrollArea (QTextEdit inherits it), check its size and adjust the edit

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jesuisbenjamin
        wrote on last edited by
        #3

        Indeed!

        I also got "a nice response on StackOverflow":http://stackoverflow.com/questions/11851020/a-qwidget-like-qtextedit-that-wraps-its-height-automatically-to-its-contents/11858803#11858803.

        Also in order to make the QTextEdit wrap its height around its document when the window / widget is resized, I had to use: @self.document().documentLayout().documentSizeChanged.connect(self.wrapHeightToContents)@

        Thanks.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          Ah good, I didn't remember that QTextDocument had that signal. That would work even more elegantly.

          http://www.catb.org/~esr/faqs/smart-questions.html

          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