<div dir="ltr">Got a response to this on stackoverflow - thanks!<div><br></div><div><a href="http://stackoverflow.com/questions/34055174/qvalidator-fixup-issue">http://stackoverflow.com/questions/34055174/qvalidator-fixup-issue</a><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Dec 2, 2015 at 4:27 PM, Kerri Reno <span dir="ltr"><<a href="mailto:kreno@yumaed.org" target="_blank">kreno@yumaed.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div dir="ltr">Good afternoon,<div><br></div><div>Following is my code to validate an account code, and add dashes at certain intervals. An example account code is 140-100-1000-6610-543. The code takes the regex: \d{3}-\d{3}-\d{4}-\d{4}-\d{3] and allows the user to type in numbers, and where there is a dash in the regex, it places the dash for the user. Or rather, it should. On our production server (('Qt version:', '4.8.1'), ('SIP version:', '4.13.2'), ('PyQt version:', '4.9.1')) it does work. Our dev server is upgraded to ('Qt version:', '4.8.6'), ('SIP version:', '4.15.5'), ('PyQt version:', '4.10.4') and it doesn't work there.</div><span class=""><div><br></div><div>On each server, I type in 140. On the production (older version), the line edit value changes to 140-. On the newer version development server, it does not add the dash.</div><div><br></div></span><div>Please let me know if you see my issue, and let me know if this is a PyQT issue, or a QT issue.</div><div><br></div><div>Thanks in advance!</div><div><div class="h5"><div>Kerri</div><div><br></div><div>CODE:</div><div><br></div><div><div>#!/usr/bin/env python</div><div># vim:set ts=4:set ff=unix</div><div>import sys</div><div>from PyQt4 import QtGui, QtCore</div><div><br></div><div>DEBUG = True</div><div><br></div><div>class acValidator(QtGui.QRegExpValidator):</div><div><br></div><div> def __init__(self, regexp, widget):</div><div> QtGui.QRegExpValidator.__init__(self, regexp, widget)</div><div> self.widget = widget</div><div><br></div><div> def validate(self, text, pos):</div><div> '''function to decide if this account code is valid'''</div><div> valid, _npos = QtGui.QRegExpValidator.validate(self, text, pos)</div><div><br></div><div> if valid != QtGui.QValidator.Acceptable:</div><div> self.fixup(text)</div><div> print 'acWidget.validate result of fixup', text</div><div><br></div><div> # move position to the end, if fixup just added a dash</div><div> # to the end</div><div> newstr = self.widget.text()</div><div> newpos = len(str(newstr))</div><div> if pos + 1 == newpos and newstr[pos:newpos] == '-':</div><div> pos = newpos</div><div><br></div><div> # return the valid variable, and the current position</div><div> return (valid, pos)</div><div><br></div><div> def fixup(self, text):</div><div> '''place dashes, if we can'''</div><div> # pylint: disable=no-member</div><div> if DEBUG:</div><div> print 'acWidget.py fixup'</div><div> reParts = self.regExp().pattern().split('-')</div><div> if DEBUG:</div><div> print list(reParts)</div><div> newreg = ''</div><div> for i in range(len(reParts)):</div><div> newreg += reParts[i]</div><div> if DEBUG:</div><div> print i, reParts[i]</div><div><br></div><div> nr = QtCore.QRegExp(newreg)</div><div> # pylint: disable=no-member</div><div> if nr.exactMatch(text) and not self.regExp().exactMatch(text):</div><div> if DEBUG:</div><div> print 'adding dash'</div><div> text += '-'</div><div> return</div><div><br></div><div> newreg += '-'</div><div><br></div><div> def isValid(self):</div><div> '''return a true or false for the validity based on whether the</div><div> widget is a lineEdit or a comboBox (acCB). true only if the</div><div> validator returns QtGui.QValidator.Acceptable</div><div> '''</div><div> valid, _npos = QtGui.QRegExpValidator.validate(self,</div><div> self.widget.text(),</div><div> self.widget.cursorPosition())</div><div> if valid == QtGui.QValidator.Acceptable:</div><div> return True</div><div><br></div><div> return False</div><div><br></div><div><br></div><div>class acWidget(QtGui.QLineEdit):</div><div><br></div><div> def __init__(self, parent=None):</div><div> QtGui.QLineEdit.__init__(self, parent)</div><div><br></div><div> self.regex = r'\d{3}-\d{3}-\d{4}-\d{4}-\d{3}'</div><div> self.setMinimumWidth(200)</div><div> self.setValidator(acValidator(QtCore.QRegExp(self.regex),</div><div> self))</div><div><br></div><div>if __name__ == '__main__':</div><div><br></div><div> app = QtGui.QApplication(sys.argv)</div><div> form = QtGui.QDialog()</div><div> layout = QtGui.QVBoxLayout(form)</div><div> a = acWidget(form)</div><div> layout.addWidget(a)</div><div> form.setLayout(layout)</div><div> form.setMinimumWidth(400)</div><div> form.setMinimumHeight(200)</div><div> form.show()</div><div> app.exec_()</div></div><span><font color="#888888"><div><br></div><div><div><br></div><div><br></div><div><span style="color:rgb(34,34,34)">-- </span><br></div></div></font></span></div></div></div></div><span class=""><div><div dir="ltr">.·:*¨¨*:·. .·:*¨¨*:·. .·:*¨¨*:·.<br>Yuma Educational Computer Consortium<br>Compass Development Team<br>Kerri Reno<br><a href="mailto:kreno@yumaed.org" target="_blank">kreno@yumaed.org</a> (928) 502-4240<br>.·:*¨¨*:·. .·:*¨¨*:·. .·:*¨¨*:·.<br>I'm so busy I don't know if I found a rope or lost a horse!</div></div>
</span></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr">Please send problems and questions to <a href="mailto:cases@yuhsd.fogbugz.com" target="_blank">compass@yumaed.org</a><br>If you have an emergency, and can't reach me, please call the help desk at 928-502-4202.<br>.·:*¨¨*:·. .·:*¨¨*:·. .·:*¨¨*:·.<br>Yuma Educational Computer Consortium<br>Compass Development Team<br>Kerri Reno<br><a href="mailto:kreno@yumaed.org" target="_blank">kreno@yumaed.org</a> (928) 502-4240<br>.·:*¨¨*:·. .·:*¨¨*:·. .·:*¨¨*:·.<br>I'm so busy I don't know if I found a rope or lost a horse!</div></div>
</div>