5

I am trying to connect to a QGIS 2.6 Python plugin function via a custom feature form button.

On my feature form, I'm attempting to do a few things :

  • enable the updateButton only if the checkbox (Reparee) is checked
  • disconnect the default validate and reconnect to my validate function
  • connect to a function in a python plugin with the click of a button
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis import utils

OriginInfofield = None
Repareefield = None
myDialog = None

myFeature = None
myLayer = None

def formOpen(dialog,layer,feature,featureid):
    global myDialog, outilInstance
    myDialog = dialog
    global Repareefield, OriginInfofield

    myFeature = feature
    myLayer = layer

    OriginInfofield = dialog.findChild(QLineEdit,"OriginInfo")
    Repareefield = dialog.findChild(QCheckBox, "Reparee")
    buttonBox = dialog.findChild(QDialogButtonBox,"buttonBox")
    updateButton = dialog.findChild(QDialogButtonBox, "updateButton")

    # Disconnect the signal that QGIS has wired up for the dialog to the button box.
    buttonBox.accepted.disconnect(myDialog.accept)

    # Wire up our own signals.
    buttonBox.accepted.connect(validate)
    buttonBox.rejected.connect(myDialog.reject)

    updateButton.clicked.connect(update)

    if Repareefield.isChecked():
        updateButton.setEnabled(False)

    if 'fuitesOutil' in utils.plugins:
        outilInstance = utils.plugins['fuitesOutil']

    QMessageBox.information(None, 'Info', 'Got to open form')

def update(featureid):
    global updateButton, Repareefield

    outilInstance.updateDeclaredTableWidget(featureid)

    QMessageBox.information(None, 'Info', 'Got to function')


def validate():
    global outilInstance
    global OriginInfofield

    if not len(OriginInfofield.text()) > 0 or OriginInfofield.text() == 'NULL':
        OriginInfofield.setStyleSheet("background-color: rgba(255, 107, 107, 150);")
        QMessageBox.warning(None,'Probleme de saisie',"Veuillez remplir 'Origin d'Information'")
    else:
        # Return the form as accepted to QGIS.
        myDialog.accept()

My plugin function looks like this (part of it -- the function works fine when triggered within the plugin itself) :

def updateDeclaredTableWidget(self, fid):
        QMessageBox.information(None, 'Info', 'Got to the plugin')
        self.decdlg.show()
        request = QgsFeatureRequest(fid)
        ...

The only QMessageBox I reach is the one in the validate function, but the condition does not work and the dialog is accepted anyways. I'm surprised the one in formOpen doesn't pop up. My python file is in OsGeo4W/apps/qgis/python. Previously, i kept it in .qgis2 but the structure seems different now that i've installed using OsGeo4.

UPDATE BELOW:

I'm looking into the updated QgsAttributeForm class reference and notice that accept and reject are now deprecated. Unless I'm overlooking some other error, I feel like I must be poorly disconnecting the accept button on my feature form. I've tried to replace them with resetValues and Save, but still have had no success.

  • Using myDialog.disconnectButtonBox() gives me an AttributeError: QgsAttributeForm object has no attribute disconnectButtonBox
  • I then tried disconnecting the way I had been doing, but with the new save function. This gives me a TypeError: disconnect() failed between 'accepted' and 'save'.

Any pointers on properly disconnecting the buttonBox and reconnecting to my functions?

My attempts so far:

def formOpen(dialog, layer, feature, featureid):
    myDialog = dialog

    #featureForm.disconnectButtonBox()
    buttonBox.accepted.disconnect(myDialog.save)
    buttonBox.accepted.connect(validate)
    buttonBox.rejected.connect(myDialog.resetValues)

def validate():
    if not myConditions == True:
        doStuff
    else:
        myDialog.save()

Still breezing through the condition.

My ultimate goal is to pre-fill some fields based on another intersecting layer's feature attributes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.