import sys
from PyQt5 import QtWidgets, QtCore
class MyWidget (QtWidgets.QWidget):
def __init __ (self, i = 0):
super () .__ init __ ()
self.setWindowTitle (‘Window‘ + str (i))
label = QtWidgets.QLabel (‘label‘ + str (i))
layout = QtWidgets.QHBoxLayout (self)
layout.addWidget (label)
class TestWidget (QtWidgets.QWidget):
def __init __ (self, parent = None):
super () .__ init __ (parent)
layout = QtWidgets.QHBoxLayout (self)
#stack = QtWidgets.QStackedWidget () # ① Widget
stack = QtWidgets.QStackedLayout () # ② Layout
list = QtWidgets.QListWidget (self)
list.setDragEnabled (True)
list.setDragDropMode (QtWidgets.QAbstractItemView.InternalMove)
layout.addWidget (list)
# layout.addWidget (stack) # ① corresponding to addWidget
layout.addLayout (stack) # ② corresponds to addLayout
for i in range (10):
stack.addWidget (MyWidget (i))
list.addItem ("window% 04i"% i)
list.currentRowChanged.connect (stack.setCurrentIndex)
if __name__ == "__main__":
app = QtWidgets.QApplication (sys.argv)
widget = TestWidget ()
#widget = MyWidget ()
widget.show ()
sys.exit (app.exec_ ())
The difference between the usage of Qstackedwidget and qstackedlayout