lrfgjj2 发表于 2011-12-22 08:53

PyQt4实现封装的QT的ip地址控件

<DIV>
<DIV class=article_content>
<P>由于QT没有提供现成的IP地址控件,而仅仅使用正则表达式和inputMask为QLineEdit进行IP地址输入限制和验证,效果不怎么理想。所以用PyQt4封装了一个IPv4地址控件。</P>
<P>from PyQt4.QtGui import *<BR>from PyQt4.QtCore import *</P>
<P><BR>class IpPartEdit(QLineEdit):<BR>&nbsp; &nbsp; def __init__(self, parent = None):<BR>&nbsp; &nbsp; &nbsp; &nbsp; QLineEdit.__init__(self, parent)</P>
<P><BR>&nbsp; &nbsp; &nbsp; &nbsp; self.nextTab = None<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.setMaxLength(3)<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.setFrame(False)<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.setAlignment(Qt.AlignCenter)<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; validator = QIntValidator(0, 255, self)<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.setValidator(validator)<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; self.connect(self, SIGNAL('textEdited(QString)'),\<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self, SLOT('text_edited(QString)'))<BR><BR>&nbsp; &nbsp; def set_nextTabEdit(self, nextTab):<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.nextTab = nextTab<BR><BR>&nbsp; &nbsp; def focusInEvent(self, event):<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.selectAll()<BR>&nbsp; &nbsp; &nbsp; &nbsp; super(IpPartEdit, self).focusInEvent(event)<BR><BR>&nbsp; &nbsp; def keyPressEvent(self, event):<BR>&nbsp; &nbsp; &nbsp; &nbsp; if (event.key() == Qt.Key_Period):<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.nextTab:<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.nextTab.setFocus()<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.nextTab.selectAll()<BR>&nbsp; &nbsp; &nbsp; &nbsp; super(IpPartEdit, self).keyPressEvent(event)<BR><BR>&nbsp; &nbsp; @pyqtSlot('QString')<BR>&nbsp; &nbsp; def text_edited(self, text):<BR>&nbsp; &nbsp; &nbsp; &nbsp; validator = QIntValidator(0, 255, self)<BR>&nbsp; &nbsp; &nbsp; &nbsp; ipaddr = text<BR>&nbsp; &nbsp; &nbsp; &nbsp; pos = 0<BR>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; state = validator.validate(ipaddr, pos)<BR>&nbsp; &nbsp; &nbsp; &nbsp; if state == QValidator.Acceptable:<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ipaddr.size() &gt; 1:<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ipaddr.size() == 2:<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ipnum = ipaddr.toInt()<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ipnum &gt; 25:<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.nextTab:<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.nextTab.setFocus()<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.nextTab.selectAll()<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.nextTab:<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.nextTab.setFocus()<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.nextTab.selectAll()<BR><BR>class Ip4Edit(QLineEdit):<BR>&nbsp; &nbsp; def __init__(self, parent = None):<BR>&nbsp; &nbsp; &nbsp; &nbsp; QLineEdit.__init__(self, parent)<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;self.ip_part1 = IpPartEdit()<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.ip_part2 = IpPartEdit()<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.ip_part3 = IpPartEdit()<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.ip_part4 = IpPartEdit()<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.ip_part1.setAlignment(Qt.AlignCenter)<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.ip_part2.setAlignment(Qt.AlignCenter)<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.ip_part3.setAlignment(Qt.AlignCenter)<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.ip_part4.setAlignment(Qt.AlignCenter)<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; self.labeldot1 = QLabel('.')<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.labeldot2 = QLabel('.')<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.labeldot3 = QLabel('.')<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.labeldot1.setAlignment(Qt.AlignCenter)<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.labeldot2.setAlignment(Qt.AlignCenter)<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.labeldot3.setAlignment(Qt.AlignCenter)<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; layout = QHBoxLayout() &nbsp; &nbsp; &nbsp; &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; layout.addWidget(self.ip_part1)<BR>&nbsp; &nbsp; &nbsp; &nbsp; layout.addWidget(self.labeldot1)<BR>&nbsp; &nbsp; &nbsp; &nbsp; layout.addWidget(self.ip_part2)<BR>&nbsp; &nbsp; &nbsp; &nbsp; layout.addWidget(self.labeldot2)<BR>&nbsp; &nbsp; &nbsp; &nbsp; layout.addWidget(self.ip_part3)<BR>&nbsp; &nbsp; &nbsp; &nbsp; layout.addWidget(self.labeldot3)<BR>&nbsp; &nbsp; &nbsp; &nbsp; layout.addWidget(self.ip_part4)<BR>&nbsp; &nbsp; &nbsp; &nbsp; layout.setSpacing(0)<BR>&nbsp; &nbsp; &nbsp; &nbsp; layout.setContentsMargins(QMargins(2, 2, 2, 2))<BR>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.setLayout(layout)<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; QWidget.setTabOrder(self.ip_part1, self.ip_part2)<BR>&nbsp; &nbsp; &nbsp; &nbsp; QWidget.setTabOrder(self.ip_part2, self.ip_part3)<BR>&nbsp; &nbsp; &nbsp; &nbsp; QWidget.setTabOrder(self.ip_part3, self.ip_part4)<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.ip_part1.set_nextTabEdit(self.ip_part2)<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.ip_part2.set_nextTabEdit(self.ip_part3)<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.ip_part3.set_nextTabEdit(self.ip_part4)<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; self.connect(self.ip_part1, SIGNAL('textChanged(QString)'),\<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self, SLOT('textChangedSlot(QString)'))<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.connect(self.ip_part2, SIGNAL('textChanged(QString)'),\<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self, SLOT('textChangedSlot(QString)'))<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.connect(self.ip_part3, SIGNAL('textChanged(QString)'),\<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self, SLOT('textChangedSlot(QString)'))<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.connect(self.ip_part4, SIGNAL('textChanged(QString)'),\<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self, SLOT('textChangedSlot(QString)'))<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; self.connect(self.ip_part1, SIGNAL('textEdited(QString)'),\<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self, SLOT('textEditedSlot(QString)'))<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.connect(self.ip_part2, SIGNAL('textEdited(QString)'),\<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self, SLOT('textEditedSlot(QString)'))<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.connect(self.ip_part3, SIGNAL('textEdited(QString)'),\<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self, SLOT('textEditedSlot(QString)'))<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.connect(self.ip_part4, SIGNAL('textEdited(QString)'),\<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self, SLOT('textEditedSlot(QString)'))<BR><BR>&nbsp; &nbsp; @pyqtSlot('QString')<BR>&nbsp; &nbsp; def textChangedSlot(self, text):<BR>&nbsp; &nbsp; &nbsp; &nbsp; ippart1 = self.ip_part1.text()<BR>&nbsp; &nbsp; &nbsp; &nbsp; ippart2 = self.ip_part2.text()<BR>&nbsp; &nbsp; &nbsp; &nbsp; ippart3 = self.ip_part3.text()<BR>&nbsp; &nbsp; &nbsp; &nbsp; ippart4 = self.ip_part4.text()<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; ipaddr = QString('%1.%2.%3.%4')\<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.arg(ippart1)\<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.arg(ippart2)\<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.arg(ippart3)\<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.arg(ippart4)<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.emit(SIGNAL('textChanged'), ipaddr)<BR><BR>&nbsp; &nbsp; @pyqtSlot('QString')<BR>&nbsp; &nbsp; def textEditedSlot(self, text):<BR>&nbsp; &nbsp; &nbsp; &nbsp; ippart1 = self.ip_part1.text()<BR>&nbsp; &nbsp; &nbsp; &nbsp; ippart2 = self.ip_part2.text()<BR>&nbsp; &nbsp; &nbsp; &nbsp; ippart3 = self.ip_part3.text()<BR>&nbsp; &nbsp; &nbsp; &nbsp; ippart4 = self.ip_part4.text()<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; ipaddr = QString('%1.%2.%3.%4')\<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.arg(ippart1)\<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.arg(ippart2)\<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.arg(ippart3)\<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.arg(ippart4)<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.emit(SIGNAL('textEdited'), ipaddr)<BR><BR>&nbsp; &nbsp; def setText(self, text):<BR>&nbsp; &nbsp; &nbsp; &nbsp; regexp = QRegExp('^((2\d|25|?\d\d?).){3}(2\d||25|?\d\d?)$')<BR>&nbsp; &nbsp; &nbsp; &nbsp; validator = QRegExpValidator(regexp ,self)<BR>&nbsp; &nbsp; &nbsp; &nbsp; nPos = 0<BR>&nbsp; &nbsp; &nbsp; &nbsp; state = validator.validate(text, nPos)<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; ippart1 = QString()<BR>&nbsp; &nbsp; &nbsp; &nbsp; ippart2 = QString()<BR>&nbsp; &nbsp; &nbsp; &nbsp; ippart3 = QString()<BR>&nbsp; &nbsp; &nbsp; &nbsp; ippart4 = QString()<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; if state == QValidator.Acceptable: &nbsp;# valid<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ippartlist = text.split('.')<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strcount = len(ippartlist)<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index = 0<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if index &lt; strcount:<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ippart1 = ippartlist<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index += 1<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if index &lt; strcount:<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ippart2 = ippartlist<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index += 1<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if index &lt; strcount:<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ippart3 = ippartlist<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index += 1<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if index &lt; strcount:<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ippart4 = ippartlist<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; self.ip_part1.setText(ippart1)<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.ip_part2.setText(ippart2)<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.ip_part3.setText(ippart3)<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.ip_part4.setText(ippart4)<BR><BR>&nbsp; &nbsp; def text(self):<BR>&nbsp; &nbsp; &nbsp; &nbsp; ippart1 = self.ip_part1.text()<BR>&nbsp; &nbsp; &nbsp; &nbsp; ippart2 = self.ip_part2.text()<BR>&nbsp; &nbsp; &nbsp; &nbsp; ippart3 = self.ip_part3.text()<BR>&nbsp; &nbsp; &nbsp; &nbsp; ippart4 = self.ip_part4.text()<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; return QString('%1.%2.%3.%4')\<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.arg(ippart1)\<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.arg(ippart2)\<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.arg(ippart3)\<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.arg(ippart4)<BR><BR>&nbsp; &nbsp; def setStyleSheet(self, styleSheet):<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.ip_part1.setStyleSheet(styleSheet)<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.ip_part2.setStyleSheet(styleSheet)<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.ip_part3.setStyleSheet(styleSheet)<BR>&nbsp; &nbsp; &nbsp; &nbsp; self.ip_part4.setStyleSheet(styleSheet)<BR></P></DIV></DIV>
页: [1]
查看完整版本: PyQt4实现封装的QT的ip地址控件