


Self.document().setTextWidth(self.viewport().width()) In order to properly set a vertical height based on the contents, you should set the document's textWidth, and also call autoResize everytime the widget is resized. Then another problem arises: if the window is resized, the contents will not adapt until the text is changed.


"Serenade in G K.525 Eine kleine Nachtmusik\u20141. "Ultimate Mozart\u2014 The Essential Masterpieces", 6 In A Minor For Solo Violin RV 356\u2014Presto", "12 Concertos Op.3 \u2014 L'estro Armonico \u2014 Concerto No. (Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignTop) Self.verticalLayout = QVBoxLayout(self.scrollAreaWidgetContents) Self.scrollAreaWidgetContents = QWidget() Self.scrollArea = QScrollArea(self.centralwidget) Self.vbox = QVBoxLayout(self.centralwidget) Height = int(self.document().size().height() + () + ())Ĭenter = self.screen().availableGeometry().center() Self.margins = mouseDoubleClickEvent(self, e: QMouseEvent) -> None: I want to automatically resize the QTextEdit to fit to the wrapped text, the text itself will always be in one line, and the wrapped text can have multiple lines, I want the QTextEdits to fit to the height of wrapped lines.īy hours upon hours of Google searching, I have found a solution, but it doesn't work as expected, there can sometimes be one extra line at the bottom, I will post example code below: from PyQt6.QtCore import * The texts can often get very long and the horizontal space is limited by design, and QTextEdit automatically wraps text in multiple lines which is good. I have a bunch of QTextEdits in a QVBoxLayout in a QScrollArea.
