title: heightForWidth label
body: <p>4-5 years I needed a widget with the following properties</p>
  
  <ul>
  <li>Display text incl. HTML </li>
  <li>Text should be wrapped on several lines </li>
  <li>When the widget is put into a layout, the height of the widget should be adjusted in such a way that the text exactly fits the widget geometry</li>
  </ul>
  
  <p>This subwidget should be used in a layout to provide some detail on how the other GUI elements in the layout work but only consume a minimum space to display its content.</p>
  
  <p>I thought this was an easy one - but each time I return to the challenge I always end by giving up.</p>
  
  <p>The main problem is that the layout breaks down when heightForWidth() is implemented and a QSizePolicy with setHeightForWidth(True) is used. It can shrink to infinitely small. Apparently this is Qt bug.</p>
  
  <p>Another approach is to call updateGeometry() when a resizeEvent() occurs and call setFixedHeight(h) using a width dependent height. But this also gives rise to some weird layout behavior.</p>
  
  <p>If anybody has any good suggestions on how to approach this, please let me know.</p>
  
  <p>Below I include a snippet that reproduces the layout resizing behavior.</p>
  
  <p>Best regards,</p>
  
  <p>Mads</p>
  
  <pre><code>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):
          # Call base class constructor
          QtGui.QWidget.__init__(self, parent)
  
          # Add a layout
          layout = QtGui.QVBoxLayout()
          self.setLayout(layout)
  
          # Add Square
          label = Square()
          layout.addWidget(label)
  
          spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
          layout.addItem(spacerItem)
  
          # Some dummy button
          self._push_button = QtGui.QPushButton('Press me')
          layout.addWidget(self._push_button)
  
  if __name__ == "__main__":
      app = QtGui.QApplication(sys.argv)
  
      widget = Widget()
      widget.show()
  
      sys.exit(app.exec_())
  </code></pre>