Results 1 to 6 of 6

Thread: Get current ip address

  1. #1
    Join Date
    Mar 2009
    Posts
    29
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Get current ip address

    Hi, I have problem with ip address.
    How can i get current ip address on my pc? I tried used this code:

    Qt Code:
    1. QHostInfo info;
    2. QNetworkInterface ifObj = QNetworkInterface::interfaceFromName(info.hostName());
    3. qDebug() << ifObj;
    To copy to clipboard, switch view to plain text mode 
    It was returned:
    Qt Code:
    1. QNetworkInterface( name = "", hardware address = "", flags = , entrise = () )
    To copy to clipboard, switch view to plain text mode 
    also I used function: QNetworkAddressEntry::ip () , but without results.

    So, I wait for yours propositions to solve my problem.
    Last edited by Trok; 16th March 2009 at 20:53.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Get current ip address

    Hi, nice to have you here. In this forum is a wonderful function: the search engine: Look here for example.

  3. #3
    Join Date
    Mar 2009
    Posts
    29
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Get current ip address

    Hi, i saw that topic, Lykurg. First method is for Linux system, whilst second is wrong. I search functions to use for Windows.

    I wait for more suggestions about my problem.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Get current ip address

    Qt Code:
    1. QList<QHostAddress> list = QNetworkInterface::allAddresses();
    To copy to clipboard, switch view to plain text mode 
    gives you all addresses on your computer. if you also want bcst and mask use QNetworkAddressEntry with its functions broadcast and netmask.

  5. The following user says thank you to Lykurg for this useful post:

    Trok (16th March 2009)

  6. #5
    Join Date
    Mar 2009
    Posts
    29
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Get current ip address

    That's good, but I have next problem. I don't see my general address whilst I see addresses to my router. I don't know how I can solve this problem. Do you have some ideas?

    PS. Functions in class QNetworkAddressEntry are useless, because return 0. I don't know why?
    Last edited by Trok; 16th March 2009 at 22:57.

  7. #6
    Join Date
    Jan 2009
    Posts
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Lightbulb Re: Get current ip address

    Quote Originally Posted by Trok View Post
    That's good, but I have next problem. I don't see my general address whilst I see addresses to my router. I don't know how I can solve this problem. Do you have some ideas?

    PS. Functions in class QNetworkAddressEntry are useless, because return 0. I don't know why?
    There is no such thing as a "main address". There is just a default route that specifies a device to use (on all operating systems). I think one way to find out which your "primary" device is, is to use use a QTcpSocket and have it "connect" to a possibly non-existing address that is not on the local network, then use QAbstractSocket's localAddress() which returns a QHostAddress, which in turn contains the IP of the local interface. After that you can then find ouch wich interface has the said IP by searching through the list of QNetworkInterfaces.

    I think there might be a easier way to find out which is your default network interface, but those approaches possibly require a linux/windows shell and are not platform independent.

Similar Threads

  1. Can I get current rotation of a QGraphicsView?
    By bjh in forum Qt Programming
    Replies: 5
    Last Post: 2nd October 2008, 22:04
  2. Distributing QT application for Mac OS
    By mb0 in forum Qt Programming
    Replies: 1
    Last Post: 31st May 2007, 18:59
  3. Replies: 15
    Last Post: 21st April 2007, 17:46
  4. Replies: 1
    Last Post: 19th April 2007, 22:23
  5. How to get current row(column) index in the QTextTable?
    By denny.t in forum Qt Programming
    Replies: 3
    Last Post: 5th April 2006, 07:53

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.