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