QLineEdit IP格式
我使用了setValidator + setInputMask,实现IP格式的输入,当setInputMask("000.000.000.000; 0")时,可以实现两个的共存有效,但是输入的Ip就会是 192.168.021.001这种格式,不满足需求;当设置setInputMask("000.000.000.000; ")、setInputMask("000.000.000.000; _")时,正则表达式就不会生效,输入IP可以为任何数字;
怎样设置才能实现 192.168.21.1这种格式呢?
QRegExp rx("((1?\\d{1,2}|2{2})\\.){3}(1?\\d{1,2}|2{2})");采用类似的正则表达式就可以了。 wdmxtk002 发表于 2019-9-18 16:03
QRegExp rx("((1?\\d{1,2}|2{2})\\.){3}(1?\\d{1,2}|2{2})");采用类似的正则表达式就可以了。 ...
只用正则表式的话,中间的小点得自己输入,也不符合需求;
需求就是不输入的时候中间的小点得存在,输入后 显示为 192.168.21.1 这种的
类似电脑的IP输入 涅槃 发表于 2019-9-18 17:02
只用正则表式的话,中间的小点得自己输入,也不符合需求;
需求就是不输入的时候中间的小点得存在,输入 ...
你意思是要跟mfc类似的ip地址控件,自己封装一个吧,只靠设定规则QLineEdit没法实现。 使用正则表达式:^{1,3}\.{1,3}\.{1,3}\.{1,3};$ wdmxtk002 发表于 2019-9-19 08:35
你意思是要跟mfc类似的ip地址控件,自己封装一个吧,只靠设定规则QLineEdit没法实现。 ...
那你直接用几个lineedit不就好了,然后去除边框
重新写了一个控件,就是几个lineEdit组合的。 #include "ip_input.h"
#include <QPainter>
#include <QDebug>
#include <QFontMetrics>
#include <QRect>
#include <QKeyEvent>
#include <QLatin1Char>
IpInput::IpInput(QWidget *parent) : QLineEdit(parent)
{
setAlignment(Qt::AlignCenter);
showIp();
}
void IpInput::keyPressEvent(QKeyEvent *event)
{
if (event->type() == QEvent::KeyPress) {
switch (event->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: {
if (cursorPosition() == 3) {
QString temp = ip1 + event->text();
if (temp.toInt() > 255) {
pos = 7;
} else {
if (temp.length() >= 3) {
temp = temp.right(3);
}
ip1 = temp;
}
}
if (cursorPosition() == 7) {
QString temp = ip2 + event->text();
if (temp.toInt() > 255) {
pos = 11;
} else {
ip2 = temp;
}
}
if (cursorPosition() == 11) {
QString temp = ip3 + event->text();
if (temp.toInt() > 255) {
pos = 15;
} else {
ip3 = temp;
}
}
if (cursorPosition() == 15) {
QString temp = ip4 + event->text();
if (temp.toInt() > 255) {
pos = 15;
} else {
ip4 = temp;
}
}
showIp();
event->accept();
break;
}
break;
case Qt::Key_Right:
if (cursorPosition() == 3) {
pos = 7;
} else if(cursorPosition() == 7) {
pos = 11;
} else if(cursorPosition() == 11) {
pos = 15;
} else if(cursorPosition() == 15) {
return;
}
showIp();
break;
case Qt::Key_Left:
if (cursorPosition() == 3) {
return;
} else if(cursorPosition() == 7) {
pos = 3;
} else if(cursorPosition() == 11) {
pos = 7;
} else if(cursorPosition() == 15) {
pos = 11;
}
showIp();
break;
case Qt::Key_Backspace:
qDebug() << "backspace:" << cursorPosition() << endl;
if (cursorPosition() == 3) {
if (ip1.isEmpty()) {
} else {
ip1.remove(ip1.length() - 1, 1);
}
}
if (cursorPosition() == 7) {
if (ip2.isEmpty()) {
pos = 3;
} else {
ip2.remove(ip2.length() - 1, 1);
}
}
if (cursorPosition() == 11) {
if (ip3.isEmpty()) {
pos = 7;
} else {
ip3.remove(ip3.length() - 1, 1);
}
}
if (cursorPosition() == 15) {
if (ip4.isEmpty()) {
pos = 11;
} else {
ip4.remove(ip4.length() - 1, 1);
}
}
showIp();
default:
event->accept();
break;
}
}
}
void IpInput::showIp()
{
QString str = QString("%1.%2.%3.%4").
arg(ip1, 3, QLatin1Char(' ')).
arg(ip2, 3, QLatin1Char(' ')).
arg(ip3, 3, QLatin1Char(' ')).
arg(ip4, 3, QLatin1Char(' '));
setText(str);
setCursorPosition(pos);
}
QSize IpInput::sizeHint() const
{
QFontMetrics fm(font());
qDebug() << "sizeHint" << endl;
return QSize(fm.width("WWW.WWW.WWW.WWW")+10, fm.height());
}
页:
[1]