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_Period: { this->focusNextChild(); } break; case Qt::Key_Backspace: { if (pEdit->text().length() <= 1) this->focusNextPrevChild(false);
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++; } } }