• 10192阅读
  • 6回复

怎样验证输入的IP、子网掩码的合法性 [复制链接]

上一主题 下一主题
离线yanjinchen
 
只看楼主 倒序阅读 楼主  发表于: 2009-05-04
现在做的交互界面需要输入IP和子网掩码,想要在输入时进行合法性判断,不知怎样实现,请各位高手帮帮忙啊,不胜感激
离线茶亭量子
只看该作者 1楼 发表于: 2009-05-04
//ip.h
#include <qwidget.h>
#include <qstring.h>
#include <qlineedit.h>
#include <qvalidator.h>

#define IP_H

class IP : public QWidget
{
    public:
        IP ( const QString & text, QWidget *parent, const char *name );
        QString getValue();
    private:
        QLineEdit * ip[4];
        QIntValidator * ipv[4];
};

//ip.cpp
#include <qwidget.h>
#include <qlabel.h>
#include <qfont.h>
#include <qhbox.h>
#include <qlineedit.h>

#include "ip.h"

IP::IP(const QString & text, QWidget *parent, const char *nombre) : QWidget(parent, nombre, 0)
{
    QFont *fuente = new QFont("Arial", 14, QFont::Normal);
    fuente->setPixelSize(14);
    QLabel *label = new QLabel( this );
    label->setFont(* fuente);
    label->setMinimumWidth(140);
    label->setMaximumWidth(140);
    QHBox * ipp = new QHBox((QWidget*) this);
    ipp->setMinimumWidth(140);
    ipp->setMaximumWidth(140);
    for (int i=0; i<4; i++)
    {
        ip = new QLineEdit((QWidget*) ipp, nombre);
        ip->setFont(* fuente);
        ip->setMinimumWidth(30);
        ip->setMaxLength(3);
        ipv = new QIntValidator(0, 255, (QObject*)ipp);
        ip->setValidator(ipv);
    }

    label->move(0, 0);
    ipp->move(150, 0);
    label->setText(text);
    // ip->setInputMask("000.000.000.000; ");
}

QString IP::getValue()
{
    bool flag = false;
    for (int i=0; i<4; i++)
        if ( ip->text().isEmpty() )
            flag = true;
    if (flag)
        return QString("0.0.0.0");
    else
        return QString(ip[0]->text()+ "." + ip[1]->text() + "." + ip[2]->text() + "." +
                ip[3]->text());
}

代码来源:http://www.tuxum.org/browser/tuxum/tuxtalador/tuxtalador/ip.h?rev=218
这样的代码先在网上找吧,一般都是有解决方案的。
生命在于运动。
没有做不到,只有想不到。
http://hi.baidu.com/kernel_linux
离线yanjinchen
只看该作者 2楼 发表于: 2009-05-05
十分感谢楼上啊,
但子网掩码的验证怎么实现阿?我在网上找了半天还是找不到,可能我文献检索能力差吧
离线foxyz

只看该作者 3楼 发表于: 2009-05-05
用Qt正则表达式,轻松搞定阿!
离线yanjinchen
只看该作者 4楼 发表于: 2009-05-06
子网掩码的正则表达式应该怎么写阿?感觉有点混乱,还请楼上指点一下,不吝赐教阿
离线yanjinchen
只看该作者 5楼 发表于: 2009-05-06
已经搞定啦,多谢大家的帮忙啊
离线yanglisun
只看该作者 6楼 发表于: 2009-05-07
搞定了贴个搞定的结果吧
快速回复
限100 字节
 
上一个 下一个