用到了:
QtCore.QRegExp类--------------正则表达式
QtGui.QRegExpValidator类------做验证
setValidator方法
# !usr/bin/python # _*_ coding: utf-8 _*_ from PyQt4 import QtGui, QtCore class MyQDialog(QtGui.QDialog): """ Class documentation goes here. """ def __init__(self, parent = None): """ Constructor """ QtGui.QDialog.__init__(self, parent) self.resize(200, 60) self.setWindowTitle(u"QLineEdit加正则") regExp=QtCore.QRegExp('^((2[0-4]d|25[0-5]|[1-9]?d|1d{2}).){3}(2[0-4]d|25[0-5]|[01]?dd?):d{1,5}$') self.IP_edit = QtGui.QLineEdit("",self) self.IP_edit.setValidator(QtGui.QRegExpValidator(regExp,self)) self.IP_edit.setGeometry(QtCore.QRect(10, 20, 180, 25)) if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) ui=MyQDialog() ui.show() app.exec_()