Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am following the tutorial found here for developing a simple QGIS plugin within QGIS 2.0.1 installed on Windows 7. However, as I have began to try developing a link between the GUI and each the mouse click of the map canvas, the main plugin code does not seem to recognize the fields in the GUI.

To illustrate with the example in the tutorial. Within in my plugin.py code I have the function:

class vector_selectbypoint:
     def __init__(self, iface):
         # Save reference to the QGIS interface
         self.iface = iface
         # refernce to map canvas
         self.canvas = self.iface.mapCanvas()
         # out click tool will emit a QgsPoint on every click
         self.clickTool = QgsMapToolEmitPoint(self.canvas)
         # create our GUI dialog
         self.dlg = vector_selectbypointDialog()    

    def handleMouseDown(self, point, button):
         self.dlg.clearTextBrowser()
         self.dlg.setTextBrowser( str(point.x()) + " , " +str(point.y()) )

that simply writes the lat and long into a TextBrowser with the GUI by calling the clearTextBrowser and setTextBrowser functions in a seperate pluginDialog.py script where the following code is ran:

class vector_selectbypointDialog(QtGui.QDialog, Ui_vector_selectbypoint):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        self.ui = Ui_vector_selectbypoint()
        self.setupUi(self)

    def setTextBrowser(self, output):
        self.ui.txtFeedback.setText(output)

    def clearTextBrowser(self):
        self.ui.txtFeedback.clear()

which interacts with the ui_plugin_ui.py where txtFeedback is the TextBrowser in the GUI. Below is a snippet of the ui_plugin_ui.py file:

class Ui_vector_selectbypoint(object):
    def setupUi(self, vector_selectbypoint):
        vector_selectbypoint.setObjectName(_fromUtf8("vector_selectbypoint"))
        vector_selectbypoint.resize(400, 300)
        self.buttonBox = QtGui.QDialogButtonBox(vector_selectbypoint)
        self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
        self.chkActivate = QtGui.QCheckBox(vector_selectbypoint)
        self.chkActivate.setGeometry(QtCore.QRect(20, 250, 81, 20))
        self.chkActivate.setObjectName(_fromUtf8("chkActivate"))
        self.txtFeedback = QtGui.QTextBrowser(vector_selectbypoint)
        self.txtFeedback.setGeometry(QtCore.QRect(80, 20, 221, 151))
        self.txtFeedback.setObjectName(_fromUtf8("txtFeedback"))

The issue is that when I run the plugin in QGIS and click on the map canvas the following error is thrown:

AttributeError: 'ui_plugin_ui' object has no attribute 'txtFeedback'

I have verified that there are no syntax issues but the issue persists. If anyone has any insight I would eternally grateful!

share|improve this question
    
Please provide the code for your class definition and your init method –  gsherman Apr 16 at 14:01
    
see the above the edits and let me know if I can clarify further. Thank you! –  user2593375 Apr 16 at 16:19
add comment

1 Answer 1

up vote 0 down vote accepted

Since your vector_selectbypoint class inherits from the UI class, get rid of:

self.ui = Ui_vector_selectbypoint()

You can then refer to the text browser using:

self.txtFeedback

The way your code is now, you are setting self.ui to your UI but never setting it up. That would require using:

self.ui.setupUI(self)

rather than

self.setupUI(self)
share|improve this answer
    
That did the trick, thank you! I suppose that is the danger when you use out-of-the-box code, and it doesn't help matters that I am rather dense when it comes to GUI development. –  user2593375 Apr 17 at 13:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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