python PyQt5 StackedWidget 內的 QPushbutton 怎麼自動換行

請問 PyQt5 StackedWidget 內的 QPushbutton 怎麼自動換行?
目前圖示如下 過多的代號會一直延伸到螢幕外
請問怎麼調整呢? 謝謝

import sys, os 
import pandas as pd 
from PyQt5.QtWidgets import (QWidget, QListWidget, QStackedWidget, QHBoxLayout, QListWidgetItem, 
QLabel, QApplication, QTabWidget, QVBoxLayout, QPushButton, QFormLayout) 
from PyQt5.QtCore import Qt, QSize

path1 = r'G:/我的雲端硬碟/Python/基本資訊'
path2 = r'G:/我的雲端硬碟/Python/盤中個股'
path6 = r'G:/我的雲端硬碟/Python/XQ分類'

# 股票資訊
stock_info = pd.read_csv(path1 + '/股票資訊.csv', index_col=[0], encoding='utf-8-sig')
stock_info.index = stock_info.index.astype(str)
code_list = stock_info.index.values.tolist()

# XQ 分類
dfg = {}
group_list = [group.replace('.csv', '') for group in os.listdir(path6)]
for i in group_list:
    dfg[i] = pd.read_csv(path6 + f"/{i}.csv", index_col=[0], encoding='utf-8-sig')
    dfg[i].index = dfg[i].index.astype(str)
    dfg[i] = pd.concat([dfg[i], stock_info], axis=1, join='inner')
print(dfg['UPS'])

class App(QWidget):
    def __init__(self):
        super().__init__()
        self.setGeometry(0, 0, 1920, 1080)
        self.setWindowTitle('策略 + 族群透視')

        # Create a top-level layout
        layout = QVBoxLayout()
        self.setLayout(layout)

        # Create tab 
        tabs = QTabWidget()
        tabs.addTab(self.strategy_tab(), '策略')
        tabs.addTab(self.group_tab(), '族群透視')
        layout.addWidget(tabs)
    
    def strategy_tab(self):
        self.stab = QWidget()
        self.leftlist = QListWidget(self.stab)
        self.leftlist.insertItem(0, 's1')
        self.leftlist.insertItem(1, 's2')
        self.leftlist.insertItem(2, 's3')
        self.stack1 = QWidget(self.stab)
        self.stack2 = QWidget(self.stab)
        self.stack3 = QWidget(self.stab)
        self.Stack = QStackedWidget(self.stab)
        self.Stack.addWidget(self.stack1)
        self.Stack.addWidget(self.stack2)
        self.Stack.addWidget(self.stack3)
        hbox = QHBoxLayout(self.stab)
        hbox.addWidget(self.leftlist)
        hbox.addWidget(self.Stack)
        self.setLayout(hbox)
        self.leftlist.currentRowChanged.connect(self.Stack.setCurrentIndex)

        self.stack1UI()

        return self.stab 

    def stack1UI(self):
        layout = QFormLayout(self.stab)
        code = {}
        for i in range(0, 15):
            code[i] = QHBoxLayout(self.stab)
        
        for i in range(15):
            for j in dfg[group_list[i]].index.values.tolist():
                code[i].addWidget(QPushButton(j))
            layout.addRow(QLabel(group_list[i]), code[i])
        self.stack1.setLayout(layout)

    def group_tab(self):
        group_tab = QWidget()
        return group_tab

if __name__ == '__main__':    
    app = QApplication(sys.argv)
    w = App()
    w.show()
    sys.exit(app.exec_())

稍微搜尋了一下,原因應該出在你使用了 QHBoxLayout 這個物件來擺放按鈕,這東西的原則就是往水平方向去擺放,所以沒有換行的概念,以下是我找到相關主題的討論(雖然是 C++):

如上文所建議,如需要按鈕在 Layout 中會根據長度換行,可以考慮使用 QGridLayout 進行手動調整或是去實作 FlowLayout 這樣的物件來實現你希望的換行機制。