Details
-
Bug
-
Resolution: Unresolved
-
P3: Somewhat important
-
None
-
5.11.3, 5.15.0
-
None
Description
Hi there,
It appears that there is no way to set the wrapping policy to `Qt::TextWrapAnywhere` so that a `QLabel` breaks lines anywhere, even within words. Instead there is only a boolean function to either turn on the policy `Qt::TextWordWrap`, which the docs say "breaks lines at appropriate points, e.g. at word boundaries." It is not clear what these appropriate points are (and I haven't been able to locate this in the Qt code base). More importantly, if there is no appropriate wrapping point, then QLabel just cuts off the text, as you can see in the example program and screenshot below.
The reason I think this is a bug is because there is no way to avoid cutting off text in a `QLabel` if there is no wrapping point within the text string, it is unknown what the supported wrapping points are, so you don't know whether or not your text will be cut off, and using `QTextEdit` or `QPlainTextEdit` as an alternative is not a viable option because of the resizing bug (which I can file a bug report for later but also see the example program above and how QPlainTextEdit doesn't resize properly so that the text is fitted, which become a much larger issue when there are special characters and lots of newlines, etc).
import sys from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QPlainTextEdit, QWidget app = QApplication(sys.argv) window = QMainWindow() centralWidget = QWidget() centralWidget.setFixedHeight(200) window.setCentralWidget(centralWidget) string = 'https://doesnotwraponperiods.com/but/does/wrap/on-forward-slashes/and-dashes' label = QLabel(parent=centralWidget) label.setMaximumWidth(100) label.setWordWrap(True) label.setStyleSheet('QLabel {background-color: #00FF00;}') label.setText(string) # Try using QPlainTextEdit instead and see how it doesn't resize so that the text is fitted within the textedit box # textedit = QPlainTextEdit(string, parent=centralWidget) # textedit.setMaximumWidth(100) # textedit.setMinimumHeight(20) # textedit.setStyleSheet('QPlainTextEdit {background-color: #00FF00;}') window.show() sys.exit(app.exec_())
Here are some other useful test strings to highlight the issue where `QLabel` wraps at various unknown points and cuts off text as well as the issue with QTextDocument not resizing properly, which is much worse for strings with lots of special characters and paragraphs:
# string = '~!@#$%^&*()_+{}|:"<>?' # string = 'gBbRmlsZSBzb3VyY2U6IGh0dHBzOi8vY29tbW9ucy53aWtpbWVkaWEub3JnL3dpa2kvRmlsZTpMb3dfcHJlc3N1cmVfc3lzdGVtX292ZXJfSWNlbGFuZC5qcGf' # string = 'Ω≈ç√∫˜µ≤≥÷åß∂ƒ©˙∆˚¬…æœ∑´®†¥¨ˆøπ“‘'