fereria
8/4/2021 - 6:08 PM

sampel layout

# -*- coding: utf-8 -*-
import sys

from PySide2.QtWidgets import (QApplication,
                               QDialog,
                               QPushButton,
                               QSizePolicy,
                               QVBoxLayout,
                               QHBoxLayout,
                               QFormLayout,
                               QGridLayout,
                               QSpacerItem)


class FormUI(QDialog):

    def __init__(self, parent=None):
        super().__init__(parent)

        layout = QGridLayout()
        self.setLayout(layout)

        btn = QPushButton("SampleA")
        layout.addWidget(btn, 0, 0)
        btn = QPushButton("SampleB")
        layout.addWidget(btn, 0, 1)
        btn = QPushButton("SampleC")
        layout.addWidget(btn, 1, 0)
        btn = QPushButton("SampleD")
        layout.addWidget(btn, 1, 1)

        btn = QPushButton("SampleE")
        layout.addWidget(btn, 2, 0, 1, 2)

        btn = QPushButton("SampleF")
        layout.addWidget(btn, 3, 0, 2, 1)
        btn.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Preferred)

        btn = QPushButton("SampleG")
        layout.addWidget(btn, 3, 1)
        btn = QPushButton("SampleH")
        layout.addWidget(btn, 4, 1)


class SampleUI(QDialog):

    def __init__(self, parent=None):
        super().__init__(parent)

        layoutType = "V"  # H or V

        if layoutType == "H":
            layout = QHBoxLayout(self)
            spacerItem = QSpacerItem(20, 40, QSizePolicy.Expanding, QSizePolicy.Minimum)
        else:
            layout = QVBoxLayout(self)
            spacerItem = QSpacerItem(0, 200, QSizePolicy.Minimum, QSizePolicy.Fixed)

        for i in range(5):
            btn = QPushButton(f"Sample {i}")
            layout.addWidget(btn)

        layout.addItem(spacerItem)
        self.setLayout(layout)

        # レイアウトに含まれるWidgetを取得する
        for i in range(layout.count()):
            print(layout.itemAt(i).widget())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    QApplication.setFallbackSessionManagementEnabled(True)
    # a = SampleUI()
    a = FormUI()
    a.show()
    sys.exit(app.exec_())