PyQT Buttons has Incorrect Size in QGridLayout
<p>I just started a simple PyQt app on Windows 7 and Python 2.7. There are 2 buttons and a table. The Apple button should be 5x taller that the Orange button, and the table should be the same height as the Apple button.</p>

<p>However both buttons are drawn the same height despite using <code>grid.addWidget(appleBtn, 0, 0, 5, 1)</code> to define its height.</p>

<p>Any suggestions?</p>

<p><a href="http://i.stack.imgur.com/cSAJg.png" rel="nofollow"><img src="http://i.stack.imgur.com/cSAJg.png" alt="enter image description here"></a></p>

<pre><code>from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys


def main():

    app     = QApplication(sys.argv)
    w       = QTabWidget()

    # Tab
    grid = QGridLayout()
    tab = QWidget() 
    tab.setLayout(grid)
    w.addTab(tab,"Hello World")


    # Button 1
    appleBtn = QPushButton("Apples")
    appleBtn.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)   
    grid.addWidget(appleBtn, 0, 0, 5, 1)


    # Button 1
    orangeBtn = QPushButton("Oranges")
    appleBtn.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)   
    grid.addWidget(orangeBtn, 5, 0, 1, 1)


    # Table
    fruitTable = QTableWidget()
    fruitTable.setRowCount(5)
    fruitTable.setColumnCount(2)
    fruitTable.setHorizontalHeaderLabels(QString("Fruit;Color;").split(";"))
    fruitTable.horizontalHeader().setResizeMode(QHeaderView.Stretch)
    grid.addWidget(fruitTable, 6, 0, 1, 1)


    w.resize(400,300)
    w.setWindowTitle('Test')
    w.show()
    sys.exit(app.exec_())


main()
</code></pre>

<p>The <code>addWidget</code> method doesn't work in the way you assume it does. The second and third arguments specify the row/column, whilst the third and fourth specify how many rows/columns to span.</p>

<p>The correct way to specify proportional heights is with <code>setRowStretch</code>:</p>

<pre><code>grid.addWidget(appleBtn, 0, 0)
grid.setRowStretch(0, 5)
...
grid.addWidget(orangeBtn, 1, 0)
...
grid.addWidget(fruitTable, 2, 0)
grid.setRowStretch(2, 5)
</code></pre>

python python-2.7 qt pyqt