Fortune client example - list all IP addresses
authorShane Kearns <ext-shane.2.kearns@nokia.com>
Wed, 15 Feb 2012 20:12:15 +0000 (20:12 +0000)
committerQt by Nokia <qt-info@nokia.com>
Fri, 17 Feb 2012 10:44:29 +0000 (11:44 +0100)
The first IP address on the machine might not be a usable one.
Changed the QLineEdit to a QComboBox, and populated it with
all IP addresses of the machine, along with the machine names
if they are configured.

Task-number: QTBUG-13121
Change-Id: I7c443f5ce6efb0d0b525c5abad1671d3dcbba33c
Reviewed-by: Casper van Donderen <casper.vandonderen@nokia.com>
examples/network/fortuneclient/client.cpp
examples/network/fortuneclient/client.h

index 01048f1..fe16554 100644 (file)
@@ -51,26 +51,35 @@ Client::Client(QWidget *parent)
     hostLabel = new QLabel(tr("&Server name:"));
     portLabel = new QLabel(tr("S&erver port:"));
 
-    // find out which IP to connect to
-    QString ipAddress;
+    hostCombo = new QComboBox;
+    hostCombo->setEditable(true);
+    // find out name of this machine
+    QString name = QHostInfo::localHostName();
+    if (!name.isEmpty()) {
+        hostCombo->addItem(name);
+        QString domain = QHostInfo::localDomainName();
+        if (!domain.isEmpty())
+            hostCombo->addItem(name + QChar('.') + domain);
+    }
+    if (name != QString("localhost"))
+        hostCombo->addItem(QString("localhost"));
+    // find out IP addresses of this machine
     QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
-    // use the first non-localhost IPv4 address
+    // add non-localhost addresses
     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 (!ipAddressesList.at(i).isLoopback())
+            hostCombo->addItem(ipAddressesList.at(i).toString());
+    }
+    // add localhost addresses
+    for (int i = 0; i < ipAddressesList.size(); ++i) {
+        if (ipAddressesList.at(i).isLoopback())
+            hostCombo->addItem(ipAddressesList.at(i).toString());
     }
-    // if we did not find one, use IPv4 localhost
-    if (ipAddress.isEmpty())
-        ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
 
-    hostLineEdit = new QLineEdit(ipAddress);
     portLineEdit = new QLineEdit;
     portLineEdit->setValidator(new QIntValidator(1, 65535, this));
 
-    hostLabel->setBuddy(hostLineEdit);
+    hostLabel->setBuddy(hostCombo);
     portLabel->setBuddy(portLineEdit);
 
     statusLabel = new QLabel(tr("This examples requires that you run the "
@@ -90,7 +99,7 @@ Client::Client(QWidget *parent)
     tcpSocket = new QTcpSocket(this);
 //! [1]
 
-    connect(hostLineEdit, SIGNAL(textChanged(QString)),
+    connect(hostCombo, SIGNAL(editTextChanged(QString)),
             this, SLOT(enableGetFortuneButton()));
     connect(portLineEdit, SIGNAL(textChanged(QString)),
             this, SLOT(enableGetFortuneButton()));
@@ -107,7 +116,7 @@ Client::Client(QWidget *parent)
 
     QGridLayout *mainLayout = new QGridLayout;
     mainLayout->addWidget(hostLabel, 0, 0);
-    mainLayout->addWidget(hostLineEdit, 0, 1);
+    mainLayout->addWidget(hostCombo, 0, 1);
     mainLayout->addWidget(portLabel, 1, 0);
     mainLayout->addWidget(portLineEdit, 1, 1);
     mainLayout->addWidget(statusLabel, 2, 0, 1, 2);
@@ -150,7 +159,7 @@ void Client::requestNewFortune()
     blockSize = 0;
     tcpSocket->abort();
 //! [7]
-    tcpSocket->connectToHost(hostLineEdit->text(),
+    tcpSocket->connectToHost(hostCombo->currentText(),
                              portLineEdit->text().toInt());
 //! [7]
 }
@@ -224,7 +233,7 @@ void Client::displayError(QAbstractSocket::SocketError socketError)
 void Client::enableGetFortuneButton()
 {
     getFortuneButton->setEnabled((!networkSession || networkSession->isOpen()) &&
-                                 !hostLineEdit->text().isEmpty() &&
+                                 !hostCombo->currentText().isEmpty() &&
                                  !portLineEdit->text().isEmpty());
 
 }
index 6e65e6e..dc65251 100644 (file)
@@ -45,6 +45,7 @@
 #include <QTcpSocket>
 
 QT_BEGIN_NAMESPACE
+class QComboBox;
 class QDialogButtonBox;
 class QLabel;
 class QLineEdit;
@@ -71,7 +72,7 @@ private slots:
 private:
     QLabel *hostLabel;
     QLabel *portLabel;
-    QLineEdit *hostLineEdit;
+    QComboBox *hostCombo;
     QLineEdit *portLineEdit;
     QLabel *statusLabel;
     QPushButton *getFortuneButton;