4-5 years old I need a widget with the following properties
- Show text on HTML
- The text should be wrapped in several lines
- When the widget is placed in the layout, the height of the widget must be adjusted so that the text exactly matches the geometry of the widget
This sub-widget should be used in the layout to provide some details on how the other GUI elements in the layout work, but only consumes minimal space to display its contents.
I thought it was easy, but every time I get back to the challenge, I always end up refusing.
The main problem is that the layout breaks when implementing heightForWidth () and uses QSizePolicy with setHeightForWidth (True). It can shrink to infinitesimal. This seems to be a Qt bug.
Another approach is to call updateGeometry () when resizeEvent () occurs and calls setFixedHeight (h), using a height dependent width. But it also leads to some weird layout behavior.
If anyone has any good suggestions on how to approach this, please let me know.
Below I include a snippet that reproduces the behavior of the resize layout.
Yours faithfully,
Mads
import sys from PyQt4 import QtCore, QtGui class Square(QtGui.QLabel): def __init__(self, parent=None): QtGui.QLabel.__init__(self, parent) self.setAutoFillBackground(True) palette = QtGui.QPalette() palette.setColor(QtGui.QPalette.Window, QtGui.QColor('red')) self.setPalette(palette) policy = self.sizePolicy() policy.setHeightForWidth(True) self.setSizePolicy(policy) def sizeHint(self): return QtCore.QSize(128, 128) def heightForWidth(self, width): return width class Widget(QtGui.QWidget): def __init__(self, parent=None):
source share