python - TypeError:'dict_values'对象在从组合框调用层时不支持索引-pyqgis-qgisplugin
问题描述
当我从组合框中调用图层时,会抛出这样的错误。执行 Python 代码时发生错误:
TypeError: 'dict_values' 对象不支持索引 Traceback (最近一次调用最后): selectedLayer = layers[selectedLayerIndex1].layer() TypeError: 'dict_values' 对象不支持索引
Python 版本:3.7.0(v3.7.0:1bf9cc5093,2018 年 6 月 27 日,04:59:51)[MSC v.1914 64 位(AMD64)] QGIS 版本:3.4.15-Madeira Madeira,e83d02e274
代码 selectedLayerIndex1 = self.dlg.comboBox.currentIndex()
selectedLayer = 层[selectedLayerIndex1].layer()
qgis vrsion:QGIS 版本:3.4.15-Madeira Madeir
解决方案
如果要使用 Index,则必须先迭代图层并将其添加到图层列表中。只有在 thar 之后,您才能使用索引并从列表中选择图层(使用索引)
在 def 中使用此代码run(self)
:
layers_list = []
curLayers = QgsProject.instance().mapLayers().values()
for layer in curLayers:
layers_list.append(layer)
def chooseIndex():
sLayerIndex = self.dockwidget.comboBox.currentIndex()
selectedLayer = layers_list[sLayerIndex]
print(selectedLayer)
self.dockwidget.comboBox.currentIndexChanged.connect(chooseIndex)
推荐阅读
- c++ - 如何从另一个进程中解开全局钩子?
- regex - pyspark - python 控制台和带有正则表达式的 jupyter-notebook 中的不同行为
- sql - 将数组传递给 SQL 以返回相同的数组大小并返回顺序
- c++ - 如何在发布模型中忽略调试模式中使用的 std::array 初始化?
- node.js - 在 hasAndBelongsToMany 关系中的节点中执行初始加载
- onelogin - OneLogin OIDC 授权请求无效
- asp.net-mvc - MVC 模型变量未更新
- git - 工作树中的 diff 文件与远程跟踪分支中的文件
- typescript - 如何将 ImmutableJS 映射与 TypeScript 一起使用
- android - ViewModel、LiveData 和 Transformations.map