Python Forum
Nested QStackedLayout - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Nested QStackedLayout (/thread-37535.html)



Nested QStackedLayout - Milestone - Jun-23-2022

If I add a widget to QStackedLayout the count() show me the correct number of stacked layout. However if I use addChildLayout the count() show one less layout. I'm not able to access the layout that is created as a child layout.

"""
Stacked Layout
"""
from PyQt6.QtCore import QSize, Qt
from PyQt6.QtWidgets import (
                            QApplication,
                            QMainWindow,
                            QPushButton,
                            QLabel,
                            QStackedLayout,
                            QVBoxLayout,
                            QWidget
                            )
from random import randint
import sys


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Stacked Layout")
        self.setStyleSheet("background-color: teal;")
        self.setMinimumSize(QSize(300, 300))

        self.page_0_lbl = QLabel("Page 0")
        self.page_0_lbl.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.page_0_btn = QPushButton("Botton 0")
        self.page_1 = QLabel("Page 1")
        self.page_1.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.page_2 = QLabel("Page 2")
        self.page_2.setAlignment(Qt.AlignmentFlag.AlignCenter)

        self.vlayout_0 = QVBoxLayout()
        self.vlayout_0.addWidget(self.page_0_lbl)
        self.vlayout_0.addWidget(self.page_0_btn)

        self.stackedLayout = QStackedLayout()
        # <0>
        self.stackedLayout.addChildLayout(self.vlayout_0)
#         self.stackedLayout.addWidget(self.page_0_lbl)
        # <1>
        self.stackedLayout.addWidget(self.page_1)
        # <2>
        self.stackedLayout.addWidget(self.page_2)
        self.stackedLayout.setCurrentIndex(0)
        print(f"{self.stackedLayout.count() = }")

        widget = QWidget()
        widget.setLayout(self.stackedLayout)
        self.setCentralWidget(widget)


app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec()