How do I rebuild a gridLayout during resize?
<p>I am trying to catch resize event of the window, and when I do, basically delete all the widget items in a gridLayout and rebuild the rows/columns to fit the new resized window.  I am having trouble getting this to work properly and not sure if this is the best method that I have used.  Right now two issues happen:</p>

<ol>
<li><p>It doesn't seem to be deleting items, rebuilding and adding the columns properly as I resize the window bigger (some items delete, some get added, but seem to just overlap and never fit to the new window size).  </p></li>
<li><p>Resize seems to get called on start/creation of the window.</p></li>
</ol>



<pre><code>class Window (QtGui.QMainWindow):
    def __init__(self):
        super(Window, self).__init__()
        self.thumbs = []
        self.thumbWidgets = []

        self._resize_timer = None
        self.resizeCompleted.connect(self.handleResizeCompleted)

        self.setGeometry(100, 100, 800, 600)
        self.home()

    def home(self):

        self.centralwidget = QtGui.QWidget(self)

        '''MainLAYOUT
        '''
        self.mainLayout = QtGui.QVBoxLayout(self.centralwidget)

        self.thumb_tab_QGroupBox = QtGui.QGroupBox(self.centralwidget)

        '''GroupBoxLAYOUT
        '''
        self.vLayout =  QtGui.QVBoxLayout(self.thumb_tab_QGroupBox)
        self.vLayout.setObjectName("GroupVLayout")


        #Scroll Area
        self.thumbScrollArea = QtGui.QScrollArea(self.thumb_tab_QGroupBox)
        self.thumbScrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.thumbScrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.thumbScrollArea.setWidgetResizable(True)
        self.thumbScrollArea.setAlignment(QtCore.Qt.AlignLeft)
        self.thumbScrollArea.setObjectName("thumb_scrollArea")

        self.scrollAreaWidgetContents = QtGui.QWidget()
        self.scrollAreaWidgetContents.setMinimumSize(QtCore.QSize(840, scrollAreaX))
        self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
        self.thumbScrollArea.setWidget(self.scrollAreaWidgetContents)

        self.vLayout.addWidget(self.thumbScrollArea)
        self.mainLayout.addWidget(self.thumb_tab_QGroupBox)

        #Grid in Scroll Area
        self.gridLayoutWidget = QtGui.QWidget(self.scrollAreaWidgetContents)
        self.gridLayoutWidget.setObjectName("gridLayoutWidget")
        self.gridLayout_QGridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
        self.gridLayout_QGridLayout.setObjectName("gridLayout")

        #Loads thumbnails
        self.getThumbnails()

        self.mainLayout.setAlignment(QtCore.Qt.AlignLeft)
        self.setCentralWidget(self.centralwidget)

    def resizeEvent(self, resizeEvent):
        self.updateResizeTimer(300)

    def updateResizeTimer(self, interval=None):
        if self._resize_timer is not None:
            self.killTimer(self._resize_timer)
        if interval is not None:
            self._resize_timer = self.startTimer(interval)
        else:
            self._resize_timer = None

    def timerEvent(self, event):
        if event.timerId() == self._resize_timer:
            self.updateResizeTimer()
            self.resizeCompleted.emit()

    def handleResizeCompleted(self):
        print('resize complete')

        # Get new window size on resize
        width = self.centralwidget.frameGeometry().width()
        height = self.centralwidget.frameGeometry().height()

        thumbsPerRow = width / 200
        print "numThumbnails per Width", thumbsPerRow

        self.gridLayoutWidget.adjustSize()
        self.gridLayout_QGridLayout.maximumSize()

        for widget in self.thumbWidgets:
            print "Removing widget", widget
            self.gridLayout_QGridLayout.removeWidget(widget)
            #widget.deleteLater()

        self.populate(self.thumbWidgets, QtCore.QSize(200,200), thumbsPerRow)

    def queryThumbnailCount(self):
        ....
        ...
        ..
        return sizeX

    def getThumbnails(self):
        .....
        ....
        ...
        .
        self.createThumbWidgets(self.thumbs, QtCore.QSize(200,200))
        self.populate(self.thumbs, QtCore.QSize(200,200))

    def createThumbWidgets(self, pics, size, imagesPerRow=4, flags=QtCore.Qt.KeepAspectRatioByExpanding):
        for pic in pics:           
            label = QtGui.QLabel("")
            pixmap = QtGui.QPixmap(pic)
            pixmap = pixmap.scaled(size, flags)
            label.setPixmap(pixmap)
            self.thumbWidgets.append(label)


    #Add thumbnails to grid
    def populate(self, pics, size, imagesPerRow=6, flags=QtCore.Qt.KeepAspectRatioByExpanding):
        row = col = 0
        for widget in self.thumbWidgets:          
            print "Adding Image to column "+str(col)
            self.gridLayout_QGridLayout.addWidget(widget, row, col)
            col +=1
            if col % imagesPerRow == 0:
                row += 1
                col = 0        


GUI = Window()
GUI.show()
</code></pre>