You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is this a bug or there is something new that I haven’t considered.
#!/usr/bin/env python
-- 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()
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
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
from PyQt4.QtCore import *
from PyQt4.QtGui import *
myDialog = None
nameField = None
def formTest(dialog, layerid, featureid):
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
The text was updated successfully, but these errors were encountered: