Results 1 to 4 of 4

Thread: Detect network connection in QT

  1. #1
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    2
    Qt products
    Qt4 Qt5 Qt/Embedded PyQt3 PyQt4
    Platforms
    Unix/X11 Windows Android

    Default Detect network connection in QT

    Hi every body,
    I have a problem when i detect network connection in Windows has install VirtualBox and VMWare.
    This is my code:
    Qt Code:
    1. bool MainWindow::IsNetworkConnected()
    2. {
    3. bool bReturn = false;
    4. QList<QString> possibleMatches;
    5. QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
    6. if ( !ifaces.isEmpty() )
    7. {
    8. for(int i=0; i < ifaces.size(); i++)
    9. {
    10. unsigned int flags = ifaces[i].flags();
    11. bool isLoopback = (bool)(flags & QNetworkInterface::IsLoopBack);
    12. bool isP2P = (bool)(flags & QNetworkInterface::IsPointToPoint);
    13. bool isRunning = (bool)(flags & QNetworkInterface::IsRunning);
    14. bool isUp = (bool)(flags && QNetworkInterface::IsUp);
    15.  
    16. // If this interface isn't running, we don't care about it
    17. if ( !isRunning )
    18. continue;
    19. // We only want valid interfaces that aren't loopback/virtual and not point to point
    20. if ( !ifaces[i].isValid() || isLoopback || isP2P || !isUp)
    21. continue;
    22. QList<QHostAddress> addresses = ifaces[i].allAddresses();
    23. for(int a=0; a < addresses.size(); a++)
    24. {
    25. // Ignore local host
    26. if ( addresses[a] == QHostAddress::LocalHost ) continue;
    27.  
    28. // Ignore non-ipv4 addresses
    29. if ( !addresses[a].toIPv4Address() )
    30. continue;
    31.  
    32. QString ip = addresses[a].toString();
    33. if ( ip.isEmpty() )
    34. continue;
    35. bool foundMatch = false;
    36. for (int j=0; j < possibleMatches.size(); j++)
    37. if ( ip == possibleMatches[j] )
    38. {
    39. foundMatch = true;
    40. break;
    41. }
    42. if ( !foundMatch )
    43. {
    44. possibleMatches.push_back( ip );
    45. qDebug() << "possible address: " << ifaces[i].humanReadableName() << "->" << ip;
    46. }
    47. }
    48. }
    49. }
    50. return bReturn;
    51. }
    To copy to clipboard, switch view to plain text mode 
    This function always return "true" when i disconnected internet conection.
    And my Network and Share Center:
    Nework1.png

    Please help me fix it.
    Sorry my for my english skill.
    Last edited by anda_skoa; 17th May 2014 at 14:13. Reason: changed [quote] to [code] tags

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,677
    Thanks
    13
    Thanked 1,596 Times in 1,524 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Detect network connection in QT

    Your code never sets bReturn to true so I cannot see a way that the function could return true.

  3. #3
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    2
    Qt products
    Qt4 Qt5 Qt/Embedded PyQt3 PyQt4
    Platforms
    Unix/X11 Windows Android

    Default Re: Detect network connection in QT

    Quote Originally Posted by ChrisW67 View Post
    Your code never sets bReturn to true so I cannot see a way that the function could return true.
    Sorry. I want ask: "why foundMatch alway is true?". Please help me, i need some hint to check network is disconnect.
    Thanks

  4. #4
    Join Date
    Oct 2013
    Posts
    41
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Detect network connection in QT

    I don't see where possibleMatches is filled with any values. My guess would be foundMatch is actually always false because your for loop is from 0 to 0. Also, I don't know if it's what you intended, but there are no brackets around that for loop.

Similar Threads

  1. Can QT application detect which type of connection is used?
    By satya@sipl in forum Qt Programming
    Replies: 1
    Last Post: 1st December 2012, 14:16
  2. Detect whether a server requires SSL connection
    By mentalmushroom in forum Qt Programming
    Replies: 0
    Last Post: 24th May 2012, 13:40
  3. postgresql connection network
    By khadija123 in forum Qt Programming
    Replies: 5
    Last Post: 12th April 2012, 17:13
  4. how to detect wifi connection under windows?
    By Raul in forum Qt Programming
    Replies: 1
    Last Post: 27th December 2011, 08:20
  5. How to detect an internet connection ?
    By ouekah in forum Newbie
    Replies: 1
    Last Post: 29th March 2010, 23:08

Tags for this Thread

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.