 <p>Let's suppose that you have a <code>QGridLayout</code> with 4 rows and 3 columns and you want to add buttons to it automatically from top to bottom and from left to right. That can easily be achieved if you are able to predict the position of the next button to be added. In our case:</p> <ul> <li>row = number of added buttons / number of columns </li> <li>column = number of added buttons % number of columns</li> </ul> <p>(other type of filling work similarly). Let's put it in code:</p> <pre><code>from PyQt4.QtGui import * class MyMainWindow(QMainWindow): def __init__(self, parent=None): super(MyMainWindow, self).__init__(parent) self.central = QWidget(self) self.grid = QGridLayout(self.central) self.rows = 4 self.cols = 3 self.items = self.grid.count() while(self.items &lt; (self.rows * self.cols)): self.addButton() self.setCentralWidget(self.central) def addButton(self): # the next free position depends on the number of added items row = self.items/self.cols col = self.items % self.cols # add the button to the next free position button = QPushButton("%s, %s" % (row, col)) self.grid.addWidget(button, row, col) # update the number of items self.items = self.grid.count() if __name__ == "__main__": import sys app = QApplication(sys.argv) ui = MyMainWindow() ui.show() sys.exit(app.exec_()) </code></pre>