7
votes

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
2
This issue is mentioned in the docs. There is also a bug report.thuga
@thuga - Wow! Thanks. I would not expect this.Fenikso
@thuga the bug report link is missing, perhaps it's this one: bugreports.qt.io/browse/QTBUG-37673ismailsunni

2 Answers

4
votes

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_())
0
votes

I also had this problem, with QT 4.8, and I couldn't upgrade my QT, since I was working on a plugin of QGIS 2.18 that still use QT 4.8.

My work around is manipulating the maximumHeight of the QWebView (or in your case, your QTextEdit) when the resize happen. You will need to emit custom signal for resized, since it's not available in QT (see: PyQt: Detect resizing in Widget-window resized signal)

So, my code is something like this:

class MetadataConverterDialog(QDialog):
    resized = pyqtSignal()
        # Adapted from https://stackoverflow.com/a/43126946/1198772
    def resizeEvent(self, event):
        """Emit custom signal when the window is re-sized.

        :param event: The re-sized event.
        :type event: QResizeEvent
        """
        self.resized.emit()
        return super(MetadataConverterDialog, self).resizeEvent(event)

    def after_resize(self):
        """Method after resizing the window."""
        max_height = self.height() - 275  # Magic number, to make it pretty
        self.metadata_preview_web_view.setMaximumHeight(max_height)

The 275 is the value that I need to spare to show the dialog properly (for the widget before the QWebView. You will need to find the value that fits with your dialog.