summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorShane Kearns <ext-shane.2.kearns@nokia.com>2012-02-15 20:12:15 +0000
committerQt by Nokia <qt-info@nokia.com>2012-02-17 11:44:29 +0100
commit304fc533bddde005ce786c4ab2da71678f89e379 (patch)
tree4b692f5e2d79672d0b58ee02a4ef277a64b18519 /examples
parentf89d02e0039562fadf8d8708bd71cff42a46b7a7 (diff)
Fortune client example - list all IP addresses
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>
Diffstat (limited to 'examples')
-rw-r--r--examples/network/fortuneclient/client.cpp43
-rw-r--r--examples/network/fortuneclient/client.h3
2 files changed, 28 insertions, 18 deletions
diff --git a/examples/network/fortuneclient/client.cpp b/examples/network/fortuneclient/client.cpp
index 01048f137d..fe16554eae 100644
--- a/examples/network/fortuneclient/client.cpp
+++ b/examples/network/fortuneclient/client.cpp
@@ -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());
}
diff --git a/examples/network/fortuneclient/client.h b/examples/network/fortuneclient/client.h
index 6e65e6e021..dc65251476 100644
--- a/examples/network/fortuneclient/client.h
+++ b/examples/network/fortuneclient/client.h
@@ -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;