I'm fiddling around with a piece of python code to use with form validation. It works fine, but the real problem is that QGIS refuses to acknowledge the changes I make in the script.

Here's what I did:

  • created the .py
  • referenced it in "layer Properties - Fields - Python init function"
  • it works as expected, popping up the message box saying CHEESE
  • now I open the py file in an editor, and change the message to MOUSE and save/close the editor
  • QGIS now still says CHEESE. I either have to restart QGIS or rename the file and reference it again, which is obviously a massive waste of time.

How can this be fixed?


from PyQt4.QtCore import *
from PyQt4.QtGui import *

nameField = None
myDialog = None
def formOpen(dialog,layerid,featureid):
    global myDialog
    myDialog = dialog
    global nameField
    nameField = dialog.findChild(QLineEdit,"Name")
    buttonBox = dialog.findChild(QDialogButtonBox,"buttonBox")

    # Disconnect the signal that QGIS has wired up for the dialog to the button box.

    # Wire up our own signals.

def validate():
  # Make sure that the name field isn't empty.
    msgBox = QMessageBox()
  • I always use the IDLE(Python) when it comes to editing and indenting code :) – Joseph Feb 5 '15 at 13:45
  • Confirmed in 2.6.1. – Germán Carrillo Feb 6 '15 at 0:09

By reading the QGIS source code I figured out how to solve this issue.

In your .py file write the following line (could be right below the imports):


This should do the trick, I tested it on QGIS v.2.6. Please tell me if you have any trouble.


There are two more possibilities to reload the code in the QGIS cache. You can use the QGIS plugin Plugin Reloader. There you can define your plugin and press the reload button everytime you need to reload the code.

Another possibility is to overwrite the closeEvent() event of PyQt if you use it for your interface.

    def closeEvent(self, event):

Everytime you close your plugin the plugin gets reloaded and the newest code is available (I'm not shure but I think when your plugin crashes the plugin won't be reloaded in this way).

  • 1
    This question is not about QGIS plugins Martin. – Germán Carrillo Feb 6 '15 at 15:25
  • 1
    Oh I'm sorry. But this works for plugins... – Martin Feb 6 '15 at 15:34
  • That is correct. – Germán Carrillo Feb 6 '15 at 15:38
  • This is helpful too and probably related enough. Thanks! – whycantIsigninwithmyusername Feb 9 '15 at 10:06

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.