[Qt] IP 주소 입력란 구현

15685 단어 qt
IP 주소의 입력 상자를 봉인했습니다.인터넷상에서 코드를 다운로드했는데 어디의 코드를 찾지 못했다.수정을 거친 후, 윈도우즈의 IP 주소 입력 상자와 비슷하게 보이도록 최선을 다합니다.코드는 다음과 같습니다.
//ipaddredit.h
#ifndef IPADDREDIT_H
#define IPADDREDIT_H

#include 

class QLineEdit;
class QLabel;
class CIpPartLineEdit;

class CIpAddrEdit : public QWidget
{
    Q_OBJECT

public:
    CIpAddrEdit(QWidget *parent = 0);
    ~CIpAddrEdit();

    void m_vSetText(const QString &text);
    QString m_strGetText();
    void m_vSetStyleSheet(const QString &styleSheet);

signals:
    void sigTextChanged(const QString &text);
    void sigTextEdited(const QString &text);

private slots:
    void slot_text_Changed(const QString &text);
    void slot_text_Edited(const QString &text);

private:
    CIpPartLineEdit * m_pIpPart1;
    CIpPartLineEdit * m_pIpPart2;
    CIpPartLineEdit * m_pIpPart3;
    CIpPartLineEdit * m_pIpPart4;

    QLabel * m_pDot1;
    QLabel * m_pDot2;
    QLabel * m_pDot3;
};

#endif // IPADDREDIT_H

//ipaddredit.cpp
#include 
#include 

#include "ippartlineedit.h"
#include "ipaddredit.h"

CIpAddrEdit::CIpAddrEdit(QWidget *parent) :
    QWidget(parent)
{
    m_pIpPart1 = new CIpPartLineEdit(this);
    m_pIpPart2 = new CIpPartLineEdit(this);
    m_pIpPart3 = new CIpPartLineEdit(this);
    m_pIpPart4 = new CIpPartLineEdit(this);

    m_pDot1 = new QLabel(this);
    m_pDot2 = new QLabel(this);
    m_pDot3 = new QLabel(this);

    m_pIpPart1->setGeometry(QRect(0, 0, 42, 27));
    m_pIpPart2->setGeometry(QRect(70, 0, 42, 27));
    m_pIpPart3->setGeometry(QRect(140, 0, 42, 27));
    m_pIpPart4->setGeometry(QRect(210, 0, 42, 27));

    m_pDot1->setText("-");
    m_pDot1->setGeometry(QRect(48, 5, 16, 16));
    m_pDot1->setAlignment(Qt::AlignCenter);

    m_pDot2->setText("-");
    m_pDot2->setGeometry(QRect(118, 5, 16, 16));
    m_pDot2->setAlignment(Qt::AlignCenter);

    m_pDot3->setText("-");
    m_pDot3->setGeometry(QRect(188, 5, 16, 16));
    m_pDot3->setAlignment(Qt::AlignCenter);

    QWidget::setTabOrder(m_pIpPart1, m_pIpPart2);
    QWidget::setTabOrder(m_pIpPart2, m_pIpPart3);
    QWidget::setTabOrder(m_pIpPart3, m_pIpPart4);

    m_pIpPart1->m_vSetNextEdit(m_pIpPart2);
    m_pIpPart2->m_vSetNextEdit(m_pIpPart3);
    m_pIpPart3->m_vSetNextEdit(m_pIpPart4);

    m_pIpPart2->m_vSetPreEdit(m_pIpPart1);
    m_pIpPart3->m_vSetPreEdit(m_pIpPart2);
    m_pIpPart4->m_vSetPreEdit(m_pIpPart3);

    connect(m_pIpPart1, SIGNAL(textChanged(const QString&)), this, SLOT(slot_text_Changed(const QString&)));
    connect(m_pIpPart2, SIGNAL(textChanged(const QString&)), this, SLOT(slot_text_Changed(const QString&)));
    connect(m_pIpPart3, SIGNAL(textChanged(const QString&)), this, SLOT(slot_text_Changed(const QString&)));
    connect(m_pIpPart4, SIGNAL(textChanged(const QString&)), this, SLOT(slot_text_Changed(const QString&)));

    connect(m_pIpPart1, SIGNAL(textEdited(const QString&)), this, SLOT(slot_text_Edited(const QString&)));
    connect(m_pIpPart2, SIGNAL(textEdited(const QString&)), this, SLOT(slot_text_Edited(const QString&)));
    connect(m_pIpPart3, SIGNAL(textEdited(const QString&)), this, SLOT(slot_text_Edited(const QString&)));
    connect(m_pIpPart4, SIGNAL(textEdited(const QString&)), this, SLOT(slot_text_Edited(const QString&)));
}

CIpAddrEdit::~CIpAddrEdit()
{

}

void CIpAddrEdit::slot_text_Changed(const QString &/*text*/)
{
    QString strIpPart1 = m_pIpPart1->text();
    QString strIpPart2 = m_pIpPart1->text();
    QString strIpPart3 = m_pIpPart1->text();
    QString strIpPart4 = m_pIpPart1->text();

    QString strIpAddr = QString("%1.%2.%3.%4")
            .arg(strIpPart1)
            .arg(strIpPart2)
            .arg(strIpPart3)
            .arg(strIpPart4);

    emit sigTextChanged(strIpAddr);
}

void CIpAddrEdit::slot_text_Edited(const QString &/*text*/)
{
    QString strIpPart1 = m_pIpPart1->text();
    QString strIpPart2 = m_pIpPart1->text();
    QString strIpPart3 = m_pIpPart1->text();
    QString strIpPart4 = m_pIpPart1->text();

    QString strIpAddr = QString("%1.%2.%3.%4")
            .arg(strIpPart1)
            .arg(strIpPart2)
            .arg(strIpPart3)
            .arg(strIpPart4);

    emit sigTextEdited(strIpAddr);
}

void CIpAddrEdit::m_vSetText(const QString &text)
{
    QString strIpAddr = text;

    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 iPos = 0;

    QValidator::State state = regexp_validator.validate(strIpAddr, iPos);
    if(state != QValidator::Acceptable)
    {
        return ;
    }

    QStringList ipAddrList = text.split(".");
    //int iCount = ipAddrList.size();

    QString strIpPart1 = ipAddrList.at(0);
    QString strIpPart2 = ipAddrList.at(1);
    QString strIpPart3 = ipAddrList.at(2);
    QString strIpPart4 = ipAddrList.at(3);

    m_pIpPart1->setText(strIpPart1);
    m_pIpPart2->setText(strIpPart2);
    m_pIpPart3->setText(strIpPart3);
    m_pIpPart4->setText(strIpPart4);
}

QString CIpAddrEdit::m_strGetText()
{
    QString strIpPart1 = m_pIpPart1->text();
    QString strIpPart2 = m_pIpPart2->text();
    QString strIpPart3 = m_pIpPart3->text();
    QString strIpPart4 = m_pIpPart4->text();

    return QString("%1.%2.%3.%4")
            .arg(strIpPart1)
            .arg(strIpPart2)
            .arg(strIpPart3)
            .arg(strIpPart4);
}

void CIpAddrEdit::m_vSetStyleSheet(const QString &styleSheet)
{
    m_pIpPart1->setStyleSheet(styleSheet);
    m_pIpPart2->setStyleSheet(styleSheet);
    m_pIpPart3->setStyleSheet(styleSheet);
    m_pIpPart4->setStyleSheet(styleSheet);
}

//ippartlineedit.h
#ifndef IPPARTLINEEDIT_H
#define IPPARTLINEEDIT_H

#include 

class QWidget;
class QFocusEvent;
class QKeyEvent;

class CIpPartLineEdit : public QLineEdit
{
    Q_OBJECT

public:
    CIpPartLineEdit(QWidget *parent = 0);
    ~CIpPartLineEdit();

    void m_vSetPreEdit(QLineEdit *preEdit);
    void m_vSetNextEdit(QLineEdit *nextEdit);

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

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

private:
    QLineEdit *m_preEdit;
    QLineEdit *m_nextEdit;
};

#endif // IPPARTLINEEDIT_H

//ippartlineedit.cpp
#include 
#include 

#include "ippartlineedit.h"

CIpPartLineEdit::CIpPartLineEdit(QWidget *parent)
    : QLineEdit(parent)
{
    m_preEdit = NULL;
    m_nextEdit = NULL;

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

    QValidator *validator = new QIntValidator(0, 255, this);
    this->setValidator(validator);

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

CIpPartLineEdit::~CIpPartLineEdit()
{

}

void CIpPartLineEdit::m_vSetPreEdit(QLineEdit *preEdit)
{
    m_preEdit = preEdit;
}

void CIpPartLineEdit::m_vSetNextEdit(QLineEdit *nextEdit)
{
    m_nextEdit = nextEdit;
}

void CIpPartLineEdit::focusInEvent(QFocusEvent *event)
{
    this->selectAll();
    QLineEdit::focusInEvent(event);
}

void CIpPartLineEdit::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_Period)
    {
        if(m_nextEdit && 
           (this->text().size()!=0) &&
           (!this->hasSelectedText()))
        {
            m_nextEdit->setFocus();
            m_nextEdit->selectAll();
        }
    }
    if(event->key() == Qt::Key_Backspace)
    {
        if(m_preEdit &&
           (this->text().size() == 0))
        {
            m_preEdit->setFocus();
            m_preEdit->setCursorPosition( m_preEdit->text().size() );
        }
    }
    QLineEdit::keyPressEvent(event);
}

void CIpPartLineEdit::slot_text_Edited(const QString & text)
{
    QIntValidator v(0, 255, this);
    QString ipAddr = text;
    int iPos = 0;

    QValidator::State state = v.validate(ipAddr, iPos);
    if(state != QValidator::Acceptable)
    {
        return ;
    }
    if(ipAddr.size() <= 1)
    {
        return ;
    }

    if(ipAddr.size() == 2)
    {
        int iIpNum = ipAddr.toInt();
        if(iIpNum > 25)
        {
            if(m_nextEdit)
            {
                m_nextEdit->setFocus();
                m_nextEdit->selectAll();
            }
        }
    }
    else
    {
        if(m_nextEdit)
        {
            m_nextEdit->setFocus();
            m_nextEdit->selectAll();
        }
    }
}

좋은 웹페이지 즐겨찾기