2

I have 3 PlotWidget (from the pyqtgraph library), obj1, obj2 and obj3, which I try to insert in a QGridLayout. I want to display all three objects in a single row, but obj1 must be twice as large as obj2 and obj3.

Thus I wrote:

layout.addWIdget(obj1, 0, 0, 1, 2)
layout.addWidget(obj2, 0, 2, 1, 1)
layout.addWidget(obj3, 0, 3, 1, 1)

However, visually, obj1 is much smaller than obj2 and obj3 which do have the same size:

---------------------------------------------------
| obj1 |     obj2           |         obj3         |
---------------------------------------------------

However, if I write:

layout.addWIdget(obj1, 0, 0, 1, 1)
layout.addWidget(obj2, 0, 1, 1, 6)
layout.addWidget(obj3, 0, 7, 1, 6)

obj1 appears bigger than obj2 and obj3 which are still of the same size, which is the expected behavior:

---------------------------------------------------
|          obj1           |    obj2   |    obj3   |
---------------------------------------------------

It seems totally contradictory to the doc of addWidget as my first solution should yield the expected behavior, no?

EDIT: Piece of code

import pyqtgraph as qtg

obj1 = qtg.PlotWidget()
obj2 = qtg.PlotWidget()
obj3 = qtg.PlotWidget()

layout = QGridLayout()

layout.addWIdget(obj1, 0, 0, 1, 2)
layout.addWidget(obj2, 0, 2, 1, 1)
layout.addWidget(obj3, 0, 3, 1, 1)

box = QGroupBox()
box.setLayout(layout)
self.setCentralWidget(box)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
floflo29
  • 2,261
  • 2
  • 22
  • 45

1 Answers1

1

When you use void QGridLayout::addWidget(QWidget* widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment = 0), rowSpan and columnSpan refers to the other row or another column, and as in your case you have just given a row the rowSpan is not applied, for example if you add a new row you get the following:

obj1 = qtg.PlotWidget()
obj2 = qtg.PlotWidget()
obj3 = qtg.PlotWidget()

obj4 = qtg.PlotWidget()
obj5 = qtg.PlotWidget()
obj6 = qtg.PlotWidget()
obj7 = qtg.PlotWidget()

layout = QGridLayout()

layout.addWidget(obj1, 0, 0, 1, 2)
layout.addWidget(obj2, 0, 2, 1, 1)
layout.addWidget(obj3, 0, 3, 1, 1)
layout.addWidget(obj4, 1, 0, 1, 1)
layout.addWidget(obj5, 1, 1, 1, 1)
layout.addWidget(obj6, 1, 2, 1, 1)
layout.addWidget(obj7, 1, 3, 1, 1)

box = QGroupBox(self)
box.setLayout(layout)
self.setCentralWidget(box)

enter image description here

If you need to establish the proportions you must use stretch, in this case setColumnStretch():

obj1 = qtg.PlotWidget()
obj2 = qtg.PlotWidget()
obj3 = qtg.PlotWidget()

layout = QGridLayout()

layout.addWidget(obj1, 0, 0, 1, 2)
layout.addWidget(obj2, 0, 2, 1, 1)
layout.addWidget(obj3, 0, 3, 1, 1)
layout.setColumnStretch(0, 2)
layout.setColumnStretch(2, 1)
layout.setColumnStretch(3, 1)

box = QGroupBox(self)
box.setLayout(layout)
self.setCentralWidget(box)

enter image description here

The same effect you could get through QHBoxLayout:

obj1 = qtg.PlotWidget()
obj2 = qtg.PlotWidget()
obj3 = qtg.PlotWidget()

layout = QHBoxLayout()

layout.addWidget(obj1, stretch=2)
layout.addWidget(obj2, stretch=1)
layout.addWidget(obj3, stretch=1)

box = QGroupBox(self)
box.setLayout(layout)
self.setCentralWidget(box)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241