Qt实现IP输入框

来源:互联网 发布:2016微信数据报告 编辑:程序博客网 时间:2024/05/16 01:31

转自:http://blog.csdn.net/u011417605/article/details/51353337


Qt作为界面框架,已经非常完善了。但是,也不是尽善尽美,IP输入框作为开发中使用很频繁的一个控件,Qt竟然没有实现,也是醉了。不过,我们自己也可以实现,并不是很复杂。

先来看下最终实现的效果:


使用起来还算顺手,实现了以下一些方便的操作:

1.连续输入;

2.连续删除;

3.任意位置插入;

4.自适应大小变化。

5.正则匹配,每个值不大于255。设置IP值时,有正则进行验证是否是IP格式。

实现方法,使用一个大的QLineEdit嵌套四个小的QLineEdit,中间的点是使用paintEvent画出来的。操作是使用eventFilter来进行分发实现的。

实现代码:

[cpp] view plain copy
  1. /************************************************************************/  
  2. /* 作者:徐冬冬 
  3.    QQ:1245178753 
  4.    博客网址:http://blog.csdn.net/u011417605 
  5. */  
  6. /************************************************************************/  
  7. #ifndef QIPLINEEDIT_H  
  8. #define QIPLINEEDIT_H  
  9.   
  10. #include <QLineEdit>  
  11. #include <QEvent>  
  12.   
  13. class QIPLineEdit : public QLineEdit  
  14. {  
  15.     Q_OBJECT  
  16.   
  17. public:  
  18.     QIPLineEdit(QWidget *parent = 0);  
  19.     ~QIPLineEdit();  
  20.   
  21.     void setText(const QString &strIP);  
  22.     QString text() const;  
  23. protected:  
  24.     void paintEvent(QPaintEvent *event);  
  25.     bool eventFilter(QObject *obj, QEvent *ev);  
  26.   
  27.     int getIndex(QLineEdit *pEdit);  
  28.     bool isTextValid(const QString &strIP);  
  29. private:  
  30.     QLineEdit *m_lineEidt[4];  
  31. };  
  32.   
  33. #endif // QIPLINEEDIT_H  
[cpp] view plain copy
  1. /************************************************************************/  
  2. /* 作者:徐冬冬 
  3. QQ:1245178753 
  4. 博客网址:http://blog.csdn.net/u011417605 
  5. */  
  6. /************************************************************************/  
  7. #include "qiplineedit.h"  
  8. #include <QRegExpValidator>  
  9. #include <QPainter>  
  10. #include <QHBoxLayout>  
  11. #include <QKeyEvent>  
  12. #include <QMessageBox>  
  13. #include <QDebug>  
  14.   
  15. QIPLineEdit::QIPLineEdit(QWidget *parent)  
  16.     : QLineEdit(parent)  
  17. {  
  18.     QRegExp rx("(2[0-5]{2}|2[0-4][0-9]|1?[0-9]{1,2})");  
  19.     QHBoxLayout *pHBox = new QHBoxLayout(this);  
  20.     pHBox->setSpacing(10);  
  21.     pHBox->setContentsMargins(0, 0, 0, 0);  
  22.     for (int i = 0; i < 4; i++)  
  23.     {  
  24.         m_lineEidt[i] = new QLineEdit(this);  
  25.         m_lineEidt[i]->setFrame(false);  
  26.         m_lineEidt[i]->setMaxLength(3);  
  27.         m_lineEidt[i]->setAlignment(Qt::AlignCenter);  
  28.         m_lineEidt[i]->installEventFilter(this);  
  29.         m_lineEidt[i]->setValidator(new QRegExpValidator(rx, this));  
  30.         m_lineEidt[i]->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);  
  31.         pHBox->addWidget(m_lineEidt[i]);  
  32.     }  
  33.     this->setReadOnly(true);  
  34. }  
  35.   
  36. QIPLineEdit::~QIPLineEdit()  
  37. {  
  38.   
  39. }  
  40.   
  41. void QIPLineEdit::paintEvent(QPaintEvent *event)  
  42. {  
  43.     __super::paintEvent(event);  
  44.     QPainter painter(this);  
  45.     QBrush brush;  
  46.     brush.setStyle(Qt::BrushStyle::SolidPattern);  
  47.     brush.setColor(Qt::black);  
  48.     painter.setBrush(brush);  
  49.   
  50.     int width = 0;  
  51.     for (int i = 0; i < 3; i++)  
  52.     {  
  53.         width += m_lineEidt[i]->width() + (i == 0 ? 3 : 10);//布局的间隔  
  54.         painter.drawEllipse(width, height() / 2 - 2, 4, 4);  
  55.     }  
  56. }  
  57.   
  58. int QIPLineEdit::getIndex(QLineEdit *pEdit)  
  59. {  
  60.     int index = -1;  
  61.     for (int i = 0; i < 4; i++)  
  62.     {  
  63.         if (pEdit == m_lineEidt[i])  
  64.             index = i;  
  65.     }  
  66.     return index;  
  67. }  
  68.   
  69. bool QIPLineEdit::eventFilter(QObject *obj, QEvent *ev)  
  70. {  
  71.     if (children().contains(obj) && QEvent::KeyPress == ev->type())  
  72.     {  
  73.         QKeyEvent *keyEvent = dynamic_cast<QKeyEvent *>(ev);  
  74.         QLineEdit *pEdit = qobject_cast<QLineEdit *>(obj);  
  75.         switch (keyEvent->key())  
  76.         {  
  77.         case Qt::Key_0:  
  78.         case Qt::Key_1:  
  79.         case Qt::Key_2:  
  80.         case Qt::Key_3:  
  81.         case Qt::Key_4:  
  82.         case Qt::Key_5:  
  83.         case Qt::Key_6:  
  84.         case Qt::Key_7:  
  85.         case Qt::Key_8:  
  86.         case Qt::Key_9:  
  87.         {  
  88.             QString strText = pEdit->text();  
  89.             if (pEdit->selectedText().length())  
  90.             {  
  91.                 pEdit->text().replace(pEdit->selectedText(), QChar(keyEvent->key()));  
  92.             }  
  93.             else if (strText.length() == 3 || strText.length() < 3 && strText.toInt() * 10 > 255)  
  94.             {  
  95.                 int index = getIndex(pEdit);  
  96.                 if (index != -1 && index != 3)  
  97.                 {  
  98.                     m_lineEidt[index + 1]->setFocus();  
  99.                     m_lineEidt[index + 1]->selectAll();  
  100.                 }  
  101.             }  
  102.             else if (strText.length() == 2 && strText.toInt() * 10 < 255)  
  103.             {  
  104.                 if (Qt::Key_0 == keyEvent->key() && strText.toInt())  
  105.                 {  
  106.                     pEdit->setText(strText.insert(pEdit->cursorPosition(), QChar(Qt::Key_0)));  
  107.                 }  
  108.             }  
  109.             return __super::eventFilter(obj, ev);  
  110.         }  
  111.         break;  
  112.         case Qt::Key_Backspace:  
  113.         {  
  114.             QString strText = pEdit->text();  
  115.             if (!strText.length() || strText.length() && !pEdit->cursorPosition())  
  116.             {  
  117.                 int index = getIndex(pEdit);  
  118.                 if (index != -1 && index != 0)  
  119.                 {  
  120.                     m_lineEidt[index - 1]->setFocus();  
  121.                     int length = m_lineEidt[index - 1]->text().length();  
  122.                     m_lineEidt[index - 1]->setCursorPosition(length ? length : 0);  
  123.                 }  
  124.             }  
  125.             return __super::eventFilter(obj, ev);  
  126.         }  
  127.         case Qt::Key_Left:  
  128.         {  
  129.             if (!pEdit->cursorPosition())  
  130.             {  
  131.                 int index = getIndex(pEdit);  
  132.                 if (index != -1 && index != 0)  
  133.                 {  
  134.                     m_lineEidt[index - 1]->setFocus();  
  135.                     int length = m_lineEidt[index - 1]->text().length();  
  136.                     m_lineEidt[index - 1]->setCursorPosition(length ? length : 0);  
  137.                 }  
  138.             }  
  139.             return __super::eventFilter(obj, ev);  
  140.         }  
  141.         case Qt::Key_Right:  
  142.         {  
  143.             if (pEdit->cursorPosition() == pEdit->text().length())  
  144.             {  
  145.                 int index = getIndex(pEdit);  
  146.                 if (index != -1 && index != 3)  
  147.                 {  
  148.                     m_lineEidt[index + 1]->setFocus();  
  149.                     m_lineEidt[index + 1]->setCursorPosition(0);  
  150.                 }  
  151.             }  
  152.             return __super::eventFilter(obj, ev);  
  153.         }  
  154.         default:  
  155.             break;  
  156.         }  
  157.     }  
  158.     return false;  
  159. }  
  160.   
  161. void QIPLineEdit::setText(const QString &strIP)  
  162. {  
  163.     if (!isTextValid(strIP))  
  164.     {  
  165.         QMessageBox::warning(this"Attention""Your IP Address is Invalid!", QMessageBox::StandardButton::Ok);  
  166.         return;  
  167.     }  
  168.     else  
  169.     {  
  170.         int i = 0;  
  171.         QStringList ipList = strIP.split(".");  
  172.         for each (QString ip in ipList)  
  173.         {  
  174.             m_lineEidt[i]->setText(ip);  
  175.             i++;  
  176.         }  
  177.     }  
  178. }  
  179.   
  180. bool QIPLineEdit::isTextValid(const QString &strIP)  
  181. {  
  182.     QRegExp rx2("\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");  
  183.     if (!rx2.exactMatch(strIP))  
  184.         return false;  
  185.     return true;  
  186. }  
  187.   
  188. QString QIPLineEdit::text() const  
  189. {  
  190.     QString strIP;  
  191.     for (int i = 0; i < 4; i++)  
  192.         strIP.append(m_lineEidt[i]->text());  
  193.     return strIP;  
  194. }  
交流qq:1245178753

本文地址:http://blog.csdn.net/u011417605/article/details/51353337

源码下载:http://download.csdn.net/detail/u011417605/9514281


原创粉丝点击