Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to make a QTextBrowser that adapts its height with the text?

How to make a QTextBrowser that adapts its height with the text?

Scheduled Pinned Locked Moved Unsolved Qt for Python
8 Posts 4 Posters 2.5k Views
  • 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.
  • F Offline
    F Offline
    Furquim
    wrote on 9 Apr 2020, 19:59 last edited by Furquim 4 Sept 2020, 20:00
    #1

    I'm trying to add a text in the QTextBrowser. But I want the whole text to be visible without the scrollbar. I already tryed to use the function setText() and setHmtl(). But neither of them seens to adapt the size of the QTextBrowser.

    For example: I want to add the following text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit." So I use setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.") in my QTextBrowser object. And that's what happens:

    qtextbrowserquestion.png

    I don't want the scrollbar to appear. The second (below) option is what I want. Is there an easy way to adapt the size with its content after adding the text?

    Thanks for your attention.

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Furquim
      wrote on 9 Apr 2020, 20:23 last edited by
      #2

      Here is the MRE:

      import sys, os
      from PyQt5 import QtCore, QtGui, QtWidgets, uic
      from PyQt5.QtGui import QIcon, QDoubleValidator
      from PyQt5.QtWidgets import QLineEdit, QTextBrowser, QPushButton
      
      class MyWindow(QtWidgets.QMainWindow):
          def __init__(self):
              QtWidgets.QMainWindow.__init__(self)
      
              self.textBrowser = QTextBrowser(self)
              self.textBrowser.setFixedWidth(100)
              self.textBrowser.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
      
      app = QtWidgets.QApplication(sys.argv)
      window = MyWindow()
      window.show()
      app.exec()
      
      1 Reply Last reply
      0
      • J Offline
        J Offline
        JonB
        wrote on 9 Apr 2020, 20:31 last edited by
        #3

        Does it alter behaviour if you add your QTextBrowser onto a layout, like on the central widget?

        F 1 Reply Last reply 9 Apr 2020, 22:06
        0
        • J JonB
          9 Apr 2020, 20:31

          Does it alter behaviour if you add your QTextBrowser onto a layout, like on the central widget?

          F Offline
          F Offline
          Furquim
          wrote on 9 Apr 2020, 22:06 last edited by
          #4

          @JonB, yes. if I do this:

          import sys, os
          from PyQt5 import QtCore, QtGui, QtWidgets, uic
          from PyQt5.QtGui import QIcon, QDoubleValidator
          from PyQt5.QtWidgets import QLineEdit, QTextBrowser, QPushButton
          
          class MyWindow(QtWidgets.QMainWindow):
              def __init__(self):
                  QtWidgets.QMainWindow.__init__(self)
          
                  self.textBrowser = QTextBrowser()
                  self.textBrowser.setFixedWidth(100)
                  self.textBrowser.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
          
                  self.setCentralWidget(self.textBrowser)
          
          app = QtWidgets.QApplication(sys.argv)
          window = MyWindow()
          window.show()
          app.exec()
          
          

          It's appears like this:
          b34a959d-7e07-4926-a146-c24888fb6216-image.png

          J 1 Reply Last reply 9 Apr 2020, 22:42
          0
          • F Furquim
            9 Apr 2020, 22:06

            @JonB, yes. if I do this:

            import sys, os
            from PyQt5 import QtCore, QtGui, QtWidgets, uic
            from PyQt5.QtGui import QIcon, QDoubleValidator
            from PyQt5.QtWidgets import QLineEdit, QTextBrowser, QPushButton
            
            class MyWindow(QtWidgets.QMainWindow):
                def __init__(self):
                    QtWidgets.QMainWindow.__init__(self)
            
                    self.textBrowser = QTextBrowser()
                    self.textBrowser.setFixedWidth(100)
                    self.textBrowser.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
            
                    self.setCentralWidget(self.textBrowser)
            
            app = QtWidgets.QApplication(sys.argv)
            window = MyWindow()
            window.show()
            app.exec()
            
            

            It's appears like this:
            b34a959d-7e07-4926-a146-c24888fb6216-image.png

            J Offline
            J Offline
            JonB
            wrote on 9 Apr 2020, 22:42 last edited by
            #5

            @Furquim
            And isn't that what you were asking for? When you place widgets you usually want them on a layout, else they do not behave as you expect.

            1 Reply Last reply
            0
            • F Offline
              F Offline
              Furquim
              wrote on 9 Apr 2020, 22:57 last edited by
              #6

              @JonB
              Somenthing similar actually. The height is not the same as shown in the first picture.

              J 1 Reply Last reply 10 Apr 2020, 06:42
              0
              • F Furquim
                9 Apr 2020, 22:57

                @JonB
                Somenthing similar actually. The height is not the same as shown in the first picture.

                J Offline
                J Offline
                JonB
                wrote on 10 Apr 2020, 06:42 last edited by
                #7

                @Furquim
                Well this is now at least the right place to start from. You can start fiddling with properties on the widget or on the layout from here. The point being, that unless a widget is on a layout you tend to get inexplicable sizing/positioning etc.

                1 Reply Last reply
                0
                • V Offline
                  V Offline
                  Volodymyr14
                  wrote on 10 Apr 2020, 09:34 last edited by
                  #8

                  Better to create the text browser height as the height of parent element that contains text browser, you don`t know, really, how many texts will be used and for big text, the scrolling is required.

                  PyQt/PySide

                  1 Reply Last reply
                  0

                  4/8

                  9 Apr 2020, 22:06

                  • Login

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