I am new to Plugin Builder and creating my first plugin called NearestNeighbour. The below is GUI of my plugin made using Qt Creator

I read in this link that it is important to rename Dialog Box, Toolbox because it will be used in our code. Label are kept as it is with object name label and label_2 respectively. The LineEdit objectName for Upload Node Layer is called lineEdit. On a similar basis the second LineEdit objectName for Upload Building Layer is called lineEdit_2. In a similar way the Push Buttonfor Upload Node Layer is called pushButton and the second Tool Button for Upload Building Layer is called pushButton_2. With reference to the link mentioned above, I have done the Following change in my Nearest_Neighbou.py

1) Add the below line where all imports are done

 from qgis.core import QgsProject

2) Add QFileDialog to QtWidget to the list of imports at the top of the file.

from PyQt5.QtWidgets import QAction, QFileDialog

3) Add a new method called Upload_Point_Layer with the following code just above def(run)self:

def Upload_Point_Layer(self):
  filename, _filter = QFileDialog.getSaveFileName(
  self.dlg, "Select   output file ","", '*.shp')

4) In the run method, added the following line in the block where the dialog is initialized.(This code will connect the Upload_Point_Layer method to the clicked signal of the push button widget)


I want to know what I am doing wrong or are there any other files that also need to be edited ? I am using QGIS3.6 on 32 Bit Windows operating System and using Pycharm as Python IDE

But after saving the changes, when I tried to reload the plugin I have got the below error

Couldn't load plugin 'Nearest_Neighbour' due to an error when calling its classFactory() method 

AttributeError: 'NearestNeighbour' object has no attribute 'dlg' 
Traceback (most recent call last):
File "C:/OSGeo4W/apps/qgis/./python\qgis\utils.py", line 335, in startPlugin
plugins[packageName] = package.classFactory(iface)
File "C:/Users/Prachi/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\Nearest_Neighbour\__init__.py", line 36, in classFactory
return NearestNeighbour(iface)
File "C:/Users/Prachi/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\Nearest_Neighbour\Nearest_Neighbour.py", line 73, in __init__
AttributeError: 'NearestNeighbour' object has no attribute 'dlg'
  • QFileDialog.getSaveFileName is not correct, and where is self.dlg.lineEdit.clear() ? – Fran Raga May 19 at 11:37
  • why QFileDialog.getSaveFileName is not correct. It now works for me. till now I haven't added clear() `thing ? – ps1 May 19 at 12:14

If you look at step 23 in the tutorial you'll see some lines in the run method:

if self.first_start == True:
   self.first_start == False
   self.dlg = SaveAttributeDialog()

You need to find those lines and make sure they are called before you try to use the dialog anywhere. Also check that the name matches the name of your dialog (probably something like NearestNeighbourDialog).

  • Yes I think there was a identation error but it works now . Thank you :) I have written some processing for local use and it works very well in qgis python. It need to just copy and paste code in pass section of def(run)self: method right ? – ps1 May 19 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.