QGridLayout与PlotWidget-pyqtgraph

时间:2017-12-06 19:09:45

标签: python qt pyqtgraph

我有3 PlotWidget(来自pyqtgraph库),obj1obj2obj3,我尝试在QGridLayout中插入。我想在一行中显示所有三个对象,但obj1必须是obj2obj3的两倍。

因此我写道:

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

但是,从视觉上看,obj1远小于obj2obj3,它们的大小相同:

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

但是,如果我写:

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

obj1似乎大于obj2obj3,它们的大小仍然相同,这是预期的行为:

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

这似乎与addWidget的文档完全矛盾,因为我的第一个解决方案应该产生预期的行为,不是吗?

编辑:一段代码

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 个答案:

答案 0 :(得分:0)

当您使用void QGridLayout::addWidget(QWidget* widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment = 0)时,rowSpan和columnSpan引用另一行或另一列,并且在您的情况下,您刚刚给出了一行rowSpan未应用,例如,如果您添加新行你得到以下信息:

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

如果您需要确定比例,则必须使用拉伸,在本例中为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

您可以通过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)