PyQt5 结合 QVBoxLayout 和 QStackedLayout
PyQt5 Combining QVBoxLayout and QStackedLayout
QVBoxLayout
可以在小部件中包含 QStackedLayout
吗?我正在尝试创建一个如下所示的自定义小部件:
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_())
我收到以下错误:
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.
您可以向 QVBoxLayout
添加任何布局,包括 QStackedLayout
,但您需要为此使用 layout.addLayout
,而不是 layout.addWidget
。 MyCustomWidget
的定义中还有一些其他问题。这是一个应该有效的版本:
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])
QVBoxLayout
可以在小部件中包含 QStackedLayout
吗?我正在尝试创建一个如下所示的自定义小部件:
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_())
我收到以下错误:
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.
您可以向 QVBoxLayout
添加任何布局,包括 QStackedLayout
,但您需要为此使用 layout.addLayout
,而不是 layout.addWidget
。 MyCustomWidget
的定义中还有一些其他问题。这是一个应该有效的版本:
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])