Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] IPv4 Address conversion
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] IPv4 Address conversion

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jediengineer
    wrote on last edited by
    #1

    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();
    @

    or

    @
    quint32 device_IP = remoteIP->property("text"),toUInt();
    @

    or I could use:

    @QHostAddress::toIPv4Address ()@

    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

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      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.

      157

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vezprog
        wrote on last edited by
        #3

        You can validate an IP address using QHostAddress:
        @
        // check for a valid ip address
        QHostAddress hostAddress;
        if (!hostAddress.setAddress(ui->text_of_ip->text())){
        // invalid ip address
        }
        else {
        // 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.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jediengineer
          wrote on last edited by
          #4

          Thanks!! I'll try those and report back!

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jediengineer
            wrote on last edited by
            #5

            All worked out, thanks!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved