#ifndef QCIPLINEEDIT_H
#define QCIPLINEEDIT_H
#include <QObject>
#include <QLineEdit>
#include <QIntValidator>
#include <HBoxLayout>
#include <QLabel>
#include <QFont>
#include <QKeyEvent>
#include <QHostAddress>
class QCIPLineEdit : public QLineEdit
{
Q_OBJECT
public:
QCIPLineEdit(QWidget *parent=0);
~QCIPLineEdit();
virtual bool eventFilter(QObject *object,QEvent *event);
void getIP(QString &addr);//获取控件文本
void setIP(const QString addr);//设置控件文本
protected:
void init();
void initForEdit(QLineEdit *edit);
QLineEdit* nextedit(QLineEdit *currentedit);
bool isEdit(QObject* object);
public slots:
void editTextChanged(const QString& text);
private:
QLineEdit* l1;
QLineEdit* l2;
QLineEdit* l3;
QLineEdit* l4;
QLabel* lb1;
QLabel* lb2;
QLabel* lb3;
QHBoxLayout *m_layout;
};
#endif
上述为头文件
#include "qciplineedit.h"
#include <QDebug>
#include <QPainter>
#include <QApplication>
#include <QStyle>
#include <QStyleOptionButton>
#include <QKeyEvent>
#include <QShortcutEvent>
#include <QRegExpValidator>
#include <QRegExp>
#include <QClipboard>
#include <QStyleOptionFrame>
QCIPLineEdit::QCIPLineEdit(QWidget *parent)
{
this->setParent(parent);
m_layout = new QHBoxLayout(this);
init();
this->setStyleSheet("background-color:white;");
this->setMaximumHeight(25);
this->setMaximumWidth(165);
}
QCIPLineEdit::~QCIPLineEdit()
{
}
void QCIPLineEdit::init()
{
l1 = new QLineEdit(this);
l2 = new QLineEdit(this);
l3 = new QLineEdit(this);
l4 = new QLineEdit(this);
initForEdit(l1);
initForEdit(l2);
initForEdit(l3);
initForEdit(l4);
lb1 = new QLabel(".",this);
lb2 = new QLabel(".",this);
lb3 = new QLabel(".",this);
if(m_layout)
{
m_layout->setSpacing(0);
m_layout->setContentsMargins(1,0,1,0);
m_layout->addWidget(l1,1);
m_layout->addWidget(lb1);
m_layout->addWidget(l2,1);
m_layout->addWidget(lb2);
m_layout->addWidget(l3,1);
m_layout->addWidget(lb3);
m_layout->addWidget(l4,1);
}
}
void QCIPLineEdit::initForEdit(QLineEdit*edit)
{
edit->setFrame(false);
edit->setAlignment(Qt::AlignCenter);
edit->installEventFilter(this);
QRegExpValidator *v = new QRegExpValidator(QRegExp("^(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$"),this);
edit->setValidator(v);
connect(edit,SIGNAL(textChanged(const QString&)),this,SLOT(editTextChanged(const QString&)));
}
QLineEdit* QCIPLineEdit::nextEdit(QLineEdit *currentedit)
{
if(currentedit==l1)
{
return l2;
}
else if(currentedit==l2)
{
return l3;
}
else if(currentedit==l3)
{
return l4;
}
else
return NULL;
}
bool QCIPLineEDit::isEdit(QObject *object)
{
return (object==l1||object==l2||object==l3||object==l4);
}
void QCIPLineEdit::getIP(QString& addr)
{
QString s1 = l1->text().isEmpty() ? "0" : l1->text();
QString s2 = l2->text().isEmpty() ? "0" : l2->text();
QString s3 = l3->text().isEmpty() ? "0" : l3->text();
QString s4 = l4->text().isEmpty() ? "0" : l4->text();
addr = QSTring("%1.%2.%3.%4").arg(s1).arg(s2).arg(s3).arg(s4);
}
void QCIPLineEdit::setIP(const QString addr)
{
QRegExp reg("^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}^(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$");
if(!reg.exactMatch(addr))
{
return;
}
QStringList ips = addr.split(".");
l1->setText(ips.at(0));
l2->setText(ips.at(1));
l3->setText(ips.at(2));
l4->setText(ips.at(3));
this->update();
}
void QCIPLineEdit::editTextChanged(const QSTring& text)
{
QLineEdit *curedit = qobject_cast<QLineEdit*>(sender());
if(text.size()==3)
{
QLineEdit *next = nextEdit(curedit);
if(next)
{
next->setFocus();
next->setlectAll();
}
}
}
bool QCIPLineEdit::eventFilter(QObject *object,QEvent *ev)
{
if(isEdit)
{
if(ev->type == QEvent::KeyPress)
{
QKeyEvent *keyevent = static_cast<QKeyEvent*>(ev);
if(keyevent->key() == Qt::Key_Period)
{
QLineEdit *next = nextEdit(qobject_cast<QLineEdit*>(object));
if(next)
{
next->setFocus();
next->setlectAll();
return true;
}
}
else if(keyevent->matches(QKeySequence::Paste))
{
QString clip = QApplication::clipboard()->text(QClipboard::Clipboard);
if(clip.split(".").size() == 4)
{
setIP(clip);
}
}
else{}
}
}
return QObject::eventFilter(object,ev);
}
上述为源文件。
只需在UI界面中将QLineEdit控件提升为QCIPLineEdit类即可。