2015-12-02 34 views
1

以下是驗證帳戶代碼的驗證碼,並在特定間隔添加破折號。示例賬戶代碼是140-100-1000-6610-543。該代碼採用正則表達式:\d{3}-\d{3}-\d{4}-\d{4}-\d{3],並允許用戶輸入數字,並在正則表達式中有一個短劃線,它爲用戶放置短劃線。或者說,它應該。在我們的生產服務器上('Qt version:','4.8.1'),('SIP version:','4.13.2'),('PyQt version:','4.9.1')) 。我們的開發服務器升級到('Qt version:','4.8.6'),('SIP version:','4.15.5'),('PyQt version:','4.10.4'),它不會在那裏工作。QValidator修正問題

在每臺服務器上,我輸入140。在製作(舊版本)上,行編輯值更改爲140-。在較新的版本開發服務器上,它不添加短劃線。

請讓我知道你是否看到我的問題,並讓我知道這是PyQt問題還是Qt問題。

import sys 
from PyQt4 import QtGui, QtCore 

DEBUG = True 

class acValidator(QtGui.QRegExpValidator): 

    def __init__(self, regexp, widget): 
     QtGui.QRegExpValidator.__init__(self, regexp, widget) 
     self.widget = widget 

    def validate(self, text, pos): 
     '''function to decide if this account code is valid''' 
     valid, _npos = QtGui.QRegExpValidator.validate(self, text, pos) 

     if valid != QtGui.QValidator.Acceptable: 
      self.fixup(text) 
      print 'acWidget.validate result of fixup', text 

      # move position to the end, if fixup just added a dash 
      # to the end 
      newstr = self.widget.text() 
      newpos = len(str(newstr)) 
      if pos + 1 == newpos and newstr[pos:newpos] == '-': 
       pos = newpos 

     # return the valid variable, and the current position 
     return (valid, pos) 

    def fixup(self, text): 
     '''place dashes, if we can''' 
     # pylint: disable=no-member 
     if DEBUG: 
      print 'acWidget.py fixup' 
     reParts = self.regExp().pattern().split('-') 
     if DEBUG: 
      print list(reParts) 
     newreg = '' 
     for i in range(len(reParts)): 
      newreg += reParts[i] 
      if DEBUG: 
       print i, reParts[i] 

      nr = QtCore.QRegExp(newreg) 
      # pylint: disable=no-member 
      if nr.exactMatch(text) and not self.regExp().exactMatch(text): 
       if DEBUG: 
        print 'adding dash' 
       text += '-' 
       return 

      newreg += '-' 

    def isValid(self): 
     '''return a true or false for the validity based on whether the 
     widget is a lineEdit or a comboBox (acCB). true only if the 
     validator returns QtGui.QValidator.Acceptable 
     ''' 
     valid, _npos = QtGui.QRegExpValidator.validate(self, 
         self.widget.text(), 
         self.widget.cursorPosition()) 
     if valid == QtGui.QValidator.Acceptable: 
      return True 

     return False 


class acWidget(QtGui.QLineEdit): 

    def __init__(self, parent=None): 
     QtGui.QLineEdit.__init__(self, parent) 

     self.regex = r'\d{3}-\d{3}-\d{4}-\d{4}-\d{3}' 
     self.setMinimumWidth(200) 
     self.setValidator(acValidator(QtCore.QRegExp(self.regex), 
        self)) 

if __name__ == '__main__': 

    app = QtGui.QApplication(sys.argv) 
    form = QtGui.QDialog() 
    layout = QtGui.QVBoxLayout(form) 
    a = acWidget(form) 
    layout.addWidget(a) 
    form.setLayout(layout) 
    form.setMinimumWidth(400) 
    form.setMinimumHeight(200) 
    form.show() 
    app.exec_() 

回答

1

該問題通過的fixupvalidate C++的簽名要求該text參數是可修改引起的。如果你使用的是Python 2,這種明顯不加區別的做事方式是PyQt的榮幸;而Python 3,signatures have been changed,以便這些方法只是返回修改後的值。

在你的代碼中的具體問題可以在這裏找到:

def fixup(self, text): 
     ... 

     text += '-' 

看來,在早期版本的PyQt的,增強對QString任務做了一個隱含的就地突變。但在最近的版本,它的工作原理更像是普通的Python增強分配和簡單的重新結合這樣的局部變量:

 text = text + '-' 

要解決這個問題,你可以做一個顯式就地突變:

 text.append('-') 
+0

非常感謝,ekhumoro!你是對的。我們仍然在新服務器上使用2.7,但顯然已經發生了一些變化。再次感謝! – Kerri