3

I am in the process of developing a plugin for QGIS using Python. Note that this is my first plugin, so the query could be quite basic...

I have used this Digital Geography link to create the basic plugin, and (quite surprisingly found it easy) created the plugin files.

I am stuck at a very basic concept. Previously, while using Python IDLE, using

QtCore.QObject.connect(self.Input, QtCore.SIGNAL("clicked()"), self.OpenBrowse)

to signal a click and writing a function OpenBrowse

 def OpenBrowse(self):
 self.Input_TB.setText(filename1)

I was able to open the Windows File Browse window on click and set the File location to an adjoining text box.

How do I do this while developing the plugin in the Python file that is created by Plugin Builder.? I am very confused about where should the code be written? Because if I place the 'clicked()' actions, etc. in the Python file generated from the UI file, it is not recognizing the variables as they are not global (obviously).

I am using QGIS 2.4, on a Windows 32-bit system.

underdark
84.9k22 gold badges237 silver badges419 bronze badges
asked Sep 29, 2014 at 7:28

2 Answers 2

3

Let us imagine your UI file name is MyDialogFile.py. Then,

from PyQt4.QtGui import QFileDialog
from MyDialogFile import MyDialog 
def InitGui(self):
 #other stuff....
 self.dialog = MyDialog() 
def OpenBrowse(self): 
 filename1 = QFileDialog.getOpenFileName()
 self.dialog.Input_TB.setText(filename1)
answered Sep 29, 2014 at 8:09
3
  • This works in a normal IDLE program. The difference with that and PyQGIS is that the UI commands are in a different file and the actual tool execution program is in a different file. This does not work in this setup of PyQGIS. No errors, but just doesn't do what's needed.. Commented Sep 29, 2014 at 9:02
  • @Akhil - mmm...i think you should import the UI file into the tool execution .py file..something like, from MyDialogFile import MyDialog Commented Sep 29, 2014 at 9:18
  • @Akhil - Updated my answer. you could look at some of the existing qgis plugins, like QuickWkt Plugin for example to see how it connects to UI, github.com/elpaso/quickwkt Commented Sep 29, 2014 at 9:20
2

Modifications need to be made in 3 different files in the folder generated by Plugin Builder...

QtCore.QObject.connect(self.Input, QtCore.SIGNAL("clicked()"), self.OpenBrowse)

To be added in the UI File under the main Class...

def OpenBrowse(self): 
 filename1 = QFileDialog.getOpenFileName()
 self.dialog.Input_TB.setText(filename1)

Function to be defined in the 'plugin_class_name'dialog.py file

and

self.dialog = Ui_'Class_Name'()

in the main plugin python script under the initGui() function...

answered Oct 1, 2014 at 9:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.