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.

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)
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..
    – Akhil
    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
    – vinayan
    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
    – vinayan
    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...

Your Answer

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

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