[Qt] IP地址输入框实现

时间:2023-03-08 16:35:05

封装了一个ip地址的输入框。网络上下载了份代码,找不到哪里的了。经过修改之后,尽力让它的行为和windows的IP地址输入框的行为看起来像些。代码如下:

//ipaddredit.h

#ifndef IPADDREDIT_H
#define IPADDREDIT_H #include <QWidget> 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 <QRegExpValidator>
#include <QLabel> #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 <QLineEdit> 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 <QIntValidator>
#include <QKeyEvent> #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();
}
}
}