Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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.


  • Banned

    Can you please supply an MRE (Minimal Reproducible Example) of what you have as this facilitates us helping you with your issue.



  • 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()
    


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



  • @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



  • @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.



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



  • @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.



  • 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.


Log in to reply