PyQt5 FormLayoutの自動伸縮設定

# 概要
PyQt5でフォーム画面を作成する際に解決まで時間がかかったことの記載
## やりたいこと
入力欄が画面いっぱいに伸びているフォームの作成
<img width="300" alt="form_layout_True.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/779817/45f78d4c-a736-da41-8a85-0858130df541.png">
## 困ったこと
Widgetによって幅がバラバラになってしまう。`setSizePolicy`でwidgetの伸縮設定を変更しても期待した様にならなかった。
<img width="300" alt="form_layout_False.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/779817/e680c27a-555a-8a5a-7090-6c431868e73d.png">

SizePolicyの参考記事
[QSizePolicy Class | Qt Widgets 5.15.2](https://doc.qt.io/qt-5/qsizepolicy.html#Policy-enum)
[ウィジェットの自動伸縮の設定 - yu00’s blog](http://yu00.hatenablog.com/entry/2015/09/06/223025)

## 解決策
`QFormLayout.setFieldGrowthPolicy`でフォームレイアウトの伸縮設定を変更
[QFormLayout Class | Qt Widgets 5.15.2](https://doc.qt.io/qt-5/qformlayout.html#FieldGrowthPolicy-enum)

# 環境
- Mac OS
- Python 3.8.5
- PyQt5 5.12.2
- PyQt5-sip 12.8.1

# 詳細
pip install

```terminal
pip install PyQt5
pip install PyQt5-sip
```

```python
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Form Layout")
        self.setGeometry(350, 150, 400, 400)
        self.ui()

        self.show()

    def ui(self) -> None:
        """UI作成"""
        self.main_design()
        self.layouts()

    def main_design(self) -> None:
        """widget作成"""
        # main_layoutで使用
        self.title = QLabel("Add Person")
        self.title.setStyleSheet("font-size: 24pt;")

        # form_layoutで使用
        self.name_lable = QLabel("Name :")
        self.name_entry = QLineEdit()
        self.surname_lable = QLabel("Surname :")
        self.surname_entry = QLineEdit()
        self.phone_lable = QLabel("Phone :")
        self.phone_entry = QLineEdit()
        self.email_lable = QLabel("Email :")
        self.email_entry = QLineEdit()
        self.img_label = QLabel("Picture :")
        self.img_button = QPushButton("Browse")
        self.address_lable = QLabel("Address :")
        self.address_entry = QTextEdit()
        self.add_button = QPushButton("Add")

    def layouts(self) -> None:
        """レイアウトの作成 & 編集"""
        # 使用するレイアウトを作成
        self.main_layout = QVBoxLayout()
        self.form_layout = QFormLayout()
        self.form_layout.setFieldGrowthPolicy(QFormLayout.AllNonFixedFieldsGrow)

        # main_layoutにwidgetとform_layoutを追加
        self.main_layout.addWidget(self.title, alignment=Qt.AlignHCenter)
        self.main_layout.addLayout(self.form_layout)

        # form_layoutにwidget追加
        self.form_layout.addRow(self.name_lable, self.name_entry)
        self.form_layout.addRow(self.surname_lable, self.surname_entry)
        self.form_layout.addRow(self.phone_lable, self.phone_entry)
        self.form_layout.addRow(self.email_lable, self.email_entry)
        self.form_layout.addRow(self.img_label, self.img_button)
        self.form_layout.addRow(self.address_lable, self.address_entry)
        self.form_layout.addRow("", self.add_button)

        # 画面にmain_layoutを適応
        self.setLayout(self.main_layout)


def main():
    App = QApplication(sys.argv)
    window = Window()
    sys.exit(App.exec_())


if __name__ == "__main__":
    main()

```
重要なのは`def layouts`内の`self.form_layout.setFieldGrowthPolicy(QFormLayout.AllNonFixedFieldsGrow)`

> [QFormLayout Class | Qt Widgets 5.15.2](https://doc.qt.io/qt-5/qformlayout.html#FieldGrowthPolicy-enum)

>| Constant                             | Value | Description                                                  |
| ------------------------------------ | ----- | ------------------------------------------------------------ |
| `QFormLayout::FieldsStayAtSizeHint`  | `0`   | The fields never grow beyond their [effective size hint](https://doc.qt.io/qt-5/qwidgetitem.html#sizeHint). This is the default for QMacStyle. |
| `QFormLayout::ExpandingFieldsGrow`   | `1`   | Fields with an horizontal [size policy](https://doc.qt.io/qt-5/qsizepolicy.html) of [Expanding](https://doc.qt.io/qt-5/qsizepolicy.html#Policy-enum) or [MinimumExpanding](https://doc.qt.io/qt-5/qsizepolicy.html#Policy-enum) will grow to fill the available space. The other fields will not grow beyond their effective size hint. This is the default policy for Plastique. |
| `QFormLayout::AllNonFixedFieldsGrow` | `2`   | All fields with a size policy that allows them to grow will grow to fill the available space. This is the default policy for most styles. |

大体の訳

- `FieldsStayAtSizeHint`：伸縮無し, Macの時はこれがデフォルト
- `ExpandingFieldsGrow`：水平方向のSizePolicyが`Expanding`か`MinimumExpanding`のwidgetは伸縮する
- `AllNonFixedFieldsGrow`：伸縮が許可されている全てのwidgetは伸縮する

valueで設定してもOK

```python
self.form_layout.setFieldGrowthPolicy(2)
```

# 参考記事
SizePolicy
[QSizePolicy Class | Qt Widgets 5.15.2](https://doc.qt.io/qt-5/qsizepolicy.html#Policy-enum)
[ウィジェットの自動伸縮の設定 - yu00’s blog](http://yu00.hatenablog.com/entry/2015/09/06/223025)

QFormLayout.setFieldGrowthPolicy
[QFormLayout Class | Qt Widgets 5.15.2](https://doc.qt.io/qt-5/qformlayout.html#FieldGrowthPolicy-enum)