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