Adam Sirrelle
Adam Sirrelle

Reputation: 397

How do you use layout.adoptLayout(layout) in PySide?

I have

widget = QtWidgets.Widgets()
layout = QtWidgets.QVBoxLayout(parent=widget)

Now in certain situations I want to change the QVBoxLayout to a QHBoxLayout, and I want to do this after creation if possible. That is, without simply creating a QHBoxLayout from the get go.

I have tried using

layout.adoptLayout(QtWidgets.QHBoxLayout())

But this simply returns True, and doesn't change the actual layout.

Upvotes: 0

Views: 331

Answers (1)

eyllanesc
eyllanesc

Reputation: 244282

Note: adoptLayout/( is an internal method that should not be exposed so I think it is a bug since it is also not documented.

To implement the change of direction then you can use the QBoxLayout class:

from PySide2 import QtCore, QtWidgets


if __name__ == "__main__":
    app = QtWidgets.QApplication()

    widget = QtWidgets.QWidget()

    lay = QtWidgets.QBoxLayout(QtWidgets.QBoxLayout.TopToBottom, widget)

    for i in range(4):
        button = QtWidgets.QPushButton(f"button {i}")
        lay.addWidget(button)

    widget.show()

    def on_timeout():
        direction = (
            QtWidgets.QBoxLayout.TopToBottom
            if lay.direction() == QtWidgets.QBoxLayout.LeftToRight
            else QtWidgets.QBoxLayout.LeftToRight
        )
        lay.setDirection(direction)

    timer = QtCore.QTimer(interval=1000, timeout=on_timeout)
    timer.start()

    app.exec_()

Upvotes: 1

Related Questions