Results 1 to 3 of 3

Thread: QNetworkInterface get IP

  1. #1
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QNetworkInterface get IP

    Since this is my first post... HI ALL!

    Let me start by saying that I'm a uber nOOb at C++ after 20 years or so not using it, so I'm pretty much getting in touch with the basics...

    As to my question:
    I simply want to extract the IP from a network interface.
    Qt Code:
    1. QNetworkInterface ifObj = QNetworkInterface::interfaceFromName(INTERFACE);
    2. qDebug() << ifObj;
    To copy to clipboard, switch view to plain text mode 

    Returns:
    QNetworkInterface(name = "eth1", hardware address = "00:13:CE:93:54:B6", flags = IsUp IsRunning CanBroadcast CanMulticast , entries = ((address = QHostAddress("192.168.3.246") , netmask = QHostAddress("255.255.255.0") , broadcast = QHostAddress("192.168.3.255") ) , (address = QHostAddress("FE80:0:0:0:213:CEFF:FE93:54B6") , netmask = QHostAddress("FFFF:FFFF:FFFF:FFFF:0:0:0:0") ) ) )
    I want to get that first QHostAddress("192.168.3.246").

    How can I achieve this? The functions/methods available don't *seem* to go my way...

    Already thankful for any advice.
    Best regards,
    Pedro Doria Meunier
    Last edited by jpn; 3rd January 2008 at 09:23. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QNetworkInterface get IP

    Quote Originally Posted by pdoria View Post
    Since this is my first post... HI ALL!
    Hi and welcome!

    Quote Originally Posted by pdoria View Post
    I simply want to extract the IP from a network interface.
    Try something like this:
    Qt Code:
    1. QNetworkInterface iface = QNetworkInterface::interfaceFromName(INTERFACE);
    2. QList<QNetworkAddressEntry> entries = iface.addressEntries();
    3. if (!entries.isEmpty())
    4. {
    5. QNetworkAddressEntry entry = entries.first();
    6. QHostAddress ip = entry.ip();
    7. ...
    8. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    pdoria (3rd January 2008)

  4. #3
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Thumbs up Re: QNetworkInterface get IP

    Thank you JPN!

    That did it!

    This is how the function ended up to be:
    Qt Code:
    1. QHostAddress getHostIp() {
    2. QNetworkInterface iface = QNetworkInterface::interfaceFromName(INTERFACE);
    3. QList<QNetworkAddressEntry> entries = iface.addressEntries();
    4. if (!entries.isEmpty()) {
    5. QNetworkAddressEntry entry = entries.first();
    6. QHostAddress ip = entry.ip();
    7. return ip;
    8. }
    9. return (QHostAddress::Null);
    10. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 3rd January 2008 at 09:53. Reason: Changed <code> to [code]

Similar Threads

  1. Discovery ifdown/ifup on all OS; by QNetworkInterface
    By patrik08 in forum Qt Programming
    Replies: 0
    Last Post: 3rd April 2007, 13:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.