QGridLayout with PlotWidget-pyqtgraph

﹥>﹥吖頭↗ 提交于 2019-12-08 07:51:58

问题


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)

回答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)

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)

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)


来源:https://stackoverflow.com/questions/47681774/qgridlayout-with-plotwidget-pyqtgraph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!