转自:http://blog.youkuaiyun.com/u011417605/article/details/51353337
版权声明:本文为转载文章,转载请注明出处。如有问题请联系QQ:1245178753。
Qt作为界面框架,已经非常完善了。但是,也不是尽善尽美,IP输入框作为开发中使用很频繁的一个控件,Qt竟然没有实现,也是醉了。不过,我们自己也可以实现,并不是很复杂。
先来看下最终实现的效果:
使用起来还算顺手,实现了以下一些方便的操作:
1.连续输入;
2.连续删除;
3.任意位置插入;
4.自适应大小变化。
5.正则匹配,每个值不大于255。设置IP值时,有正则进行验证是否是IP格式。
实现方法,使用一个大的QLineEdit嵌套四个小的QLineEdit,中间的点是使用paintEvent画出来的。操作是使用eventFilter来进行分发实现的。
实现代码:
- /************************************************************************/
- /* 作者:徐冬冬
- QQ:1245178753
- 博客网址:http://blog.youkuaiyun.com/u011417605
- */
- /************************************************************************/
- #ifndef QIPLINEEDIT_H
- #define QIPLINEEDIT_H
- #include <QLineEdit>
- #include <QEvent>
- class QIPLineEdit : public QLineEdit
- {
- Q_OBJECT
- public:
- QIPLineEdit(QWidget *parent = 0);
- ~QIPLineEdit();
- void setText(const QString &strIP);
- QString text() const;
- protected:
- void paintEvent(QPaintEvent *event);
- bool eventFilter(QObject *obj, QEvent *ev);
- int getIndex(QLineEdit *pEdit);
- bool isTextValid(const QString &strIP);
- private:
- QLineEdit *m_lineEidt[4];
- };
- #endif // QIPLINEEDIT_H
- /************************************************************************/
- /* 作者:徐冬冬
- QQ:1245178753
- 博客网址:http://blog.youkuaiyun.com/u011417605
- */
- /************************************************************************/
- #include "qiplineedit.h"
- #include <QRegExpValidator>
- #include <QPainter>
- #include <QHBoxLayout>
- #include <QKeyEvent>
- #include <QMessageBox>
- #include <QDebug>
- QIPLineEdit::QIPLineEdit(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(10);
- pHBox->setContentsMargins(0, 0, 0, 0);
- for (int i = 0; i < 4; i++)
- {
- m_lineEidt[i] = new QLineEdit(this);
- m_lineEidt[i]->setFrame(false);
- m_lineEidt[i]->setMaxLength(3);
- 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);
- pHBox->addWidget(m_lineEidt[i]);
- }
- this->setReadOnly(true);
- }
- QIPLineEdit::~QIPLineEdit()
- {
- }
- void QIPLineEdit::paintEvent(QPaintEvent *event)
- {
- __super::paintEvent(event);
- QPainter painter(this);
- QBrush brush;
- brush.setStyle(Qt::BrushStyle::SolidPattern);
- brush.setColor(Qt::black);
- painter.setBrush(brush);
- int width = 0;
- for (int i = 0; i < 3; i++)
- {
- width += m_lineEidt[i]->width() + (i == 0 ? 3 : 10);//布局的间隔
- painter.drawEllipse(width, height() / 2 - 2, 4, 4);
- }
- }
- int QIPLineEdit::getIndex(QLineEdit *pEdit)
- {
- int index = -1;
- for (int i = 0; i < 4; i++)
- {
- if (pEdit == m_lineEidt[i])
- index = i;
- }
- return index;
- }
- bool QIPLineEdit::eventFilter(QObject *obj, QEvent *ev)
- {
- if (children().contains(obj) && QEvent::KeyPress == ev->type())
- {
- QKeyEvent *keyEvent = dynamic_cast<QKeyEvent *>(ev);
- QLineEdit *pEdit = qobject_cast<QLineEdit *>(obj);
- switch (keyEvent->key())
- {
- case Qt::Key_0:
- case Qt::Key_1:
- case Qt::Key_2:
- case Qt::Key_3:
- case Qt::Key_4:
- case Qt::Key_5:
- case Qt::Key_6:
- case Qt::Key_7:
- case Qt::Key_8:
- case Qt::Key_9:
- {
- QString strText = pEdit->text();
- if (pEdit->selectedText().length())
- {
- pEdit->text().replace(pEdit->selectedText(), QChar(keyEvent->key()));
- }
- else if (strText.length() == 3 || strText.length() < 3 && strText.toInt() * 10 > 255)
- {
- int index = getIndex(pEdit);
- if (index != -1 && index != 3)
- {
- m_lineEidt[index + 1]->setFocus();
- m_lineEidt[index + 1]->selectAll();
- }
- }
- else if (strText.length() == 2 && strText.toInt() * 10 < 255)
- {
- if (Qt::Key_0 == keyEvent->key() && strText.toInt())
- {
- pEdit->setText(strText.insert(pEdit->cursorPosition(), QChar(Qt::Key_0)));
- }
- }
- return __super::eventFilter(obj, ev);
- }
- break;
- case Qt::Key_Backspace:
- {
- QString strText = pEdit->text();
- if (!strText.length() || strText.length() && !pEdit->cursorPosition())
- {
- int index = getIndex(pEdit);
- if (index != -1 && index != 0)
- {
- m_lineEidt[index - 1]->setFocus();
- int length = m_lineEidt[index - 1]->text().length();
- m_lineEidt[index - 1]->setCursorPosition(length ? length : 0);
- }
- }
- return __super::eventFilter(obj, ev);
- }
- case Qt::Key_Left:
- {
- if (!pEdit->cursorPosition())
- {
- int index = getIndex(pEdit);
- if (index != -1 && index != 0)
- {
- m_lineEidt[index - 1]->setFocus();
- int length = m_lineEidt[index - 1]->text().length();
- m_lineEidt[index - 1]->setCursorPosition(length ? length : 0);
- }
- }
- return __super::eventFilter(obj, ev);
- }
- case Qt::Key_Right:
- {
- if (pEdit->cursorPosition() == pEdit->text().length())
- {
- int index = getIndex(pEdit);
- if (index != -1 && index != 3)
- {
- m_lineEidt[index + 1]->setFocus();
- m_lineEidt[index + 1]->setCursorPosition(0);
- }
- }
- return __super::eventFilter(obj, ev);
- }
- default:
- break;
- }
- }
- return false;
- }
- void QIPLineEdit::setText(const QString &strIP)
- {
- if (!isTextValid(strIP))
- {
- QMessageBox::warning(this, "Attention", "Your IP Address is Invalid!", QMessageBox::StandardButton::Ok);
- return;
- }
- else
- {
- int i = 0;
- QStringList ipList = strIP.split(".");
- for each (QString ip in ipList)
- {
- m_lineEidt[i]->setText(ip);
- i++;
- }
- }
- }
- bool QIPLineEdit::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 QIPLineEdit::text() const
- {
- QString strIP;
- for (int i = 0; i < 4; i++)
- strIP.append(m_lineEidt[i]->text());
- return strIP;
- }
本文地址:http://blog.youkuaiyun.com/u011417605/article/details/51353337
源码下载:http://download.youkuaiyun.com/detail/u011417605/9514281