How to make a QTextBrowser that adapts its height with the text?
-
wrote on 9 Apr 2020, 19:59 last edited by Furquim 4 Sept 2020, 20:00
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.
-
wrote on 9 Apr 2020, 20:23 last edited by
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()
-
wrote on 9 Apr 2020, 20:31 last edited by
Does it alter behaviour if you add your
QTextBrowser
onto a layout, like on the central widget? -
Does it alter behaviour if you add your
QTextBrowser
onto a layout, like on the central widget?wrote on 9 Apr 2020, 22:06 last edited by@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()
-
@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.wrote on 10 Apr 2020, 06:42 last edited by@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. -
wrote on 10 Apr 2020, 09:34 last edited by
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.
2/8