I have the following code:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " \
"Nullam malesuada tellus in ex elementum, vitae rutrum enim vestibulum."
#==============================================================================
class Window(QWidget):
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
# Widgets
self.label = QLabel(TEXT, self)
# self.label.setWordWrap(True)
self.text = QTextEdit(self)
self.text.setMinimumSize(480, 320)
self.text.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
# Layout
self.layout = QGridLayout()
self.layout.addWidget(self.label, 0, 0)
self.layout.addWidget(self.text, 1, 0)
self.layout.setContentsMargins(5, 5, 5, 5)
self.layout.setSpacing(5)
self.setLayout(self.layout)
self.adjustSize()
self.show()
#==============================================================================
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())
It works as expected, producing a window, which cannot be resized to be smaller:

However, when I uncomment the self.label.setWordWrap(True) line, these constrains seem to disappear. I can resize the window to smaller, completely breaking the layout, as QTextEdit still retains its size constrain:

I tried to fix this using self.setMinimumSize(self.size()). This works for this particular example, however breaks if the size is bigger, such as self.text.setMinimumSize(480, 800). That makes the window too small even when it is being created, so setMinimumSize does not help:

Is there a way how to fix it and make the window / layout still match the minimum size of QTextEdit even when word wrap is enabled?
Version information:
OS: Windows-7-SP1 (32bit)
Python: 3.4.1
PyQt: 5.3.1
Qt: 5.3.1
As @thuga suggested in a comment the problem is mentioned in docs and already reported, apparently with "won't / can't fix" resolution.
I have found a hint for a workaround for this particular problem here. It works for my example and also my application:
self.setMinimumSize(self.sizeHint()).
The code then looks like this:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " \
"Nullam malesuada tellus in ex elementum, vitae rutrum enim vestibulum."
#==============================================================================
class Window(QWidget):
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
# Widgets
self.label = QLabel(TEXT, self)
self.label.setWordWrap(True)
self.text = QTextEdit(self)
self.text.setMinimumSize(480, 800)
self.text.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
# Layout
self.layout = QGridLayout()
self.layout.addWidget(self.label, 0, 0)
self.layout.addWidget(self.text, 1, 0)
self.layout.setContentsMargins(5, 5, 5, 5)
self.layout.setSpacing(5)
self.setLayout(self.layout)
self.setMinimumSize(self.sizeHint())
self.show()
#==============================================================================
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With