我正在使用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)
我希望文本框的位置类似于
答案 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)