PyQt6 – How can I expand a field in a QFormLayout

I would like to expand the combobox field in a form layout, but I just can’t seem to find the right combination of spells to achieve this. Specific documentation on practical layout management seems a bit thin on the ground – I don’t get a lot of mileage out of the doc.qt.io site.

This image shows what I am trying to achieve:

 extended combo box field

The label and combo box are contained in a QFormLayout, which in turn is contained within a QVBoxLayout.

What actually happens is that the label/combo occupies the left hand side, with the combo NOT extended to the size of the QVBoxLayout width.

Here is my code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import sys
from PyQt6.QtWidgets import QApplication, QComboBox, QDialog, QWidget, QVBoxLayout, QFormLayout, QTableView, QDialogButtonBox, QHeaderView, QLabel, QSizePolicy
from PyQt6.QtCore import Qt
class Dialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setObjectName("Dialog")
self.resize(500, 300)
self.verticalLayoutWidget = QWidget(self)
self.verticalLayoutWidget.resize(495, 295)
self.verticalLayout = QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(5, 5, 5, 5)
self.tableView = QTableView(parent=self.verticalLayoutWidget)
self.tableView.resizeColumnsToContents()
self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode(1))
self.tableView.SelectionMode(1)
self.verticalLayout.addWidget(self.tableView)
self.myCombo = QComboBox()
self.items = ['N/A', 'Amphibian','Bird','Fish','Invertebrate','Mammal', 'Reptile']
self.myCombo.addItems(self.items)
#self.myCombo.currentTextChanged.connect(self.model.customSlot)
self.myCombo.setSizePolicy(QSizePolicy.horizontalStretch())
self.formLayout = QFormLayout()
self.formLayout.setObjectName("formLayout")
self.formLayout.setContentsMargins(5, 5, 5, 5)
self.label = QLabel('Category', parent=self.verticalLayoutWidget)
self.label.setObjectName("categoriesLabel")
self.formLayout.setWidget(0, QFormLayout.ItemRole.LabelRole, self.label)
self.formLayout.setWidget(0, QFormLayout.ItemRole.FieldRole, self.myCombo)
self.formLayout.setFormAlignment(Qt.AlignmentFlag.AlignLeft) # the documentation specifies Qt.AlignLeft
self.formLayout.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.ExpandingFieldsGrow)
self.verticalLayout.addLayout(self.formLayout)
#self.myCombo.setModelColumn(1)
self.buttonBox = QDialogButtonBox(parent=self.verticalLayoutWidget)
self.buttonBox.setStandardButtons(QDialogButtonBox.StandardButton.Cancel|QDialogButtonBox.StandardButton.Ok)
self.buttonBox.setObjectName("buttonBox")
self.verticalLayout.addWidget(self.buttonBox)
self.buttonBox.rejected.connect(self.close)
#self.buttonBox.accepted.connect(self.model.showModel)
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = Dialog()
ui.show()
app.exec()
</code>
<code>import sys from PyQt6.QtWidgets import QApplication, QComboBox, QDialog, QWidget, QVBoxLayout, QFormLayout, QTableView, QDialogButtonBox, QHeaderView, QLabel, QSizePolicy from PyQt6.QtCore import Qt class Dialog(QDialog): def __init__(self, parent=None): super().__init__(parent) self.setObjectName("Dialog") self.resize(500, 300) self.verticalLayoutWidget = QWidget(self) self.verticalLayoutWidget.resize(495, 295) self.verticalLayout = QVBoxLayout(self.verticalLayoutWidget) self.verticalLayout.setContentsMargins(5, 5, 5, 5) self.tableView = QTableView(parent=self.verticalLayoutWidget) self.tableView.resizeColumnsToContents() self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode(1)) self.tableView.SelectionMode(1) self.verticalLayout.addWidget(self.tableView) self.myCombo = QComboBox() self.items = ['N/A', 'Amphibian','Bird','Fish','Invertebrate','Mammal', 'Reptile'] self.myCombo.addItems(self.items) #self.myCombo.currentTextChanged.connect(self.model.customSlot) self.myCombo.setSizePolicy(QSizePolicy.horizontalStretch()) self.formLayout = QFormLayout() self.formLayout.setObjectName("formLayout") self.formLayout.setContentsMargins(5, 5, 5, 5) self.label = QLabel('Category', parent=self.verticalLayoutWidget) self.label.setObjectName("categoriesLabel") self.formLayout.setWidget(0, QFormLayout.ItemRole.LabelRole, self.label) self.formLayout.setWidget(0, QFormLayout.ItemRole.FieldRole, self.myCombo) self.formLayout.setFormAlignment(Qt.AlignmentFlag.AlignLeft) # the documentation specifies Qt.AlignLeft self.formLayout.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.ExpandingFieldsGrow) self.verticalLayout.addLayout(self.formLayout) #self.myCombo.setModelColumn(1) self.buttonBox = QDialogButtonBox(parent=self.verticalLayoutWidget) self.buttonBox.setStandardButtons(QDialogButtonBox.StandardButton.Cancel|QDialogButtonBox.StandardButton.Ok) self.buttonBox.setObjectName("buttonBox") self.verticalLayout.addWidget(self.buttonBox) self.buttonBox.rejected.connect(self.close) #self.buttonBox.accepted.connect(self.model.showModel) if __name__ == '__main__': app = QApplication(sys.argv) ui = Dialog() ui.show() app.exec() </code>
import sys
from PyQt6.QtWidgets import  QApplication, QComboBox, QDialog, QWidget, QVBoxLayout,  QFormLayout, QTableView,  QDialogButtonBox, QHeaderView, QLabel, QSizePolicy
from PyQt6.QtCore import Qt

class Dialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setObjectName("Dialog")
        self.resize(500, 300)
        
        self.verticalLayoutWidget = QWidget(self)
        self.verticalLayoutWidget.resize(495, 295)
        self.verticalLayout = QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(5, 5, 5, 5)
        
        self.tableView = QTableView(parent=self.verticalLayoutWidget)
        self.tableView.resizeColumnsToContents()
        self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode(1))
        self.tableView.SelectionMode(1)  
        self.verticalLayout.addWidget(self.tableView)

        self.myCombo = QComboBox()
        self.items = ['N/A', 'Amphibian','Bird','Fish','Invertebrate','Mammal', 'Reptile']
        self.myCombo.addItems(self.items)
        #self.myCombo.currentTextChanged.connect(self.model.customSlot)
        self.myCombo.setSizePolicy(QSizePolicy.horizontalStretch())

        self.formLayout = QFormLayout()
        self.formLayout.setObjectName("formLayout")
        self.formLayout.setContentsMargins(5, 5, 5, 5)
        self.label = QLabel('Category', parent=self.verticalLayoutWidget)
        self.label.setObjectName("categoriesLabel")
        self.formLayout.setWidget(0, QFormLayout.ItemRole.LabelRole, self.label)
        self.formLayout.setWidget(0, QFormLayout.ItemRole.FieldRole, self.myCombo)
        self.formLayout.setFormAlignment(Qt.AlignmentFlag.AlignLeft)  # the documentation specifies Qt.AlignLeft
        self.formLayout.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.ExpandingFieldsGrow)

        self.verticalLayout.addLayout(self.formLayout)
        #self.myCombo.setModelColumn(1)
                
        self.buttonBox = QDialogButtonBox(parent=self.verticalLayoutWidget)
        self.buttonBox.setStandardButtons(QDialogButtonBox.StandardButton.Cancel|QDialogButtonBox.StandardButton.Ok)
        self.buttonBox.setObjectName("buttonBox")
        self.verticalLayout.addWidget(self.buttonBox)
        self.buttonBox.rejected.connect(self.close)
        #self.buttonBox.accepted.connect(self.model.showModel)
        

if __name__ == '__main__':        
    app = QApplication(sys.argv)
    ui = Dialog()
    ui.show()
    app.exec()

I was hoping that the following lines I used might help :

self.myCombo.setSizePolicy(QSizePolicy.horizontalStretch())
self.formLayout.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.ExpandingFieldsGrow)

Does anyone have any ideas on how I might achieve this

I have tried dozens of combinations of setSizePolicy on the combo box and on the form layout but invariably I generate errors

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật