maurobio
maurobio

Reputation: 1577

How to control size of widgets in Qt/PyQt/PySide grid layout

I am struck with a foolish but annoying question. I would like to set different sizes for two list widgets on a grid layout, one above the other. So, I would like to set 60% of the form space to the upper widget and 40% to the lower widget. I attempted to use setRowStretch, without success.

Here is my code:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

def window():
    app = QApplication(sys.argv)
    win = QWidget()

    list1 = QListView()
    list2 = QListView()

    grid = QGridLayout()
    grid.setRowStretch(6, 4)
    grid.addWidget(list1)
    grid.setSpacing(2)
    grid.addWidget(list2)

    win.setLayout(grid)
    win.setGeometry(300, 150, 350, 300)
    win.setWindowTitle("Example")
    win.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
   window()

Thanks for any assistance you can provide.

Upvotes: 2

Views: 11634

Answers (1)

titusjan
titusjan

Reputation: 5546

The first parameter of the rowStretch method is the row number, the second is the stretch factor. So you need two calls to rowStretch, like this:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

def window():
    app = QApplication(sys.argv)
    win = QWidget()

    list1 = QListView()
    list2 = QListView()

    grid = QGridLayout()
    grid.setRowStretch(0, 6)
    grid.setRowStretch(1, 4)
    grid.addWidget(list1)
    grid.setSpacing(2)
    grid.addWidget(list2)

    win.setLayout(grid)
    win.setGeometry(300, 150, 350, 300)
    win.setWindowTitle("Example")
    win.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
   window()

Upvotes: 5

Related Questions