QLabel setting word wrap breaks window size limits

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, creating a window whose size cannot be smaller:

enter image description here

However, when I uncomment the line self.label.setWordWrap(True) , these restrictions seem to disappear. I can resize the window to a smaller one, completely breaking the layout, since QTextEdit still retains the size limit:

enter image description here

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

enter image description here

Is there a way to fix this and make the window / layout still match the minimum QTextEdit size even if word wrap is enabled?

Version Information:

 OS: Windows-7-SP1 (32bit) Python: 3.4.1 PyQt: 5.3.1 Qt: 5.3.1 
+5
source share
2 answers

As @thuga explained in the comment, the problem is mentioned in docs and, apparently, already with the permission “will not / cannot fix”.

I found a hint for a workaround for this particular problem here . It works for my example as well as my application: self.setMinimumSize(self.sizeHint()) .

Then the code 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_()) 
+3
source

I also had this problem with QT 4.8 and I was unable to update QT since I was working on a QGIS 2.18 plugin that still uses QT 4.8.

My work works with maximumHeight QWebView (or in your case, your QTextEdit ) when resizing. You will need to fix the custom signal for resizing because it is not available in QT (see: PyQt: detecting resizing in the resized viewport )

So my code looks 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) 

275 is the value I need to save in order to display the dialog correctly (for the widget before QWebView. You will need to find the value that matches your dialog box.

0
source

Source: https://habr.com/ru/post/1201727/


All Articles