[SOLVED] IPv4 Address conversion
Just a quick question if anyone has a second - Sorry if this seems a bit simple, I'm a little new with this Qt thing...
My program is taking text from a QML TextInput to convert to an IPv4 address. I can do this several ways - these work, I've tried them (pardon the incomplete code, I gave the quick version):
@QQuickItem* object = myDialog->rootObject();
// Locate the remote IP address input text:
QObject remoteIP = object->findChild<QObject>("remoteIP");
// Convert IP address to string:
QString device_IP = remoteIP->property("text").toString();
quint32 device_IP = remoteIP->property("text"),toUInt();
or I could use:
which I'm assuming would work something like this:
quint32 device_IP = remoteIP->property("text").toIPV4Address();
In the latter case, the documentation says "This value is only valid if the Protocol() is IPv4Protocol" which I assume means that if it's not in the format of IPv4 (so if it contains letters or isn't something like "10.1.10.43") it will not return a value?
I'm just looking for a way using any of those methods I posted, to convert my user entered QString() to an IPv4 address, and have an indication that the address the user entered is either in the correct format or not. So basically, if someone screws up entering the number, is there proper, general method or means to check?
Thanks for your time all - xD
To check if user entered a proper IP address you could validate it using a Regular expression. See "regexpvalidator":http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-regexpvalidator.html and "qregexpvalidator":http://qt-project.org/doc/qt-5.0/qtgui/qregexpvalidator.html. You just need to set a Regular expression that will satisfy the IPv4 requirements.
You can validate an IP address using QHostAddress:
// check for a valid ip address
// invalid ip address
// valid ip address entered
Or you can use a regular expression like P3c0 has stated above. I think this way is quick and easy though.
Thanks!! I'll try those and report back!
All worked out, thanks!