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:
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.
-
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:
-
@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:
-
@JonB
Somenthing similar actually. The height is not the same as shown in the first picture. -
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.