找回密码
 立即注册
收起左侧

QLineEdit IP格式

7
回复
6356
查看
[复制链接]

尚未签到

来源: 2019-9-18 11:09:16 显示全部楼层 |阅读模式
5Qter豆
我使用了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这种格式呢?

回复

使用道具 举报

累计签到:955 天
连续签到:1 天
2019-9-18 16:03:48 显示全部楼层
QRegExp rx("((1?\\d{1,2}|2[0-5]{2})\\.){3}(1?\\d{1,2}|2[0-5]{2})");采用类似的正则表达式就可以了。

点评

只用正则表式的话,中间的小点得自己输入,也不符合需求; 需求就是不输入的时候中间的小点得存在,输入后 显示为 192.168.21.1 这种的 类似电脑的IP输入  详情 回复 发表于 2019-9-18 17:02
回复

使用道具 举报

尚未签到

2019-9-18 17:02:31 显示全部楼层
wdmxtk002 发表于 2019-9-18 16:03
QRegExp rx("((1?\\d{1,2}|2[0-5]{2})\\.){3}(1?\\d{1,2}|2[0-5]{2})");采用类似的正则表达式就可以了。 ...

只用正则表式的话,中间的小点得自己输入,也不符合需求;

需求就是不输入的时候中间的小点得存在,输入后 显示为 192.168.21.1 这种的
类似电脑的IP输入

点评

你意思是要跟mfc类似的ip地址控件,自己封装一个吧,只靠设定规则QLineEdit没法实现。  详情 回复 发表于 2019-9-19 08:35
回复

使用道具 举报

累计签到:955 天
连续签到:1 天
2019-9-19 08:35:57 显示全部楼层
涅槃 发表于 2019-9-18 17:02
只用正则表式的话,中间的小点得自己输入,也不符合需求;

需求就是不输入的时候中间的小点得存在,输入 ...

你意思是要跟mfc类似的ip地址控件,自己封装一个吧,只靠设定规则QLineEdit没法实现。
回复

使用道具 举报

尚未签到

2020-4-20 13:24:14 显示全部楼层
使用正则表达式:^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3};$
回复

使用道具 举报

尚未签到

2020-4-20 13:26:21 显示全部楼层
wdmxtk002 发表于 2019-9-19 08:35
你意思是要跟mfc类似的ip地址控件,自己封装一个吧,只靠设定规则QLineEdit没法实现。 ...

那你直接用几个lineedit不就好了,然后去除边框
回复

使用道具 举报

尚未签到

2020-9-25 10:32:49 显示全部楼层
重新写了一个控件,就是几个lineEdit组合的。
回复

使用道具 举报

尚未签到

2022-1-19 10:16:09 显示全部楼层
#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());
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了