如何在PyQT中定位动态创建的小部件

时间:2017-12-11 04:54:27

标签: python pyqt qgis

我正在使用PyQGIS根据图层中的字段名称动态创建窗口小部件。小部件成功创建,但未正确定位。有没有办法使用Python将lineedit框定位到中间?下面是我动态创建窗口小部件的代码。

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)

enter image description here

我希望文本框的位置类似于

enter image description here

1 个答案:

答案 0 :(得分:0)

如果您想获得表单的相同顺序,则必须使用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)