pyqt5qwidgetpyqtgraphqvboxlayout

PyQt5 Combining QVBoxLayout and QStackedLayout


Can QVBoxLayout contain a QStackedLayout in a widget? I am trying to create a custom widget that looks like the following:

enter image description here

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.

Solution

  • You can add any layout to a QVBoxLayout including a QStackedLayout, but you need to use layout.addLayout for this, not layout.addWidget. There are a few other things off in the definition of MyCustomWidget. Here is a version that should work:

    class MyCustomWidget(QWidget):
        def __init__(self):
            super().__init__()
    
            self.my_combo_box = QComboBox(self)
            self.vlayout = QVBoxLayout(self)
            self.slayout = QStackedLayout()
    
            self.vlayout.addWidget(self.my_combo_box)
            # add stacked layout to self.vlayout (only need to be done once).
            self.vlayout.addLayout(self.slayout)
    
            self.graphWidget = {}
            
            # self.widget isn't doing anything in your code so I commented out this line
            # self.widget = QWidget()
    
            # Add signal-slot connection to show correct graph when current index in combo box is changed.
            self.my_combo_box.currentIndexChanged.connect(self.slayout.setCurrentIndex)
    
    
        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])