我正在创建一个QGIS插件,用户可以在其中根据特定策略将列重命名为指定名称。 在图像中您可以看到,用户在 QGIS 中选择一个图层,该图层的字段名称将填充 mFieldCombobox(“实际名称”),并在第二个组合框中为用户提供在插件中定义的名称列表(以及取决于所选政策)。
但是,图层中应重命名的列数取决于用户的需求,因此我希望他/她能够添加尽可能多的组合框(“实际名称”和“重命名为”)需要。为此,我添加了“+”按钮,按下该按钮后,应该生成一个新的“实际名称”-mFieldCombobox 和一个新的“重命名为”-combobox。
这可能吗?如果可能的话,如何实现? 非常欢迎任何帮助!
到目前为止我的代码:
def run(self):
"""Run method that performs all the real work"""
# Create the dialog with elements (after translation) and keep reference
# Only create GUI ONCE in callback, so that it will only load when the plugin is started
if self.first_start == True:
self.first_start = False
self.dlg = ReformatToLKNamesDialog()
# Populate Richtlinien Combobox mit Richtliniennamen
self.populateRichtinie()
# Get value from Richtlinie
self.selected_RL = self.dlg.Richtlinie.currentText()
self.dlg.Richtlinie.currentTextChanged.connect(self.populateRLS19)
# Update the combobox with the field names
self.dlg.LayerName.layerChanged.connect(self.updateFields)
# show the dialog
self.dlg.show()
# Run the dialog event loop
result = self.dlg.exec_()
print(type(result))
# See if OK was pressed
if result:
# Write here your code which should run when you hit the button 'OK'
pass
def printRL(self):
self.selected_RL = self.dlg.Richtlinie.currentText()
if self.selected_RL:
print(self.dlg.Richtlinie.currentText())
def updateFields(self):
self.dlg.mFieldComboBox.clear()
selectedLayer = self.dlg.LayerName.currentLayer()
if selectedLayer:
self.dlg.mFieldComboBox.setLayer(selectedLayer)
def populateRichtinie(self):
richtlinien = ['RLS-19', 'BUB', 'RLS-90'] # may be added: 'All'
print(richtlinien)
self.dlg.Richtlinie.addItems([item for item in richtlinien])
def populateRLS19(self):
self.dlg.LKName.clear()
selectedRichtlinie = self.dlg.Richtlinie.currentText()
rlkurz = {'st':'sr', 'b':'ra', 'ls':'la'}
selctedRL_kurzel = rlkurz[selectedRichtlinie]
print(selctedRL_kurzel)
rl_kuerzel = [
'list', 'of', 'names']
rl_f = [selctedRL_kurzel+kuerzel for kuerzel in rl_kuerzel]
self.dlg.LKName.addItems([item for item in rl_f])
这段代码应该有帮助:
class FieldChanger:
def __init__(self):
self.cmbPairList = [] # make cmb accesible as an attribute e.g. for reading values
self.makeUI()
self.window.exec()
def makeUI(self):
"""example UI"""
self.window = QDialog()
self.mainLayout = QVBoxLayout()
# press this button to add comboboxes
self.btn_add = QPushButton('Add Combobox Pair')
self.mainLayout.addWidget(self.btn_add)
self.btn_add.clicked.connect(self.addCMBPair)
# press this button to read data from comboboxes in dialog
self.btn_finished = QPushButton('Done')
self.mainLayout.addWidget(self.btn_finished)
self.btn_finished.clicked.connect(self.readSelection)
self.window.setLayout(self.mainLayout)
def addCMBPair(self):
# whenever the add button is clicked ...
subLayout = QHBoxLayout() # ... create a horizontal layout
cmb_left = QComboBox() # combobox for current field name
cmb_left.addItem('aFieldNameFromALayer')
cmb_left.addItem('anotherFieldNameFromALayer')
subLayout.addWidget(cmb_left) # add the combobox to the layout
cmb_right = QComboBox() # for the new field name
cmb_right.addItem('aNewName')
cmb_right.addItem('bNewName')
subLayout.addWidget(cmb_right)
self.cmbPairList.append([cmb_left, cmb_right]) # add the boxes to the list (or a dictionary or whatever)
self.mainLayout.addLayout(subLayout) # and add the sublayout (which contains the comboboxes) to the main layout
def readSelection(self):
# example for how to get the values from the comboboxes
for old, new in self.cmbPairList:
print(old.currentText(), new.currentText())
a = FieldChanger()