查看完整版本: [-- 怎样验证输入的IP、子网掩码的合法性 --]

QTCN开发网 -> Qt基础编程 -> 怎样验证输入的IP、子网掩码的合法性 [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

yanjinchen 2009-05-04 10:21

怎样验证输入的IP、子网掩码的合法性

现在做的交互界面需要输入IP和子网掩码,想要在输入时进行合法性判断,不知怎样实现,请各位高手帮帮忙啊,不胜感激

茶亭量子 2009-05-04 18:26
//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
这样的代码先在网上找吧,一般都是有解决方案的。

yanjinchen 2009-05-05 13:01
十分感谢楼上啊,
但子网掩码的验证怎么实现阿?我在网上找了半天还是找不到,可能我文献检索能力差吧

foxyz 2009-05-05 17:34
用Qt正则表达式,轻松搞定阿!

yanjinchen 2009-05-06 14:16
子网掩码的正则表达式应该怎么写阿?感觉有点混乱,还请楼上指点一下,不吝赐教阿

yanjinchen 2009-05-06 15:17
已经搞定啦,多谢大家的帮忙啊

yanglisun 2009-05-07 10:11
搞定了贴个搞定的结果吧


查看完整版本: [-- 怎样验证输入的IP、子网掩码的合法性 --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled