码迷,mamicode.com
首页 > 其他好文 > 详细

Qt版IP地址控件

时间:2015-08-04 13:40:24      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:

头文件

#ifndef IPADDRESS_H
#define IPADDRESS_H

#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QHBoxLayout>

class ipAddress : public QWidget
{
    Q_OBJECT
public:
    explicit ipAddress(QWidget *parent = 0);

    bool validCheck();
    bool subCheck(QLineEdit*);
    QString getIPAddress();
    void textClear();

signals:

public slots:

public:

    QLabel *label1;
    QLabel *label2;
    QLabel *label3;

    QHBoxLayout *mainLayout;

    QLineEdit *number1;
    QLineEdit *number2;
    QLineEdit *number3;
    QLineEdit *number4;
};

#endif // IPADDRESS_H

源文件

#include "ipaddress.h"

ipAddress::ipAddress(QWidget *parent) :
    QWidget(parent)
{
//    布局
    mainLayout=new QHBoxLayout(this);
    label1=new QLabel(".",this);
    label2=new QLabel(".",this);
    label3=new QLabel(".",this);

    number1=new QLineEdit(this);
    number2=new QLineEdit(this);
    number3=new QLineEdit(this);
    number4=new QLineEdit(this);

    mainLayout->addWidget(number1);
    mainLayout->addWidget(label1);
    mainLayout->addWidget(number2);
    mainLayout->addWidget(label2);
    mainLayout->addWidget(number3);
    mainLayout->addWidget(label3);
    mainLayout->addWidget(number4);

    setLayout(mainLayout);

}

bool ipAddress::validCheck()
{//判断IP地址是否合理 
    return subCheck(number1) && subCheck(number2) && subCheck(number3) && subCheck(number4);
}

bool ipAddress::subCheck(QLineEdit *number)
{
    QString str=number->text();
    bool ok=true;
    int tmp=str.toInt(&ok,10);//合理性判断
    if(!ok)
        return false;

    if(tmp<0 || tmp>255)
        return false;
    return true;
}

QString ipAddress::getIPAddress()//get the IP address
{
    QString str;

    str+=number1->text();
    str+=".";
    str+=number2->text();
    str+=".";
    str+=number3->text();
    str+=".";
    str+=number4->text();

    return str;
}

void ipAddress::textClear()
{
    number1->clear();
    number2->clear();
    number3->clear();
    number4->clear();
}


Qt版IP地址控件

标签:

原文地址:http://blog.csdn.net/walker19900515/article/details/47274337

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!