Results 1 to 13 of 13

Thread: how to read pc's network IP address

  1. #1
    Join Date
    Feb 2007
    Posts
    16
    Thanks
    9

    Default how to read pc's network IP address

    how use qt coding to read pc's network IP / internet IP address instead of 127.0.0.1?

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to read pc's network IP address

    Quote Originally Posted by wei243 View Post
    how use qt coding to read pc's network IP / internet IP address instead of 127.0.0.1?
    On Window OS....

    Qt Code:
    1. void Gui_FrontPage::TestQprocessqt4()
    2. {
    3.  
    4. QProcess process;
    5. #if defined Q_WS_WIN
    6. process.setReadChannelMode(QProcess::MergedChannels);
    7. process.start("ipconfig" , QStringList() << "/all");
    8.  
    9. if (!process.waitForFinished()) {
    10. msgb->information( this , "QT4 function test", process.errorString() );
    11. } else {
    12. msgb->information( this , "QT4 function test", process.readAll() );
    13. }
    14.  
    15. #endif
    16. }
    To copy to clipboard, switch view to plain text mode 

    on linux only admin user can discovery
    sudo ifconfig

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

    wei243 (8th March 2007)

  4. #3
    Join Date
    Feb 2007
    Posts
    16
    Thanks
    9

    Default Re: how to read pc's network IP address

    thx patrix08,
    it is amazing - ur program show all the detail of ipconfig.
    if i want to show ip address only, wat should i do?

  5. #4
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to read pc's network IP address

    Quote Originally Posted by wei243 View Post
    thx patrix08,
    it is amazing - ur program show all the detail of ipconfig.
    if i want to show ip address only, wat should i do?

    first job .... split all line to a qstringlist....
    register gateway or dns or network adress...
    && after .... http://www.regular-expressions.info/examples.html ...IP . && grep your
    ip http://doc.trolltech.com/4.2/qregexp.html

    similar to http://www.qtforum.de/forum/viewtopi...ighlight=regex

  6. The following user says thank you to patrik08 for this useful post:

    wei243 (9th March 2007)

  7. #5
    Join Date
    Feb 2007
    Posts
    16
    Thanks
    9

    Default Re: how to read pc's network IP address

    i failed to try the QRegExp in reading the ip address from qprocess's return string.
    actually i m a newbie n never seen QRegExp.
    pls give me an example coding to read it.
    thx

  8. #6
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to read pc's network IP address

    Quote Originally Posted by wei243 View Post
    i failed to try the QRegExp in reading the ip address from qprocess's return string.
    actually i m a newbie n never seen QRegExp.
    pls give me an example coding to read it.
    thx
    Google have so match QRegExp ip adress
    on is.....
    http://lists.trolltech.com/qt-intere...ad00085-0.html
    search on http://www.koders.com/ you can find app piece and code trick ...


    if you beginn on qt search qt function on http://www.koders.com/ && test it....

    I learning qt 50% from http://www.koders.com/ && 30% from books and the rest from my php5 objekt know-how ....

    My original work is cook chief 15 years a go.... and on 1999 i begin to write php & phython


    untested code .....


    Qt Code:
    1. QString incomming = "result from ipconfig /all"; /* qprocess read*/
    2. QStringList alline = split("\n");;
    3.  
    4.  
    5. for (int i = 0; i < alline.size(); ++i) {
    6. QString onrow = alline.at(i);
    7. if (onrow.contain("dns")) {
    8. /* list onrow.split(" "); */
    9. qDebug() << "### onrow " << onrow;
    10. }
    11.  
    12. }
    13.  
    14. QString myip = "[0-9]{1,3}(.[0-9]{1,3}){3,3}";
    15.  
    16.  
    17. QString str = "dgsdgsdgsdgsgsg 192.168.1.71 sdgdsgsgsgsdgsdgsg";
    18.  
    19. QRegExp expression( myip , Qt::CaseInsensitive );
    20. expression.setMinimal(true);
    21. int iPosition = 0;
    22. while( (iPosition = expression.indexIn( str , iPosition )) != -1 ) {
    23. QString grep = expression.cap( 1 );
    24.  
    25. qDebug() << "### grep " << grep;
    26. iPosition += expression.matchedLength();
    27. }
    To copy to clipboard, switch view to plain text mode 

    && on line 84 from http://qt-webdav.svn.sourceforge.net....h?view=markup
    you find a sample wo find html image tag....
    from my projekt
    Last edited by patrik08; 9th March 2007 at 10:13.

  9. The following user says thank you to patrik08 for this useful post:

    wei243 (10th March 2007)

  10. #7
    Join Date
    Feb 2007
    Posts
    16
    Thanks
    9

    Default Re: how to read pc's network IP address

    i manage to get ip now. thx alot.

  11. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to read pc's network IP address

    With QT 4.2+ you can use QNetworkInterface:
    Qt Code:
    1. #include <QtDebug>
    2. QList<QHostAddress> addrlist = QNetworkInterface::allAddresses();
    3. foreach(QHostAddress addr, addrlist){
    4. qDebug() << addr.toString();
    5. }
    To copy to clipboard, switch view to plain text mode 

  12. The following user says thank you to wysota for this useful post:

    montylee (3rd November 2008)

  13. #9
    Join Date
    Jan 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to read pc's network IP address

    Continuing this thread, how'd you do that on linux. Specifically, I need to retrieve gateway address on linux.
    Regards,

  14. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to read pc's network IP address

    The gateway address is not tied to an interface. Each system can have any number of gateways. You can run "route -n" and parse it to find the default gateway (one for 0.0.0.0 network) or do the same for /proc/net/route but that's probably everything that can be done (unless HAL exposes gateway definitions).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #11
    Join Date
    May 2009
    Posts
    11
    Thanks
    3

    Default Re: how to read pc's network IP address

    Quote Originally Posted by wysota View Post
    The gateway address is not tied to an interface. Each system can have any number of gateways. You can run "route -n" and parse it to find the default gateway (one for 0.0.0.0 network) or do the same for /proc/net/route but that's probably everything that can be done (unless HAL exposes gateway definitions).
    Meaning there is no ... QT way to get the gateway?
    Is there a Qt wrapper class for getting the Adapter information?
    (information such as the DHCP enabled, adapter name and etc. )

  16. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to read pc's network IP address

    Gateway and network interface are two completely different things. For the latter see QNetworkInterface.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #13
    Join Date
    Jan 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to read pc's network IP address

    Thanks for the answer. I guess the simplest way is to use QProcess to run route -n command.
    Regards

Similar Threads

  1. How to Read and display BMP image using QT
    By agsrinivasan in forum Qt Programming
    Replies: 3
    Last Post: 29th January 2007, 07:14
  2. read files and save the,
    By kernel_panic in forum Qt Programming
    Replies: 2
    Last Post: 4th January 2007, 06:31
  3. QSocket - signal for network wire disconnection
    By manivannan_1984 in forum Qt Programming
    Replies: 7
    Last Post: 5th September 2006, 13:52
  4. Replies: 13
    Last Post: 1st June 2006, 14:01
  5. Qt 4.1.1 linker warnings
    By Matt Smith in forum Installation and Deployment
    Replies: 0
    Last Post: 26th February 2006, 22:14

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.