Python AttributeError:对象在QGIS插件中没有属性

时间:2017-10-20 08:25:47

标签: python qgis qpython

我正在尝试使用 QT Designer 创建 GUI 。我已将 .ui 设计器文件转换为 .py

这是我的代码:

from PyQt4.QtCore import QSettings, QTranslator, qVersion, QCoreApplication 
from PyQt4.QtGui import QAction, QIcon
from qgis.core import *
import resources
from delete_feature_dialog import DeleteFeatureDialog
import os.path

class DeleteFeature:

    def __init__(self, iface):
        # Save reference to the QGIS interface
        self.iface = iface
        # Declare instance attributes
        self.actions = []
        self.menu = self.tr(u'&DeleteFeature')
        self.toolbar = self.iface.addToolBar(u'DeleteFeature')
        self.toolbar.setObjectName(u'DeleteFeature')

    def add_action(
        self,
        icon_path,
        text,
        callback,
        enabled_flag=True,
        add_to_menu=True,
        add_to_toolbar=True,
        status_tip=None,
        whats_this=None,
        parent=None):

        # Create the dialog (after translation) and keep reference
        self.dlg = DeleteFeatureDialog()

        ....

        return action

    def initGui(self):  
        icon_path = ':/plugins/DeleteFeature/icon.png'
        self.add_action(
            icon_path,
            text=self.tr(u''),
            callback=self.run,
            parent=self.iface.mainWindow())

    def run(self):
        #this code will populate the combo box with all vector layer
    self.dlg.layerListCombo.clear()
    layers = self.iface.legendInterface().layers()
    layer_list = []
    for layer in layers:
            layerType = layer.type()
            if layerType == QgsMapLayer.VectorLayer:
                layer_list.append(layer.name())
    self.dlg.layerListCombo.addItems(layer_list)
        # show the dialog
        self.dlg.show()
        # Run the dialog event loop
        result = self.dlg.exec_()
        # See if OK was pressed
        if result:
            # Do something useful here - delete the line containing pass and
            # substitute with your code.
            selectedLayerIndex = self.dlg.layerlistcombo.currentIndex()
            selectedLayer = layers [selectedLayerIndex]
            .....

然后当我打开插件时,出现以下错误:

  

'DeleteFeatureDialog'objectObject没有属性   QGIS插件中的'layerlistcombo'

对此有任何建议。

1 个答案:

答案 0 :(得分:0)

似乎你写道:

selectedLayerIndex = self.dlg.layerlistcombo.currentIndex()

但你应该写:

selectedLayerIndex = self.dlg.layerListCombo.currentIndex()

就像你之前在代码中所做的那样(在写作时注意Camel Notation,而不仅仅是小写字母),这可能会导致No Attribute错误。