PyQt4学习--QLineEdit加入正则判断IP

用到了:

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|1\d{2})\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?):\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_()

 

posted @ 2014-03-13 19:37  洛卜哒  阅读(1773)  评论(1编辑  收藏  举报