QT 自定义IP输入框(自己重写一部分,增加一部分功能)

QT没有提供一个完整的IP地址控件,

1. 可以使用QLineEdit简单的实现
    QRegExp regExp("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)");
    QRegExpValidator 
*pValidator = new QRegExpValidator(regExp, this);
    QLineEdit 
*lineEdit = new QLineEdit(this);
    lineEdit
->setValidator(new QRegExpValidator(pValidator, this));
    lineEdit
->setInputMask("000.000.000.000;");
但是上面的输入时没有window的IP地址控件好用。所以自己就用4个QLineEdit封装了一个,用起来好多了,
下面是代码:
// MyIpPartLineEdit.h
#pragma once
#include <QLineEdit>


class QWidget;
class QFocusEvent;
class QKeyEvent;


class MyIpPartLineEdit : public QLineEdit
{
Q_OBJECT
public:
MyIpPartLineEdit(QWidget *parent = 0);
~MyIpPartLineEdit(void);


void set_nexttab_edit(QLineEdit *nexttab) { next_tab_ = nexttab; }
void set_beforetab_del(QLineEdit *beforetab)  { before_tab_ = beforetab;}


protected:
virtual void focusInEvent(QFocusEvent *e);
virtual    void keyPressEvent(QKeyEvent *event);  


private slots:
void text_edited(const QString& text);



private:
QLineEdit *next_tab_;
QLineEdit *before_tab_;
};
// MyIpPartLineEdit.cpp
#include "MyIpPartLineEdit.h"
#include 
<QIntValidator>
#include 
<QKeyEvent>

MyIpPartLineEdit::MyIpPartLineEdit(QWidget 
*parent/* = 0*/)
: QLineEdit(parent)
{
    next_tab_ 
= NULL;

    
this->setMaxLength(3);
    
this->setFrame(false);
    
this->setAlignment(Qt::AlignCenter);

    QValidator 
*validator = new QIntValidator(0255this);
    
this->setValidator(validator);

    connect(
this, SIGNAL(textEdited(const QString&)), this, SLOT(text_edited(const QString&)));
}

MyIpPartLineEdit::
~MyIpPartLineEdit(void)
{
}

void MyIpPartLineEdit::focusInEvent(QFocusEvent *e)
{
    
this->selectAll();
    QLineEdit::focusInEvent(e);
}

void MyIpPartLineEdit::keyPressEvent(QKeyEvent *event)
{
   
if (event->key() == Qt::Key_Period)
{
if (next_tab_)
{
next_tab_->setFocus();
next_tab_->selectAll();
}
}
//ccg 使删除完当前文本框以后跳到前一个文本框
if (event->key() == Qt::Key_Backspace)
{

while ((this->text().size() == 0)&&(this->objectName() != "ip_part1_"))
{
if (before_tab_)
{
before_tab_->setFocus();
return;
}
}
}


//CCG左键跳到前一个编辑框最右位置
if(event->key() == Qt::Key_Left)
{
if ((this->cursorPosition() ==0)&&(this->objectName() != "ip_part1_"))
{
if (before_tab_)
{
before_tab_->setFocus();
before_tab_->setCursorPosition(this->text().size());
return;
}
}
}


if(event->key() == Qt::Key_Right)
{
if ((this->cursorPosition() ==this->text().size())&&(this->objectName() != "ip_part4_"))
{
if (next_tab_)
{
next_tab_->setFocus();
next_tab_->setCursorPosition(0);
return;
}
}
}
QLineEdit::keyPressEvent(event);
}

void MyIpPartLineEdit::text_edited(const QString& text)
{
    QIntValidator v(
0255this);
    QString ipaddr 
= text;
    
int pos = 0;
    QValidator::State state 
= v.validate(ipaddr, pos); 
    
if (state == QValidator::Acceptable)
    {
        
if (ipaddr.size() > 1)
        {
            
if (ipaddr.size() == 2)
            {
                
int ipnum = ipaddr.toInt();
                
                
if (ipnum > 25)
                {
                    
if (next_tab_)
                    {
                        next_tab_
->setFocus();
                        next_tab_
->selectAll();
                    }    
                }
            }
            
else
            {
                
if (next_tab_)
                {
                    next_tab_
->setFocus();
                    next_tab_
->selectAll();
                }    
            }
        }
    }
}

// MyIpAddrEdit.h
#pragma once

#include 
<QWidget>

class QLineEdit;
class QLabel;
class MyIpPartLineEdit;

class MyIpAddrEdit : public QWidget
{
    Q_OBJECT
public:
    MyIpAddrEdit(QWidget
* pParent = 0);
    
~MyIpAddrEdit();

    
void settext(const QString &text);
    QString text();
    
void setStyleSheet(const QString &styleSheet);

signals:
    
void textchanged(const QString& text);
    
void textedited(const QString &text);

private slots:
    
void textchangedslot(const QString& text);
    
void texteditedslot(const QString &text);

private:
    MyIpPartLineEdit 
*ip_part1_;
    MyIpPartLineEdit 
*ip_part2_;
    MyIpPartLineEdit 
*ip_part3_;
    
 MyIpPartLineEdit *ip_part4_;

    QLabel 
*labeldot1_;
    QLabel 
*labeldot2_;    
    QLabel 
*labeldot3_;
};

// MyIpAddrEdit.cpp
#include "MyIpAddrEdit.h"
#include 
<QRegExpValidator>
#include 
<QLabel>
#include 
"MyIpPartLineEdit.h"

MyIpAddrEdit::MyIpAddrEdit(QWidget
* pParent /* = 0 */)
: QWidget(pParent)
{
    ip_part1_ 
= new MyIpPartLineEdit(this);
    ip_part2_ 
= new MyIpPartLineEdit(this);
    ip_part3_ 
= new MyIpPartLineEdit(this);
    ip_part4_ 
= new MyIpPartLineEdit(this);

    labeldot1_ 
= new QLabel(this);
    labeldot2_ 
= new QLabel(this);
    labeldot3_ 
= new QLabel(this);

    ip_part1_
->setGeometry(QRect(003020));
    ip_part2_
->setGeometry(QRect(3003020));
    ip_part3_
->setGeometry(QRect(6003020));
    ip_part4_
->setGeometry(QRect(9003020));

    labeldot1_
->setText(" .");
    labeldot1_
->setGeometry(QRect(271618));
    labeldot1_
->setAlignment(Qt::AlignCenter);

    labeldot2_
->setText(" .");
    labeldot2_
->setGeometry(QRect(571618));
    labeldot2_
->setAlignment(Qt::AlignCenter);

    labeldot3_
->setText(" .");
    labeldot3_
->setGeometry(QRect(871618));
    labeldot3_
->setAlignment(Qt::AlignCenter);

    QWidget::setTabOrder(ip_part1_, ip_part2_);
    QWidget::setTabOrder(ip_part2_, ip_part3_);
    QWidget::setTabOrder(ip_part3_, ip_part4_);
    
//ccg设置后置索引顺序
ip_part1_->set_nexttab_edit(ip_part2_);
ip_part2_->set_nexttab_edit(ip_part3_);
ip_part3_->set_nexttab_edit(ip_part4_);

//ccg设置前置索引顺序

ip_part4_->set_beforetab_del(ip_part3_);
ip_part3_->set_beforetab_del(ip_part2_);
ip_part2_->set_beforetab_del(ip_part1_);


    connect(ip_part1_, SIGNAL(textChanged(
const QString&)), this, SLOT(textchangedslot(const QString&)));
    connect(ip_part2_, SIGNAL(textChanged(
const QString&)), this, SLOT(textchangedslot(const QString&)));
    connect(ip_part3_, SIGNAL(textChanged(
const QString&)), this, SLOT(textchangedslot(const QString&)));
    connect(ip_part4_, SIGNAL(textChanged(
const QString&)), this, SLOT(textchangedslot(const QString&)));

    connect(ip_part1_, SIGNAL(textEdited (
const QString&)), this, SLOT(texteditedslot(const QString&)));
    connect(ip_part2_, SIGNAL(textEdited (
const QString&)), this, SLOT(texteditedslot(const QString&)));
    connect(ip_part3_, SIGNAL(textEdited (
const QString&)), this, SLOT(texteditedslot(const QString&)));
    connect(ip_part4_, SIGNAL(textEdited (
const QString&)), this, SLOT(texteditedslot(const QString&)));
    
}

MyIpAddrEdit::
~MyIpAddrEdit()
{

}

void MyIpAddrEdit::textchangedslot(const QString& /*text*/)
{
    QString ippart1, ippart2, ippart3, ippart4;
    ippart1 
= ip_part1_->text();
    ippart2 
= ip_part2_->text();
    ippart3 
= ip_part3_->text();
    ippart4 
= ip_part4_->text();

    QString ipaddr 
= QString("%1.%2.%3.%4")
                     .arg(ippart1)
                     .arg(ippart2)
                     .arg(ippart3)
                     .arg(ippart4);

    emit textchanged(ipaddr);
}

void MyIpAddrEdit::texteditedslot(const QString &/*text*/)
{
    QString ippart1, ippart2, ippart3, ippart4;
    ippart1 
= ip_part1_->text();
    ippart2 
= ip_part2_->text();
    ippart3 
= ip_part3_->text();
    ippart4 
= ip_part4_->text();

    QString ipaddr 
= QString("%1.%2.%3.%4")
        .arg(ippart1)
        .arg(ippart2)
        .arg(ippart3)
        .arg(ippart4);

    emit textedited(ipaddr);
}

void MyIpAddrEdit::settext(const QString &text)
{
    QString ippart1, ippart2, ippart3, ippart4;
    QString qstring_validate 
= text;

    
// IP地址验证
    QRegExp regexp("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)");
    QRegExpValidator regexp_validator(regexp, 
this);
    
int nPos = 0;
    QValidator::State state 
= regexp_validator.validate(qstring_validate, nPos);
    
// IP合法
    if (state == QValidator::Acceptable)
    {
        QStringList ippartlist 
= text.split(".");
    
        
int strcount = ippartlist.size();
        
int index = 0;
        
if (index < strcount)
        {
            ippart1 
= ippartlist.at(index);
        }
        
if (++index < strcount)
        {
            ippart2 
= ippartlist.at(index);
        }
        
if (++index < strcount)
        {
            ippart3 
= ippartlist.at(index);
        }
        
if (++index < strcount)
        {
            ippart4 
= ippartlist.at(index);
        }
    }

    ip_part1_
->setText(ippart1);
    ip_part2_
->setText(ippart2);
    ip_part3_
->setText(ippart3);
    ip_part4_
->setText(ippart4);
}

QString MyIpAddrEdit::text()
{
    QString ippart1, ippart2, ippart3, ippart4;
    ippart1 
= ip_part1_->text();
    ippart2 
= ip_part2_->text();
    ippart3 
= ip_part3_->text();
    ippart4 
= ip_part4_->text();

    
return QString("%1.%2.%3.%4")
        .arg(ippart1)
        .arg(ippart2)
        .arg(ippart3)
        .arg(ippart4);
}

void MyIpAddrEdit::setStyleSheet(const QString &styleSheet)
{
    ip_part1_
->setStyleSheet(styleSheet);
    ip_part2_
->setStyleSheet(styleSheet);
    ip_part3_
->setStyleSheet(styleSheet);
    ip_part4_
->setStyleSheet(styleSheet);
}

下面是调用方法
#include <QApplication>
#include 
<QDialog>
#include 
"MyIpAddrEdit.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QDialog 
*my_dialog = new QDialog;
    MyIpAddrEdit 
*ipAddr = new MyIpAddrEdit(my_dialog);
    ipAddr
->settext("127.0.0.1");
    my_dialog
->show();

    
return app.exec();
}


PS:
如果是在QT designer中拖的控件LINEEDIT,则可以右键提升为MyIpAddrEdit类,在需要调用的界面#include "MyIpAddrEdit.h",既可以使用了。
展开阅读全文

没有更多推荐了,返回首页