summaryrefslogtreecommitdiffstats
path: root/src/network/doc/snippets/code/src_network_kernel_qhostinfo.cpp
blob: b845e5bcc708f88e7e09bec3c24adf9af34c1d1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

//! [0]
// To find the IP address of qt-project.org
QHostInfo::lookupHost("qt-project.org",
                      this, SLOT(printResults(QHostInfo)));

// To find the host name for 4.2.2.1
QHostInfo::lookupHost("4.2.2.1",
                      this, SLOT(printResults(QHostInfo)));
//! [0]


//! [1]
QHostInfo info = QHostInfo::fromName("qt-project.org");
//! [1]


//! [2]
QHostInfo::lookupHost("www.kde.org",
                      this, SLOT(lookedUp(QHostInfo)));
//! [2]


//! [3]
void MyWidget::lookedUp(const QHostInfo &host)
{
    if (host.error() != QHostInfo::NoError) {
        qDebug() << "Lookup failed:" << host.errorString();
        return;
    }

    const auto addresses = host.addresses();
    for (const QHostAddress &address : addresses)
        qDebug() << "Found address:" << address.toString();
}
//! [3]


//! [4]
QHostInfo::lookupHost("4.2.2.1",
                      this, SLOT(lookedUp(QHostInfo)));
//! [4]


//! [5]
QHostInfo info;
...
if (!info.addresses().isEmpty()) {
    QHostAddress address = info.addresses().first();
    // use the first IP address
}
//! [5]