QT5 obtains the local IP address, computer name, network connection name, MAC address, subnet mask, and broadcast address.

Source: Internet
Author: User
Tags nets

QT5 obtains the local IP address, computer name, network connection name, MAC address, subnet mask, and broadcast address.
Get Host Name

/ *
  * Name: get_localmachine_name
  * Function: Get local machine name
  * Parameter: no
  * Returns: QString
  * /
QString CafesClient :: get_localmachine_name ()
{
     QString machineName = QHostInfo :: localHostName ();
     return machineName;
}
Get local IP Address
/ *
  * Name: get_localmachine_ip
  * Function: Get the IP address of the machine
  * Parameter: no
  * Returns: QString
  * /
QString CafesClient :: get_localmachine_ip ()
{
     QString ipAddress;
     QList ipAddressesList = QNetworkInterface :: allAddresses ();
     // use the first non-localhost IPv4 address
     for (int i = 0; i <ipAddressesList.size (); ++ i) {
         if (ipAddressesList.at (i)! = QHostAddress :: LocalHost &&
             ipAddressesList.at (i) .toIPv4Address ()) {
             ipAddress = ipAddressesList.at (i) .toString ();
             break;
         }
     }
     // if we did not find one, use IPv4 localhost
     if (ipAddress.isEmpty ())
         ipAddress = QHostAddress (QHostAddress :: LocalHost) .toString ();
     return ipAddress;
}
Obtain the network connection name and MAC address of the local machine.
/ *
  * Name: get_localmachine_mac
  * Function: Get the MAC address of the machine
  * Parameter: no
  * Returns: void
  * /
QString CafesClient :: get_localmachine_mac ()
{
     QList nets = QNetworkInterface :: allInterfaces ();
     int i = 0;
     foreach (QNetworkInterface ni, nets)
     {
         i ++;
         qDebug () <
   
Obtain the subnet mask and broadcast address of the local machine.
// In the context of the previous function
QList entryList = interface.addressEntries ();
// Get a list of IP address entries, each entry contains an IP address, a subnet mask, and a broadcast address
foreach (QNetworkAddressEntry entry, entryList)
{
     // Iterate through each IP address entry
qDebug () << ”IP Address:
             "< 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.