加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Qt编程3--IP地址、MAC地址输入框

(2013-06-25 16:38:50)
分类: QT编程学习
一、效果图片
http://s1/mw690/c2818bebtdff647227530&690

二、头文件:



  @名称:IPV4、IPV6(未完)、MAC输入部件

  @日期:2013年6月25日

  @作者:zhj

  @提示:有许多繁琐的地方,日后改进、对IP规范未做详细检查(日后改进)


#ifndef IPLINEEDIT_H
#define IPLINEEDIT_H

//#define IPV4
//#define IPV6
#define MAC_AREA "ABCDEFacbcdef0123456789"

#include <QWidget>
#include <QLabel>a
#include <QMessageBox>
#include <QLineEdit>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QIntValidator>
#include <QRegExp>
#include <QRegExpValidator>
#include <iostream>
using namespace std;

//ip地址部分类
class ipPart : public QLineEdit
{
    Q_OBJECT
public:
    ipPart(QFrame *parent = 0,int type = 0);
    ~ipPart();
    int type_value;
private slots:
    void textIpvCheck(const QString &text);
    void macModify(QString);
private:
    QLineEdit *nextLineEdit;
    QLineEdit *lastLineEdit;
    QString m_keyStr;

signals:
    void sendMsg(QString);

protected:
    void keyPressEvent(QKeyEvent *event);

public:
    void getNextEdit(QLineEdit *nextEdit);
    void getLastEdit(QLineEdit *lastEdit);
};



//ip地址整体类
class ipLine : public QFrame
{
    Q_OBJECT
public:
    ipLine(QWidget *parent = 0,int type = 0);
    ~ipLine(void);

    enum editType
    {
        IPV4,
        IPV6,
        MAC
    };

public:
    ipPart *ipv4Edit1;
    ipPart *ipv4Edit2;
    ipPart *ipv4Edit3;
    ipPart *ipv4Edit4;

    ipPart *macEdit1;
    ipPart *macEdit2;
    ipPart *macEdit3;
    ipPart *macEdit4;
    ipPart *macEdit5;
    ipPart *macEdit6;

    int type_value;

private:
    void ipv4LineInit();
    void macLineInit();

public:
    QString getInfor();
    void setInfor(const QString ipStr);
    void clear();

private slots:
    void disPlayInfor();
};

#endif // IPLINEEDIT_H


三、源文件

#include "ipLineEdit.h"

ipPart::ipPart(QFrame *parent,int type):QLineEdit(parent),type_value(type)
{

    nextLineEdit = NULL;
    lastLineEdit = NULL;
    flag = 1;
    this->setAlignment(Qt::AlignCenter);
    this->setFrame(false);

    if(type_value == 0)
    {
        this->setMaxLength(3);
        this->setValidator(new QIntValidator(0,255,this));
        this->setText(tr("0"));
    }

    if(type_value == 1)
    {

    }

    if(type_value == 2)
    {
        this->setMaxLength(2);
        QRegExp rx;
        rx.setPattern(QString("^[0-9A-Fa-f]+$"));
        this->setValidator(new QRegExpValidator(rx,this));
        this->setText(tr("FF"));
    }
    connect(this,SIGNAL(textChanged(QString)),this,SLOT(textIpvCheck(QString)));
    connect(this,SIGNAL(sendMsg(QString)),this,SLOT(macModify(QString)));
}

ipPart::~ipPart()
{

}

//输入文本检查
void ipPart::textIpvCheck(const QString &text)
{
    if(type_value == 0)
    {
        if(text.size()>1)
        {
            if(text.size()==2)
            {
                int num = text.toInt();
                QString str = QString::number(num,10);
                this->setText(str);
                if(num > 25)
                {
                    //移动焦点位置
                    if(nextLineEdit)
                    {
                        nextLineEdit->setFocus();
                        nextLineEdit->selectAll();
                    }

                }
            }
            else
            {
                int num = text.toInt();
                QString str = QString::number(num,10);
                this->setText(str);
                //移动焦点位置
                if(nextLineEdit)
                {
                    nextLineEdit->setFocus();
                    nextLineEdit->selectAll();
                }
            }
        }
    }

    if(type_value == 1)
    {
        if(text.size() >= 4)
        {
            if(nextLineEdit)
            {
                nextLineEdit->setFocus();
                nextLineEdit->selectAll();
            }
        }
    }

    if(type_value == 2)
    {
        //MAC地址删除操作
        if(text.size() == 1)
        {
            if(this->cursorPosition() == 1)
            {
                QString tmpStr = this->text();
                const char *str = tmpStr.toStdString().c_str();
                char des[3] = {0};
                strcpy(des,str);
                des[1] = 'F';
                des[2] = '\0';
                this->setText(des);
                this->setCursorPosition(1);
            }
            else                                    //this->cursorPosition() == 0
            {
                QString tmpStr = this->text();
                const char *str = tmpStr.toStdString().c_str();
                char des[3] = {0};
                strcpy(des,str);
                des[1] = des[0];
                des[0] = 'F';
                des[2] = '\0';
                this->setText(des);
                this->setCursorPosition(0);
            }
        }
    }
}

//MAC地址修改函数
void ipPart::macModify(QString macStr)
{
    if(type_value == 2)
    {
        if((this->text().size() == 2)  && (this->cursorPosition() != 2))
        {
            if(this->cursorPosition() == 0)
            {
                const char * keyStr = macStr.toStdString().c_str();
                char sour[3] = {0};
                strcpy(sour,keyStr);
                strupr(sour);                   //将sour数组中的小写字母转换为大写
                if(strspn(keyStr,MAC_AREA))      //判断输入是否在规定范围
                {
                    QString tmpStr = this->text();
                    const char *str = tmpStr.toStdString().c_str();
                    char des[3] = {0};
                    strcpy(des,str);
                    des[0] = sour[0];
                    this->setText(des);
                    this->setCursorPosition(1);
                }
            }
            else if(this->cursorPosition() == 1)
            {
                const char * keyStr = macStr.toStdString().c_str();
                char sour[3] = {0};
                strcpy(sour,keyStr);
                strupr(sour);
                if(strspn(keyStr,MAC_AREA))      //判断输入是否在规定范围
                {
                    QString tmpStr = this->text();
                    const char *str = tmpStr.toStdString().c_str();
                    char des[3] = {0};
                    strcpy(des,str);
                    des[1] = sour[0];
                    this->setText(des);
                    if(nextLineEdit)
                    {
                        this->nextLineEdit->setFocus();
                        this->nextLineEdit->setCursorPosition(0);
                    }
                }
            }
        }
    }
}

//键盘事件(只包含小数点"." 删除键 光标移动键(左、右)的事件操作)
void ipPart::keyPressEvent(QKeyEvent *event)
{
    //QMessageBox::information(this,"123",event->text());                                  //获取键盘输入的字符

    if(event->key() == Qt::Key_Period)
    {
        //移动焦点位置
        if(nextLineEdit)
        {
            if(this->text().isEmpty())
            {
                //do nothing
            }
            else if(this->hasSelectedText())
            {
                //do nothing
            }
            else
            {
                nextLineEdit->setFocus();
                nextLineEdit->selectAll();
            }
        }
    }

    if(event->key() == Qt::Key_Backspace)
    {
        if(lastLineEdit && this->cursorPosition() == 0)
        {
            lastLineEdit->setFocus();
            lastLineEdit->setCursorPosition(2);
        }
    }

    if(event->key() == Qt::Key_Left)
    {
        //获取光标在文本框中的位置
        if(this->cursorPosition() == 0)
        {
            if(lastLineEdit)
            {
                lastLineEdit->setFocus();
                //lastLineEdit->selectAll();
            }
        }

    }

    if(event->key() == Qt::Key_Right)
    {
        //当字符串长度与光标的位置相等时证明光标在文本框的最后
        if(this->cursorPosition() == this->text().length())
        {
            if(nextLineEdit)
            {
                nextLineEdit->setFocus();
            }
        }
    }

    if(!(m_keyStr = event->text()).isEmpty())
    {
        emit sendMsg(m_keyStr);
    }

    QLineEdit::keyPressEvent(event);                //其余操作转交给父类的键盘事件处理函数处理
}


//以下两个函数键盘事件使用
void ipPart::getNextEdit(QLineEdit *nextEdit)
{
    nextLineEdit = nextEdit;
}

void ipPart::getLastEdit(QLineEdit *lastEdit)
{
    lastLineEdit = lastEdit;
}

/////////////////////////////////////////////////////////////////////////////////

//ip地址整体类的实现
ipLine::ipLine(QWidget *parent,int type):QFrame(parent),type_value(type)
{
    if(type_value == IPV4)
    {
        ipv4LineInit();
    }

    if(type_value == IPV6)
    {

    }

    if(type_value == MAC)
    {
        macLineInit();
    }
    this->setFixedSize(164,24);
}


ipLine::~ipLine()
{

}


void ipLine::ipv4LineInit()
{
    ipv4Edit1 = new ipPart(this);
    ipv4Edit2 = new ipPart(this);
    ipv4Edit3 = new ipPart(this);
    ipv4Edit4 = new ipPart(this);

    QLabel *ipv4Label1 = new QLabel(tr("."),this);
    QLabel *ipv4Label2 = new QLabel(tr("."),this);
    QLabel *ipv4Label3 = new QLabel(tr("."),this);

    ipv4Edit1->setGeometry(2,1,40,22);
    ipv4Edit2->setGeometry(42,1,40,22);
    ipv4Edit3->setGeometry(82,1,40,22);
    ipv4Edit4->setGeometry(122,1,38,22);

    ipv4Label1->setGeometry(40,1,2,22);
    ipv4Label2->setGeometry(80,1,2,22);
    ipv4Label3->setGeometry(120,1,2,22);

    ipv4Edit1->getNextEdit(ipv4Edit2);
    ipv4Edit2->getNextEdit(ipv4Edit3);
    ipv4Edit3->getNextEdit(ipv4Edit4);
    ipv4Edit4->getNextEdit(NULL);

    ipv4Edit1->getLastEdit(NULL);
    ipv4Edit2->getLastEdit(ipv4Edit1);
    ipv4Edit3->getLastEdit(ipv4Edit2);
    ipv4Edit4->getLastEdit(ipv4Edit3);
}


void ipLine::macLineInit()
{
    macEdit1 = new ipPart(this,2);
    macEdit2 = new ipPart(this,2);
    macEdit3 = new ipPart(this,2);
    macEdit4 = new ipPart(this,2);
    macEdit5 = new ipPart(this,2);
    macEdit6 = new ipPart(this,2);

    QLabel *macLabel1 = new QLabel(tr(":"),this);
    QLabel *macLabel2 = new QLabel(tr(":"),this);
    QLabel *macLabel3 = new QLabel(tr(":"),this);
    QLabel *macLabel4 = new QLabel(tr(":"),this);
    QLabel *macLabel5 = new QLabel(tr(":"),this);

    macEdit1->setGeometry(2,1,27,22);
    macEdit2->setGeometry(29,1,27,22);
    macEdit3->setGeometry(56,1,27,22);
    macEdit4->setGeometry(83,1,27,22);
    macEdit5->setGeometry(110,1,27,22);
    macEdit6->setGeometry(137,1,25,22);

    macLabel1->setGeometry(27,1,4,22);
    macLabel2->setGeometry(54,1,4,22);
    macLabel3->setGeometry(81,1,4,22);
    macLabel4->setGeometry(108,1,4,22);
    macLabel5->setGeometry(135,1,4,22);

    macEdit1->getNextEdit(macEdit2);
    macEdit2->getNextEdit(macEdit3);
    macEdit3->getNextEdit(macEdit4);
    macEdit4->getNextEdit(macEdit5);
    macEdit5->getNextEdit(macEdit6);
    macEdit6->getNextEdit(NULL);

    macEdit1->getLastEdit(NULL);
    macEdit2->getLastEdit(macEdit1);
    macEdit3->getLastEdit(macEdit2);
    macEdit4->getLastEdit(macEdit3);
    macEdit5->getLastEdit(macEdit4);
    macEdit6->getLastEdit(macEdit5);

}

//获取ip地址
QString ipLine::getInfor()
{
    QString ipStr;
    if(type_value == IPV4)
    {
        if(ipv4Edit1->text().isEmpty()&&ipv4Edit2->text().isEmpty()
                &&ipv4Edit3->text().isEmpty()&&ipv4Edit4->text().isEmpty())
        {
            return "ip is empty";
        }

        else if(ipv4Edit1->text() == "0")
        {
            return " the first part of the ipaddress\n can not be 0";
        }

        else
        {
            ipStr = ipv4Edit1->text() + "." + ipv4Edit2->text() +
                    "." + ipv4Edit3->text() + "." + ipv4Edit4->text();
            return ipStr;
        }
    }

    if(type_value == IPV6)
    {

    }

    if(type_value == MAC)
    {
        ipStr = macEdit1->text()+":"+macEdit2->text()+":"+macEdit3->text()+":"+macEdit4->text()+":"+macEdit5->text()+":"+macEdit6->text();
    }
}

//地址设置函数(字符串处理函数,有点繁琐)
void ipLine::setInfor(const QString ipStr)
{
    string ip_str = ipStr.toStdString();
    const char* ch = ip_str.c_str();
    if(type_value == IPV4)
    {
        char c = '.';
        char tem[20] = {0};
        strcpy(tem,ch);

        char *temp = tem;
        int tempNum = strchr(temp,c) - temp;
        char buf[10] = {0};
        strncpy(buf,temp,tempNum);
        buf[tempNum] = '\0';
        ipv4Edit1->setText(buf);

        char *cc = ".";
        temp = strstr(temp,cc)+1;
        tempNum = strchr(temp,c)  - temp;
        strncpy(buf,temp,tempNum);
        buf[tempNum] = '\0';
        ipv4Edit2->setText(buf);

        temp = strstr(temp,cc) + 1;
        tempNum = strchr(temp,c) - temp;
        strncpy(buf,temp,tempNum);
        buf[tempNum] = '\0';
        ipv4Edit3->setText(buf);

        temp = strstr(temp,cc) + 1;
        ipv4Edit4->setText(temp);
    }

    if(type_value == IPV6)
    {

    }

    if(type_value == MAC)
    {
        char c = ':';
        char tem[20] = {0};
        strcpy(tem,ch);

        char *temp = tem;
        int tempNum = strchr(temp,c)-temp;
        char buf[10] = {0};
        strncpy(buf,temp,tempNum);
        buf[tempNum] = '\0';
        strupr(buf);
        macEdit1->setText(buf);

        char *cc = ":";
        temp = strstr(temp,cc)+1;
        tempNum = strchr(temp,c) - temp;
        strncpy(buf,temp,tempNum);
        buf[tempNum] = '\0';
        strupr(buf);
        macEdit2->setText(buf);

        temp = strstr(temp,cc)+1;
        tempNum = strchr(temp,c) - temp;
        strncpy(buf,temp,tempNum);
        buf[tempNum] = '\0';
        strupr(buf);
        macEdit3->setText(buf);

        temp = strstr(temp,cc)+1;
        tempNum = strchr(temp,c) - temp;
        strncpy(buf,temp,tempNum);
        buf[tempNum] = '\0';
        strupr(buf);
        macEdit4->setText(buf);

        temp = strstr(temp,cc)+1;
        tempNum = strchr(temp,c) - temp;
        strncpy(buf,temp,tempNum);
        buf[tempNum] = '\0';
        strupr(buf);
        macEdit5->setText(buf);

        temp = strstr(temp,cc)+1;
        strupr(temp);
        macEdit6->setText(temp);

    }
}

void ipLine::clear()
{
    if(type_value == IPV4)
    {
        ipv4Edit1->clear();
        ipv4Edit2->clear();
        ipv4Edit3->clear();
        ipv4Edit4->clear();
    }

    if(type_value == IPV6)
    {

    }

    if(type_value == MAC)
    {
        macEdit1->clear();
        macEdit2->clear();
        macEdit3->clear();
        macEdit4->clear();
        macEdit5->clear();
        macEdit6->clear();
    }
}

//显示ip地址槽函数
void ipLine::disPlayInfor()
{
    QMessageBox::information(this,"information",getInfor());
}



0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有