原文出处:【上善若静水】
1.通过自定义类方式实现IP4地址范围限制输入:
//--------------------TLineEditIP.h--------------------
#ifndef TLINEEDITIP_H
#define TLINEEDITIP_H #include <QLineEdit>
#include <QEvent> class TLineEditIP : public QLineEdit
{
Q_OBJECT #define QTUTL_IP_SIZE 4 signals :
void signalTextChanged(QLineEdit* pEdit); public:
TLineEditIP(QWidget *parent = );
~TLineEditIP(); void setText(const QString &strIP);
QString text() const; public slots:
void slotTextChanged(QLineEdit* pEdit); protected:
void paintEvent(QPaintEvent *event);
bool eventFilter(QObject *obj, QEvent *ev); int getIndex(QLineEdit *pEdit);
bool isTextValid(const QString &strIP); private:
static QString getIPItemStr(unsigned char item); private:
QLineEdit *m_lineEidt[];
}; #endif // TLINEEDITIP_H
//--------------------TLineEditIP.cpp--------------------
#include "TLineEditIP.h"
#include <QRegExpValidator>
#include <QPainter>
#include <QHBoxLayout>
#include <QKeyEvent>
#include <QMessageBox>
#include <QDebug> TLineEditIP::TLineEditIP(QWidget *parent)
:QLineEdit(parent)
{
QRegExp rx("(2[0-5]{2}|2[0-4][0-9]|1?[0-9]{1,2})");
QHBoxLayout *pHBox = new QHBoxLayout(this);
pHBox->setSpacing();
pHBox->setContentsMargins(,,,);
for (int i = ; i < ; i++)
{
m_lineEidt[i] = new QLineEdit(this);
m_lineEidt[i]->setFrame(false);
m_lineEidt[i]->setMaxLength();
m_lineEidt[i]->setTextMargins(,,,);
m_lineEidt[i]->setAlignment(Qt::AlignCenter);
m_lineEidt[i]->installEventFilter(this);
m_lineEidt[i]->setValidator(new QRegExpValidator(rx, this));
m_lineEidt[i]->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
m_lineEidt[i]->setStyleSheet("QLineEdit{font: 12px \"Microsoft YaHei\";color:rgb(119,119,119);background-color:rgb(255,255,255);border:none;}");
m_lineEidt[i]->setAttribute(Qt::WA_MacShowFocusRect, );
m_lineEidt[i]->setContextMenuPolicy(Qt::NoContextMenu);
pHBox->addWidget(m_lineEidt[i]);
}
this->setReadOnly(true);
this->setContextMenuPolicy(Qt::NoContextMenu);
installEventFilter(this);
connect(this, SIGNAL(signalTextChanged(QLineEdit*)),
this, SLOT(slotTextChanged(QLineEdit*)),
Qt::QueuedConnection);
} TLineEditIP::~TLineEditIP()
{ } void TLineEditIP::paintEvent(QPaintEvent *event)
{
QLineEdit::paintEvent(event);
QPainter painter(this);
QBrush brush;
brush.setStyle(Qt::BrushStyle::SolidPattern);
brush.setColor(Qt::black);
painter.setBrush(brush); int width = ;
for (int i = ; i < ; i++)
{
width += m_lineEidt[i]->width() + (i == ? : );//布局的间隔
painter.drawEllipse(width, height() / - , , );
}
} QString TLineEditIP::getIPItemStr(unsigned char item)
{
return QString("%1\n").arg(item);
} void TLineEditIP::slotTextChanged(QLineEdit* pEdit)
{
for (unsigned int i = ; i != QTUTL_IP_SIZE; ++i)
{
if (pEdit == m_lineEidt[i])
{
if (pEdit->text().size() == getIPItemStr(UCHAR_MAX).size() &&
pEdit->text().size() == pEdit->cursorPosition())
{
// auto-move to next item
if (i + != QTUTL_IP_SIZE)
{
m_lineEidt[i + ]->setFocus();
m_lineEidt[i + ]->selectAll();
}
}
}
}
} int TLineEditIP::getIndex(QLineEdit *pEdit)
{
int index = -;
for (int i = ; i < ; i++)
{
if (pEdit == m_lineEidt[i])
index = i;
}
return index;
} bool TLineEditIP::eventFilter(QObject *obj, QEvent *event)
{
bool bRes = QLineEdit::eventFilter(obj, event);
if (event->type() == QEvent::Show)
{
if (obj == this)
{
m_lineEidt[]->setFocus();
m_lineEidt[]->setCursorPosition(m_lineEidt[]->text().size());
m_lineEidt[]->selectAll();
}
}
else if (event->type() == QEvent::KeyPress)
{
QKeyEvent* pEvent = dynamic_cast<QKeyEvent*>(event);
if (pEvent)
{
for (unsigned int i = ; i != QTUTL_IP_SIZE; ++i)
{
QLineEdit* pEdit = m_lineEidt[i];
if (pEdit == obj)
{
switch (pEvent->key())
{
case Qt::Key_Left:
if (pEdit->cursorPosition() == )
{
// user wants to move to previous item
m_lineEidt[i > ? i - : QTUTL_IP_SIZE - ]->setFocus();
m_lineEidt[i > ? i - : QTUTL_IP_SIZE - ]->setCursorPosition(m_lineEidt[i > ? i - : QTUTL_IP_SIZE - ]->text().size());
m_lineEidt[i > ? i - : QTUTL_IP_SIZE - ]->selectAll();
}
break;
case Qt::Key_Right:
if (pEdit->text().isEmpty() ||(pEdit->text().size() == pEdit->cursorPosition()))
{
// user wants to move to next item
m_lineEidt[i + != QTUTL_IP_SIZE ? i + : ]->setFocus();
m_lineEidt[i + != QTUTL_IP_SIZE ? i + : ]->setCursorPosition();
}
break;
case Qt::Key_Return:
case Qt::Key_Tab:
{
m_lineEidt[i + != QTUTL_IP_SIZE ? i + : ]->setFocus();
m_lineEidt[i + != QTUTL_IP_SIZE ? i + : ]->setCursorPosition();
m_lineEidt[i + != QTUTL_IP_SIZE ? i + : ]->selectAll();
return true;
}
case Qt::Key_Backspace:
{
QString strText = pEdit->text();
if (!strText.length() || strText.length() && !pEdit->cursorPosition())
{
int index = getIndex(pEdit);
if (index != - && index != )
{
m_lineEidt[index - ]->setFocus();
int length = m_lineEidt[index - ]->text().length();
m_lineEidt[index - ]->setCursorPosition(length ? length : );
}
}
return QLineEdit::eventFilter(obj, event);
}
default:
emit signalTextChanged(pEdit);
}
break;
}
}
}
}
return bRes;
}
void TLineEditIP::setText(const QString &strIP)
{
if (!isTextValid(strIP))
{
QMessageBox::warning(this, "Attention", "Your IP Address is Invalid!", QMessageBox::StandardButton::Ok);
return;
}
else
{
int i = ;
QStringList ipList = strIP.split(".");
foreach (const QString &ip, ipList)
{
m_lineEidt[i]->setText(ip);
i++;
}
}
m_lineEidt[]->selectAll();
} bool TLineEditIP::isTextValid(const QString &strIP)
{
QRegExp rx2("\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");
if (!rx2.exactMatch(strIP))
return false;
return true;
} QString TLineEditIP::text() const
{
return QString("%1.%2.%3.%4")
.arg(m_lineEidt[]->text().isEmpty() ? "" : m_lineEidt[]->text())
.arg(m_lineEidt[]->text().isEmpty() ? "" : m_lineEidt[]->text())
.arg(m_lineEidt[]->text().isEmpty() ? "" : m_lineEidt[]->text())
.arg(m_lineEidt[]->text().isEmpty() ? "" : m_lineEidt[]->text());
}
2.通过QLineEdit成员函数setInputMask("000.000.000.000;_")+setValidator也能实现相关IP输入(但光标效果和删除效果不大理想,具体接口可参看QLineEdit文档);