Qt实现IP输入框
#include "qiplineedit.h" #include <QApplication> #include <QWidget> #include <QLabel> int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget w; w.resize(300, 150); QIPLineEdit *ipEdit=new QIPLineEdit(&w); ipEdit->setGeometry(50, 20, 200, 50); ipEdit->setText("192.168.11.110"); QLabel *ipLabel=new QLabel(&w); ipLabel->move(50, 90); ipLabel->setText(QString("IP is: %1").arg(ipEdit->text())); w.show(); return a.exec(); }
#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
#include "qiplineedit.h" #include <QRegExpValidator> #include <QPainter> #include <QHBoxLayout> #include <QKeyEvent> #include <QMessageBox> #include <QDebug> QIPLineEdit::QIPLineEdit(QWidget *parent) : QLineEdit(parent) { QRegExp regx("(2[0-5]{2}|2[0-4][0-9]|1?[0-9]{1,2})"); //只能输入IP QHBoxLayout *pHBox = new QHBoxLayout(this); pHBox->setSpacing(10); pHBox->setContentsMargins(2, 2, 2, 2); //不能设置为全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]->setAttribute(Qt::WA_InputMethodEnabled, false); //屏蔽中文输入法干扰 QRegExpValidator *validator = new QRegExpValidator(regx, m_lineEidt[i]); m_lineEidt[i]->setValidator(validator); m_lineEidt[i]->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); pHBox->addWidget(m_lineEidt[i]); } this->setReadOnly(true); } QIPLineEdit::~QIPLineEdit() { } void QIPLineEdit::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 = 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 QLineEdit::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 QLineEdit::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 QLineEdit::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 QLineEdit::eventFilter(obj, ev); } case Qt::Key_Period: //小数点 { 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); m_lineEidt[index + 1]->selectAll(); } } return QLineEdit::eventFilter(obj, ev); } default: break; } } return false; } void QIPLineEdit::setText(const QString &strIP) { if (!isTextValid(strIP)) {
m_lineEdit[0]->setFocus(); return; } else { QStringList ipList = strIP.split("."); for(int i=0; i<ipList.size(); i++) { QString ip=ipList.at(i); m_lineEidt[i]->setText(ip); } } } 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++) { if(i>0) { strIP.append("."); } strIP.append(m_lineEidt[i]->text()); } return strIP; }
转载于:Qt实现IP输入框(模仿Windows系统中的IP输入框)_Keycer的博客-CSDN博客