Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting word wrap on QLabel breaks size constrains for the window

Tags:

python

qt

pyqt

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:

enter image description here

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:

enter image description here

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:

enter image description here

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
like image 962
Fenikso Avatar asked Sep 03 '14 12:09

Fenikso


1 Answers

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_())
like image 85
Fenikso Avatar answered Oct 31 '22 10:10

Fenikso