1

I'm having trouble fitting 2 tables into QGridLayout. I've read some examples and thought I got the grip of it. Apparently, I didn't. This is how I visualized it:

enter image description here

So I supposed this code should work:

layout = QGridLayout()
layout.addWidget(smalltable, 0, 1, 1, 2)
layout.addWidget(bigtable, 1, 0, 4, 4)

But instead I got something like this:

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

1

One possible solution is to set the stretch factor using setRowStretch():

import sys

from PyQt5.QtWidgets import QApplication, QGridLayout, QTableWidget, QWidget

app = QApplication(sys.argv)

smalltable = QTableWidget(4, 4)
bigtable = QTableWidget(5, 5)

w = QWidget()

layout = QGridLayout(w)
layout.addWidget(smalltable, 0, 1, 1, 2)
layout.addWidget(bigtable, 1, 0, 1, 4)
layout.setRowStretch(0, 1)
layout.setRowStretch(1, 4)

w.resize(640, 480)
w.show()

sys.exit(app.exec_())

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thank you! The heights of big and small tables seem to be proportionally correct using setRowStretch() but they are still weird. The small table now is "aligned right". It stuck to the top right corner. – Michael Evergreen Dec 08 '20 at 20:18
  • @MichaelEvergreen Do you see that behavior using my code or in your original code where you try to implement my solution? If it is the first then it would be great if you provide an image of what you get, and if it is the second without any [MRE] it is impossible for me to know what is generating the error. To show that my solution works I have added a screenshot in my answer – eyllanesc Dec 08 '20 at 20:21
  • Thank you! I think I found out why. I commented out "layout.addWidget" for some buttons thinking that would make those buttons disappear but they actually stuck to the top left corner. – Michael Evergreen Dec 08 '20 at 20:37