Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested QStackedLayout
#1
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()
Larz60+ write Jun-23-2022, 06:51 PM:
Please use python tags, not quote for code.
Fixed for you.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020