IP控件

[cpp]  view plain copy
  1. // MyIpPartLineEdit.h   
[cpp]  view plain copy
  1. #pragma once   
  2.   
  3. #include <QLineEdit>   
  4.   
  5. class QWidget;   
  6. class QFocusEvent;   
  7. class QKeyEvent;   
  8.   
  9. class MyIpPartLineEdit : public QLineEdit   
  10. {   
  11.     Q_OBJECT   
  12. public:   
  13.     MyIpPartLineEdit(QWidget *parent = 0);   
  14.     ~MyIpPartLineEdit(void);   
  15.   
  16.     void set_nexttab_edit(QLineEdit *nexttab) { next_tab_ = nexttab; }   
  17.   
  18. protected:   
  19.     virtual void focusInEvent(QFocusEvent *e);   
  20.     virtual    void keyPressEvent(QKeyEvent *event);     
  21.   
  22. private slots:   
  23.     void text_edited(const QString& text);   
  24.   
  25. private:   
  26.     QLineEdit *next_tab_;   
  27. };   


[cpp]  view plain copy
  1. // MyIpPartLineEdit.cpp   
  2. #include "MyIpPartLineEdit.h"   
  3. #include <QIntValidator>   
  4. #include <QKeyEvent>   
  5.   
  6. MyIpPartLineEdit::MyIpPartLineEdit(QWidget *parent/* = 0*/)   
  7. : QLineEdit(parent)   
  8. {   
  9.     next_tab_ = NULL;   
  10.   
  11.     this->setMaxLength(3);   
  12.     this->setFrame(false);   
  13.     this->setAlignment(Qt::AlignCenter);   
  14.   
  15.     QValidator *validator = new QIntValidator(0, 255, this);   
  16.     this->setValidator(validator);   
  17.   
  18.     connect(this, SIGNAL(textEdited(const QString&)), this, SLOT(text_edited(const QString&)));   
  19. }   
  20.   
  21. MyIpPartLineEdit::~MyIpPartLineEdit(void)   
  22. {   
  23. }   
  24.   
  25. void MyIpPartLineEdit::focusInEvent(QFocusEvent *e)   
  26. {   
  27.     this->selectAll();   
  28.     QLineEdit::focusInEvent(e);   
  29. }   
  30.   
  31. void MyIpPartLineEdit::keyPressEvent(QKeyEvent *event)   
  32. {   
  33.     if (event->key() == Qt::Key_Period)   
  34.     {   
  35.         if (next_tab_)   
  36.         {   
  37.             next_tab_->setFocus();   
  38.             next_tab_->selectAll();   
  39.         }   
  40.     }   
  41.     QLineEdit::keyPressEvent(event);   
  42. }   
  43.   
  44. void MyIpPartLineEdit::text_edited(const QString& text)   
  45. {   
  46.     QIntValidator v(0, 255, this);   
  47.     QString ipaddr = text;   
  48.     int pos = 0;   
  49.     QValidator::State state = v.validate(ipaddr, pos);    
  50.     if (state == QValidator::Acceptable)   
  51.     {   
  52.         if (ipaddr.size() > 1)   
  53.         {   
  54.             if (ipaddr.size() == 2)   
  55.             {   
  56.                 int ipnum = ipaddr.toInt();   
  57.                    
  58.                 if (ipnum > 25)   
  59.                 {   
  60.                     if (next_tab_)   
  61.                     {   
  62.                         next_tab_->setFocus();   
  63.                         next_tab_->selectAll();   
  64.                     }       
  65.                 }   
  66.             }   
  67.             else   
  68.             {   
  69.                 if (next_tab_)   
  70.                 {   
  71.                     next_tab_->setFocus();   
  72.                     next_tab_->selectAll();   
  73.                 }       
  74.             }   
  75.         }   
  76.     }   
  77. }  

[cpp]  view plain copy
  1. // MyIpAddrEdit.h   
  2. #pragma once   
  3.   
  4. #include <QWidget>   
  5.   
  6. class QLineEdit;   
  7. class QLabel;   
  8. class MyIpPartLineEdit;   
  9.   
  10. class MyIpAddrEdit : public QWidget   
  11. {   
  12.     Q_OBJECT   
  13. public:   
  14.     MyIpAddrEdit(QWidget* pParent = 0);   
  15.     ~MyIpAddrEdit();   
  16.   
  17.     void settext(const QString &text);   
  18.     QString text();   
  19.     void setStyleSheet(const QString &styleSheet);   
  20.   
  21. signals:   
  22.     void textchanged(const QString& text);   
  23.     void textedited(const QString &text);   
  24.   
  25. private slots:   
  26.     void textchangedslot(const QString& text);   
  27.     void texteditedslot(const QString &text);   
  28.   
  29. private:   
  30.     MyIpPartLineEdit *ip_part1_;   
  31.     MyIpPartLineEdit *ip_part2_;   
  32.     MyIpPartLineEdit *ip_part3_;   
  33.     QLineEdit *ip_part4_;   
  34.   
  35.     QLabel *labeldot1_;   
  36.     QLabel *labeldot2_;       
  37.     QLabel *labeldot3_;   
  38. };  

[cpp]  view plain copy
  1. // MyIpAddrEdit.cpp   
  2. #include "MyIpAddrEdit.h"   
  3. #include <QRegExpValidator>   
  4. #include <QLabel>   
  5. #include "MyIpPartLineEdit.h"   
  6.   
  7. MyIpAddrEdit::MyIpAddrEdit(QWidget* pParent /* = 0 */)   
  8. : QWidget(pParent)   
  9. {   
  10.     ip_part1_ = new MyIpPartLineEdit(this);   
  11.     ip_part2_ = new MyIpPartLineEdit(this);   
  12.     ip_part3_ = new MyIpPartLineEdit(this);   
  13.     ip_part4_ = new MyIpPartLineEdit(this);   
  14.   
  15.     labeldot1_ = new QLabel(this);   
  16.     labeldot2_ = new QLabel(this);   
  17.     labeldot3_ = new QLabel(this);   
  18.   
  19.     ip_part1_->setGeometry(QRect(0, 0, 30, 20));   
  20.     ip_part2_->setGeometry(QRect(30, 0, 30, 20));   
  21.     ip_part3_->setGeometry(QRect(60, 0, 30, 20));   
  22.     ip_part4_->setGeometry(QRect(90, 0, 30, 20));   
  23.   
  24.     labeldot1_->setText(" .");   
  25.     labeldot1_->setGeometry(QRect(27, 1, 6, 18));   
  26.     labeldot1_->setAlignment(Qt::AlignCenter);   
  27.   
  28.     labeldot2_->setText(" .");   
  29.     labeldot2_->setGeometry(QRect(57, 1, 6, 18));   
  30.     labeldot2_->setAlignment(Qt::AlignCenter);   
  31.   
  32.     labeldot3_->setText(" .");   
  33.     labeldot3_->setGeometry(QRect(87, 1, 6, 18));   
  34.     labeldot3_->setAlignment(Qt::AlignCenter);   
  35.   
  36.     QWidget::setTabOrder(ip_part1_, ip_part2_);   
  37.     QWidget::setTabOrder(ip_part2_, ip_part3_);   
  38.     QWidget::setTabOrder(ip_part3_, ip_part4_);   
  39.     ip_part1_->set_nexttab_edit(ip_part2_);   
  40.     ip_part2_->set_nexttab_edit(ip_part3_);   
  41.     ip_part3_->set_nexttab_edit(ip_part4_);   
  42.   
  43.   
  44.     connect(ip_part1_, SIGNAL(textChanged(const QString&)), this, SLOT(textchangedslot(const QString&)));   
  45.     connect(ip_part2_, SIGNAL(textChanged(const QString&)), this, SLOT(textchangedslot(const QString&)));   
  46.     connect(ip_part3_, SIGNAL(textChanged(const QString&)), this, SLOT(textchangedslot(const QString&)));   
  47.     connect(ip_part4_, SIGNAL(textChanged(const QString&)), this, SLOT(textchangedslot(const QString&)));   
  48.   
  49.     connect(ip_part1_, SIGNAL(textEdited (const QString&)), this, SLOT(texteditedslot(const QString&)));   
  50.     connect(ip_part2_, SIGNAL(textEdited (const QString&)), this, SLOT(texteditedslot(const QString&)));   
  51.     connect(ip_part3_, SIGNAL(textEdited (const QString&)), this, SLOT(texteditedslot(const QString&)));   
  52.     connect(ip_part4_, SIGNAL(textEdited (const QString&)), this, SLOT(texteditedslot(const QString&)));   
  53.        
  54. }   
  55.   
  56. MyIpAddrEdit::~MyIpAddrEdit()   
  57. {   
  58.   
  59. }   
  60.   
  61. void MyIpAddrEdit::textchangedslot(const QString& /*text*/)   
  62. {   
  63.     QString ippart1, ippart2, ippart3, ippart4;   
  64.     ippart1 = ip_part1_->text();   
  65.     ippart2 = ip_part2_->text();   
  66.     ippart3 = ip_part3_->text();   
  67.     ippart4 = ip_part4_->text();   
  68.   
  69.     QString ipaddr = QString("%1.%2.%3.%4")   
  70.                      .arg(ippart1)   
  71.                      .arg(ippart2)   
  72.                      .arg(ippart3)   
  73.                      .arg(ippart4);   
  74.   
  75.     emit textchanged(ipaddr);   
  76. }   
  77.   
  78. void MyIpAddrEdit::texteditedslot(const QString &/*text*/)   
  79. {   
  80.     QString ippart1, ippart2, ippart3, ippart4;   
  81.     ippart1 = ip_part1_->text();   
  82.     ippart2 = ip_part2_->text();   
  83.     ippart3 = ip_part3_->text();   
  84.     ippart4 = ip_part4_->text();   
  85.   
  86.     QString ipaddr = QString("%1.%2.%3.%4")   
  87.         .arg(ippart1)   
  88.         .arg(ippart2)   
  89.         .arg(ippart3)   
  90.         .arg(ippart4);   
  91.   
  92.     emit textedited(ipaddr);   
  93. }   
  94.   
  95. void MyIpAddrEdit::settext(const QString &text)   
  96. {   
  97.     QString ippart1, ippart2, ippart3, ippart4;   
  98.     QString qstring_validate = text;   
  99.   
  100.     // IP地址验证   
  101.     QRegExp regexp("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)");   
  102.     QRegExpValidator regexp_validator(regexp, this);   
  103.     int nPos = 0;   
  104.     QValidator::State state = regexp_validator.validate(qstring_validate, nPos);   
  105.     // IP合法   
  106.     if (state == QValidator::Acceptable)   
  107.     {   
  108.         QStringList ippartlist = text.split(".");   
  109.        
  110.         int strcount = ippartlist.size();   
  111.         int index = 0;   
  112.         if (index < strcount)   
  113.         {   
  114.             ippart1 = ippartlist.at(index);   
  115.         }   
  116.         if (++index < strcount)   
  117.         {   
  118.             ippart2 = ippartlist.at(index);   
  119.         }   
  120.         if (++index < strcount)   
  121.         {   
  122.             ippart3 = ippartlist.at(index);   
  123.         }   
  124.         if (++index < strcount)   
  125.         {   
  126.             ippart4 = ippartlist.at(index);   
  127.         }   
  128.     }   
  129.   
  130.     ip_part1_->setText(ippart1);   
  131.     ip_part2_->setText(ippart2);   
  132.     ip_part3_->setText(ippart3);   
  133.     ip_part4_->setText(ippart4);   
  134. }   
  135.   
  136. QString MyIpAddrEdit::text()   
  137. {   
  138.     QString ippart1, ippart2, ippart3, ippart4;   
  139.     ippart1 = ip_part1_->text();   
  140.     ippart2 = ip_part2_->text();   
  141.     ippart3 = ip_part3_->text();   
  142.     ippart4 = ip_part4_->text();   
  143.   
  144.     return QString("%1.%2.%3.%4")   
  145.         .arg(ippart1)   
  146.         .arg(ippart2)   
  147.         .arg(ippart3)   
  148.         .arg(ippart4);   
  149. }   
  150.   
  151. void MyIpAddrEdit::setStyleSheet(const QString &styleSheet)   
  152. {   
  153.     ip_part1_->setStyleSheet(styleSheet);   
  154.     ip_part2_->setStyleSheet(styleSheet);   
  155.     ip_part3_->setStyleSheet(styleSheet);   
  156.     ip_part4_->setStyleSheet(styleSheet);   
  157. }  

[cpp]  view plain copy
  1. //main.cpp  
  2. #include <QApplication>   
  3. #include <QDialog>   
  4. #include "MyIpAddrEdit.h"  
  5. int main(int argc, char *argv[])   
  6. {   
  7.     QApplication app(argc, argv);  
  8.     MyIpAddrEdit *ipAddr = new MyIpAddrEdit();   
  9.     ipAddr->settext("127.0.0.1");   
  10.     ipAddr->show();  
  11.     return app.exec();   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值