2017-10-20 211 views
0

我正在嘗試創建一個GUIQT設計器。我已將我的.ui設計器文件轉換爲.pyPython屬性錯誤:對象在QGIS插件中沒有屬性

這裏是我的代碼:

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 has no attribute 'layerlistcombo'in QGIS Plugin

這個任何建議。

+0

你在這個對話框中有一個組合框?你的對象名是layerlistcombo?可能不是。 –

回答

0

看來你寫道:

selectedLayerIndex = self.dlg.layerlistcombo.currentIndex() 

,但你應該寫:

selectedLayerIndex = self.dlg.layerListCombo.currentIndex() 

就像你在你的代碼以前一樣(寫作時,不只是小寫字母注意到駱駝符號),這可能會導致您得到的No Attribute錯誤。

相關問題