我的QGIS 2.0,2.2插件已经出现了PyQt组合框问题。我最近创建了另一个虽然类似的线程(QCombo box to set layer (for Python QGIS plugin)),但意识到我的问题肯定超出了答案的范围。我在更简单的情况下测试了类似的方法,但仍然存在问题。我想用QGIS图例中的图层动态填充此组合框,然后根据项目选择设置活动图层。我的问题是,如果重新加载插件,组合框只会填充。
def initGui(self):
self.projCombo = QComboBox(self.iface.mainWindow())
self.projCombo.clear()
for layer in self.iface.legendInterface().layers():
if layer.type() == QgsMapLayer.VectorLayer:
self.projCombo.addItem(layer.name(), layer.id())
self.projComboAction = self.toolbar.addWidget(self.projCombo)
QObject.connect(self.projCombo,SIGNAL("currentIndexChanged(int)"),self.myActiveLayer)
def myActiveLayer(self, index):
registry = QgsMapLayerRegistry.instance()
activeID = str(self.projCombo.itemData(index))
self.activeLayer = registry.mapLayer(activeID)
self.iface.setActiveLayer(activeLayer)