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:
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:
from PyQt6. QtWidgets import QApplication, QComboBox, QDialog, QWidget, QVBoxLayout, QFormLayout, QTableView, QDialogButtonBox, QHeaderView, QLabel, QSizePolicy
from PyQt6. QtCore import Qt
def __init__ ( self, parent=None ) :
self. setObjectName ( "Dialog" )
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 )
<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