简体   繁体   中英

How to position the dynamically created widgets in PyQT

I am creating widgets dynamically based on the number of field names in a layer using PyQGIS. The widgets gets created successfully, but its not positioned properly. Is there a way to position the lineedit box to the middle using Python?Below is my code for creating widgets dynamically.

selectedLayerIndex = self.dlg.comboBox.currentIndex()
selectedLayer = layers[selectedLayerIndex]
fields = selectedLayer.pendingFields()
fieldnames = [field.name() for field in fields]
for i,field in enumerate(fieldnames):
    self.labels = QLabel()
    self.linedit = QLineEdit()
    self.labels.setText(field)
    self.linedit.setFixedWidth(100)
    self.dlg.verticalLayout.addWidget(self.labels)
    self.dlg.verticalLayout.addWidget(self.linedit)

在此处输入图片说明

I want the textboxes to be placed something like this

在此处输入图片说明

If you want to obtain the same order of a form you must use QFormLayout .

selectedLayerIndex = self.dlg.comboBox.currentIndex()
selectedLayer = layers[selectedLayerIndex]
fields = selectedLayer.pendingFields()
fieldnames = [field.name() for field in fields]
flay = QFormLayout()
for field in fieldnames:
    le = QLineEdit()
    le.setFixedWidth(100)
    flay.addRow(field, le)
self.dlg.verticalLayout.addLayout(flay)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM