2

I have a custom form for editting feature attributes. However, I can't seem to correctly disconnect and reconnect certain bindings. The init code is below and should work for any simple QT widget with buttons.

It doesn't throw an error. The OK button is disconnected from the original connect that QGIS assigns. But it does not reconnect to the validate function.

Can anyone point me in the direction of the mistake here?

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

# QGIS passes in the dialog, he layer_id and the feature_id
def form_open(myDialog,layer_id,feature_id):
    # QGIS passes in the dialog, he layer_id and the feature_id
    # I send all this to be handled by its own class. So I can use 
    #'self' and avoid having to use globals...
    return MyClass(myDialog,layer_id,feature_id)

class MyClass(QDialog):
    def __init__(self,myDialog,layer_id,feature_id):
        self.ui = myDialog
        self.buttonBox = self.ui.findChild(QDialogButtonBox,"buttonBox")

        # The problem is here...
        self.buttonBox.accepted.disconnect(self.ui.accept)
        self.buttonBox.accepted.connect(self.validate)

    def validate(self):
        msgBox = QMessageBox()
        msgBox.setText("I validate here...")
        msgBox.exec_()
4

Are you sure MyClass is staying alive while the form is opened. In the C++ we don't store the return value of the call to the init function. So MyClass is getting collected as soon as your function returns because there is no reference to it.

Try something like this:

form = None

def form_open():
    global form
    form = MyClass(....)

It's a bit messy I know. I'll have a look at updating the core codebase to store the return value for the life of the opened form.

You are missing QDialog.__init__(self) in your class

class MyClass(QDialog):
    def __init__(self,myDialog,layer_id,feature_id):
        QDialog.__init__(self)

without doing that you are not creating a QWidget just a normal Python object. Qt can't connect to validate if your class doesn't inherit from QObject


As of QGIS 2.0 the return style will now work to keep the object instance alive.

def form_open(myDialog,layer_id,feature_id):
    return MyClass(myDialog,layer_id,feature_id)
|improve this answer|||||

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.