Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Forms with Python logic don't work anymore as the dialog closes even though the .accept() function is not run #19123

Closed
qgib opened this issue Jun 26, 2014 · 3 comments
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Forms

Comments

@qgib
Copy link
Contributor

qgib commented Jun 26, 2014

Author Name: Tudor Bărăscu (@tudorbarascu)
Original Redmine Issue: 10739
Affected QGIS version: master
Redmine category:forms


One wonderful QGIS feature is the ability to add Python logic to forms.
Thank you Nathan and Tim for showing how.

The dialog closes at the push of the ok button even though the validate method is not accessing the dialog.accept().

I have made a small project that illustrates the new behavior:

https://www.dropbox.com/s/itdpp097sm5bhkd/form_problem.zip

Is this a bug or there is something new that I haven’t considered.

#!/usr/bin/env python

  1. -- coding: utf-8 --
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *

myDialog = None
nameField = None

def formTest(dialog, layerid, featureid):

global myDialog
myDialog = dialog
global nameField
nameField = dialog.findChild(QLineEdit, "name")

buttonBox = dialog.findChild(QDialogButtonBox, "buttonBox")
buttonBox.accepted.disconnect(myDialog.accept)

buttonBox.accepted.connect(validate)
buttonBox.rejected.connect(myDialog.reject)

def validate():
if not len(nameField.text()) >= 4:
msgBox = QMessageBox()
msgBox.setText("Name field must have at least 4 letters.")
msgBox.exec_()
else:
myDialog.accept()



Related issue(s): #17564 (duplicates)
Redmine related issue(s): 8885


@qgib
Copy link
Contributor Author

qgib commented Jun 26, 2014

Author Name: Matthias Kuhn (@m-kuhn)


See also #17564 and "this gis.se":http://gis.stackexchange.com/questions/74640/buttons-ok-cancel-disappear-in-apri-modulo-from-attribute-table-in-qgis-2-0 entry.

On a technical level, the problem is that the previous solution was really closely integrated into the QDialog system by rewiring very specific signals. This is an easy and straightforward way for when the form is shown on a dialog.

However, this way is not flexible to be used in other contexts like when the form is shown on the attribute table. Therefore a new and improved system will be implemented, but that needs to wait for "PR/1370":#1370 to be merged.

I tried to get the old behavior back by pretending to still support the signals. On current master (thus 2.4 release), the message in your project shows up when closing the dialog and the value is dismissed, but it won't prevent the dialog from closing. It also will not work on the form view in attribute table.

On a related note: constraints introduced by this kind of form logic have never been considered on the attribute table. A constraints (domains) system has been discussed on a previous hackfest. That would allow to proper checking at appropriate times throughout the code.

Currently there is another possibility which I think might even be better suited in terms of usability. The @Form@ variable passed to your python init code will emit a signal @attributeChanged@ whenever the user types something. So you can use it for checks and show warnings before the user clicks "Ok". This also works on the form view on the attribute table. Modifying your example a bit it then looks like this:

def formTest(form, layerid, featureid):
    ....
    form.attributeChanged.connect(validate)

def validate( attr, value ):
    label=myDialog.findChild(QLabel, "label")

    if attr == "name":
        if len( value ) < 4:
            label.setText( "Name is too short. Must have at least 4 characters." )
            nameField.setStyleSheet( "background-color: '#ff9999'")
        else:
            label.setText( "Well done! Everything is ok." )
            nameField.setStyleSheet("")


  • fixed_version_id was changed from Version 2.4 to Future Release - High Priority

@qgib
Copy link
Contributor Author

qgib commented Apr 30, 2017

Author Name: Giovanni Manghi (@gioman)


  • easy_fix was configured as 0
  • regression was configured as 0

@qgib
Copy link
Contributor Author

qgib commented Oct 15, 2018

Author Name: Tudor Bărăscu (@tudorbarascu)


Not relevant anymore (at least for me).


  • status_id was changed from Open to Closed

@qgib qgib closed this as completed Oct 15, 2018
@qgib qgib added Bug Either a bug report, or a bug fix. Let's hope for the latter! Forms labels May 25, 2019
@qgib qgib added this to the Future Release - High Priority milestone May 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Forms
Projects
None yet
Development

No branches or pull requests

1 participant