Can QVBoxLayout
contain a QStackedLayout
in a widget? I am trying to create a custom widget that looks like the following:
import sys
from PyQt5.QtWidgets import *
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg
class MyCustomWidget(QWidget):
def __init__(self):
super().__init__()
self.my_combo_box = QComboBox(self)
self.vlayout = QVBoxLayout(self)
self.slayout = QStackedLayout(self)
self.vlayout.addWidget(self.my_combo_box)
self.graphWidget = {}
self.widget = QWidget()
def add_new_graphics(self, name, x, y):
self.my_combo_box.addItem(name)
self.graphWidget[name] = pg.PlotWidget()
self.graphWidget[name].plot(x, y)
self.slayout.addWidget(self.graphWidget[name])
self.vlayout.addWidget(self.slayout)
self.widget.setLayout(self.vlayout)
app = QApplication([])
a = MyCustomWidget()
x1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y1 = [30, 32, 34, 32, 33, 31, 29, 32, 35, 45]
a.add_new_graphics('data1', x1, y1)
x2 = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
y2 = [0, 2, 4, 2, 3, 1, 9, 2, 5, 5]
a.add_new_graphics('data2', x2, y2)
a.show()
sys.exit(app.exec_())
I get the following errors:
TypeError: addWidget(self, QWidget, stretch: int = 0, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment()): argument 1 has unexpected type 'QStackedLayout'
sys:1: RuntimeWarning: Visible window deleted. To prevent this, store a reference to the window object.
You can add any layout to a
QVBoxLayout
including aQStackedLayout
, but you need to uselayout.addLayout
for this, notlayout.addWidget
. There are a few other things off in the definition ofMyCustomWidget
. Here is a version that should work: