diff options
Diffstat (limited to 'examples/network/fortuneclient')
-rw-r--r-- | examples/network/fortuneclient/client.cpp | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/examples/network/fortuneclient/client.cpp b/examples/network/fortuneclient/client.cpp index b850a57..7b2991a 100644 --- a/examples/network/fortuneclient/client.cpp +++ b/examples/network/fortuneclient/client.cpp @@ -56,7 +56,20 @@ Client::Client(QWidget *parent) hostLabel = new QLabel(tr("&Server name:")); portLabel = new QLabel(tr("S&erver port:")); - hostLineEdit = new QLineEdit("Localhost"); + // find out which IP to connect to + QString ipAddress; + QList<QHostAddress> 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(); + } + // 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)); |