from PyQt4 import QtCore, QtGui

class RectWidget(QtGui.QGraphicsWidget):
   def __init__(self, rect, parent = None):
      QtGui.QGraphicsWidget.__init__(self, parent)

      self._boundingRect = rect

   def boundingRect(self):
      return self._boundingRect

   def sizeHint(self, which, constraint = QtCore.QSizeF()):
      #if QtCore.Qt.PreferredSize == which or \
      #   QtCore.Qt.MinimumSize == which:
      #   return self._boundingRect.size()
      return self._boundingRect.size()

   def paint(self, painter, option, widget):
      painter.setPen(QtCore.Qt.red)
      painter.drawRect(self._boundingRect)


if __name__ == "__main__":
   app = QtGui.QApplication([])

   scene = QtGui.QGraphicsScene(0.0, 0.0, 600.0, 600.0)
   view = QtGui.QGraphicsView(scene)
   view.setWindowTitle("Alert Windows")

   panel = QtGui.QGraphicsWidget()
   scene.addItem(panel)
   layout = QtGui.QGraphicsGridLayout()
   panel.setLayout(layout)

   rect0 = RectWidget(QtCore.QRectF(0.0, 0.0, 16.0, 16.0))
   rect1 = RectWidget(QtCore.QRectF(0.0, 0.0, 16.0, 16.0))
   rect2 = RectWidget(QtCore.QRectF(0.0, 0.0, 16.0, 16.0))
   rect3 = RectWidget(QtCore.QRectF(0.0, 0.0, 16.0, 16.0))

   layout.addItem(rect0, 0, 0, 1, 1)
   layout.addItem(rect1, 0, 1, 1, 1)
   layout.addItem(rect2, 0, 2, 1, 1)
   layout.addItem(rect3, 0, 3, 1, 1)

   bottom_rect = RectWidget(QtCore.QRectF(0.0, 0.0, 100.0, 100.0))

   # If we add to a single column it pushes the top rects into their correct position.
   #layout.addItem(bottom_rect, 1, 0, 1, 1)
   # If we span two columns the top row does not maintain it's positions.
   layout.addItem(bottom_rect, 1, 0, 1, 2)

   view.show()
   app.exec_()
