Te traemos el hallazgo a esta aprieto, al menos eso pensamos. Si sigues con dudas coméntalo, que para nosotros será un placer ayudarte
Solución:
Acepta un valor superior a 255.
Absolutamente, porque ‘0’ significa esto:
ASCII digit permitted but not required.
Como puede ver, esta no es su taza de té. Existen al menos las siguientes formas de eludirlo:
-
Escribe un validador personalizado
-
Usar expresiones regulares
-
Divida la entrada en cuatro entradas y valide cada entrada por sí sola sin dejar de tener delimitadores visuales entre las entradas.
La solución de expresiones regulares sería probablemente la más rápida, pero también la más fea en mi humilde opinión:
QString ipRange = "(?:[0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])";
// You may want to use QRegularExpression for new code with Qt 5 (not mandatory).
QRegExp ipRegex ("^" + ipRange
+ "\." + ipRange
+ "\." + ipRange
+ "\." + ipRange + "$");
QRegExpValidator *ipValidator = new QRegExpValidator(ipRegex, this);
lineEdit->setValidator(ipValidator);
Descargo de responsabilidad: esto solo validará las direcciones IP4 correctamente, por lo que no funcionará para IP6 si lo necesita en el futuro.
El método más fácil es probar la opción inputmask
ui->lineEdit_IP->setInputMask( "000.000.000.000" );
y en su código de validación use la siguiente condición simple
QHostAddress myIP;
if( myIP.setAddress( ui->lineEdit_IP->text()) )
qDebug()<<"Valid IP Address";
else
qDebug()<<"Invalid IP address";
Hola, tuve casi el mismo problema. Las soluciones con inputmask o regExp no fueron satisfactorias. Decidí hacer mi propia bicicleta.
Encabezamiento:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
namespace Ui
class MainWindow;
class MainWindow : public QMainWindow
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
;
//=============================================================================
class CustomIpEditor : public QFrame
Q_OBJECT
public:
explicit CustomIpEditor(QWidget *parent = 0);
virtual ~CustomIpEditor()
;
//=============================================================================
class CustomLineEdit : public QLineEdit
Q_OBJECT
public:
explicit CustomLineEdit(const QString & contents = "", QWidget *parent = 0);
virtual ~CustomLineEdit()
signals:
void jumpForward();
void jumpBackward();
public slots:
void jumpIn();
protected:
virtual void focusInEvent(QFocusEvent *event);
virtual void keyPressEvent(QKeyEvent * event);
virtual void mouseReleaseEvent(QMouseEvent *event);
private:
bool selectOnMouseRelease;
;
#endif // MAINWINDOW_H
Fuente:
#include
#include
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
ui->setupUi(this);
centralWidget()->setLayout(new QVBoxLayout);
CustomIpEditor *myed = new CustomIpEditor;
centralWidget()->layout()->addWidget(myed);
MainWindow::~MainWindow()
delete ui;
//=============================================================================
CustomLineEdit::CustomLineEdit(const QString & contents, QWidget *parent) :
QLineEdit(contents, parent), selectOnMouseRelease(false)
QIntValidator *valid = new QIntValidator(0, 255, this);
setValidator(valid);
void CustomLineEdit::jumpIn()
setFocus();
selectOnMouseRelease = false;
selectAll();
void CustomLineEdit::focusInEvent(QFocusEvent *event)
QLineEdit::focusInEvent(event);
selectOnMouseRelease = true;
void CustomLineEdit::keyPressEvent(QKeyEvent * event)
int key = event->key();
int cursorPos = cursorPosition();
// Jump forward by Space
if (key == Qt::Key_Space)
emit jumpForward();
event->accept();
return;
// Jump Backward only from 0 cursor position
if (cursorPos == 0)
// Jump forward from last postion by right arrow
if (cursorPos == text().count())
if (key == Qt::Key_Right)
emit jumpForward();
event->accept();
return;
// After key is placed cursor has new position
QLineEdit::keyPressEvent(event);
int freshCurPos = cursorPosition();
if ((freshCurPos == 3) && (key != Qt::Key_Right))
emit jumpForward();
void CustomLineEdit::mouseReleaseEvent(QMouseEvent *event)
if(!selectOnMouseRelease)
return;
selectOnMouseRelease = false;
selectAll();
QLineEdit::mouseReleaseEvent(event);
//=============================================================================
void makeCommonStyle(QLineEdit* line)
line->setContentsMargins(0, 0, 0, 0);
line->setAlignment(Qt::AlignCenter);
line->setStyleSheet("QLineEdit border: 0px none; ");
line->setFrame(false);
line->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
QLineEdit* makeIpSpliter()
QLineEdit *spliter = new QLineEdit(".");
makeCommonStyle(spliter);
spliter->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
spliter->setMaximumWidth(10);
spliter->setReadOnly(true);
spliter->setFocusPolicy(Qt::NoFocus);
return spliter;
CustomIpEditor::CustomIpEditor(QWidget *parent) :
QFrame(parent)
setContentsMargins(0, 0, 0, 0);
setStyleSheet("QFrame background-color: white; border: 1px solid white; border-radius: 15px; ");
QList lines;
QList ::iterator linesIterator;
lines.append(new CustomLineEdit);
lines.append(new CustomLineEdit);
lines.append(new CustomLineEdit);
lines.append(new CustomLineEdit);
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->setSpacing(0);
setLayout(mainLayout);
for (linesIterator = lines.begin(); linesIterator != lines.end(); ++linesIterator)
makeCommonStyle(*linesIterator);
mainLayout->addWidget(*linesIterator);
if (*linesIterator != lines.last())
connect(*linesIterator, &CustomLineEdit::jumpForward,
*(linesIterator+1), &CustomLineEdit::jumpIn);
mainLayout->addWidget(makeIpSpliter());
if (*linesIterator != lines.first())
connect(*linesIterator, &CustomLineEdit::jumpBackward,
*(linesIterator-1), &CustomLineEdit::jumpIn);