Noughbee
Noughbee

Reputation: 1

pyside6: Resizing a QVBoxLayout containing a widget with a QStackedLayout vs without?

I created a simple pyside6 GUI with a row containing a QLabel and a QLineEdit, followed by a row with a QPlainTextEdit:

from PySide6.QtWidgets import QWidget, QPlainTextEdit, QLabel, QVBoxLayout, QHBoxLayout, \
    QLineEdit, QStackedLayout, QMainWindow, QApplication


class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()

        main_layout = QVBoxLayout()

        row_layout = QHBoxLayout()
        row_layout.addWidget(QLabel('Enter Your Secret:'))
        row_layout.addWidget(QLineEdit())
        widget = QWidget()
        widget.setLayout(row_layout)
        main_layout.addWidget(widget)

        main_layout.addWidget(QPlainTextEdit())

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


app = QApplication()
window = MainWindow()
window.show()
app.exec()

The layout looks perfect and stays what I want when the window is resized: The first row never gets taller, and extra space when the window is resized is absorbed in the size of the QPlainTextEdit.

Now I convert the first row into a QStackedLayout, in preparation for replacing the first row with other things, as the GUI is running:

from PySide6.QtWidgets import QWidget, QPlainTextEdit, QLabel, QVBoxLayout, QHBoxLayout, \
    QLineEdit, QStackedLayout, QMainWindow, QApplication


class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()

        main_layout = QVBoxLayout()

        row_layout = QHBoxLayout()
        row_layout.addWidget(QLabel('Enter Your Secret:'))
        row_layout.addWidget(QLineEdit())
        widget = QWidget()
        widget.setLayout(row_layout)

        switcher = QStackedLayout()
        switcher.addWidget(widget)
        switcher.setCurrentIndex(0)

        main_layout.addLayout(switcher)

        main_layout.addWidget(QPlainTextEdit())

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


app = QApplication()
window = MainWindow()
window.show()
app.exec()

Now when I resize the window, the first row and the QPlainTextEdit() get equal vertical space in the resized window, which adds lots of blank space around the first row.

What are my options for using the QStackedLayout and always having the height of the first row to be the minimum height needed?

Upvotes: 0

Views: 14

Answers (0)

Related Questions