This repository has been archived by the owner on Sep 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbroadcastreceiver.qdoc
72 lines (59 loc) · 3.15 KB
/
broadcastreceiver.qdoc
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** GNU Free Documentation License
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms
** and conditions contained in a signed written agreement between you
** and Nokia.
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
/*!
\page broadcastreceiver
\example broadcastreceiver
\title Broadcast Receiver Example
\section1 Description
The Broadcast Receiever example shows how to receive information that is broadcasted
over a local network.
\image broadcastreceiver-example.png
\section1 Overview
In this example we'll learn how to use the QUdpSocket class to send and receive broadcast messages
on the network.
The example application is splitted in two executables, \l{broadcastsender}{broadcastsender} and broadcastreceiver.
While the \l{broadcastsender}{broadcastsender} allows the user to start the broadcasting of messages on the local network, the broadcastreceiver
will listen on the local network for these messages and display the received ones in the UI.
\section1 Receiver
The central class in broadcastreceiver is Receiver, which encapsulates the retrieval of the broadcasted messages
and provides a property to display the content of the received datagrams in the UI.
\snippet broadcastreceiver/assets/main.qml 0
The 'status' property of the exported Receiver object is bound against the 'text' property of a Label control, so
that whenever a new datagram arrives, the text is updated automatically on screen.
Like in the \l{broadcastsender}{broadcastsender}, the size of that label is animated here as well whenever
its content changes.
\snippet broadcastreceiver/src/Receiver.cpp 0
Inside the constructor of the Receiver class, we initialize the 'status' property and create the QUdpSocket object that
encapsulates the low-level network communication.
By calling \l{QUdpSocket::bind()}{bind()} we let the socket listen on port 45454 for incoming broadcast messages.
In the next step we connect the \l{QUdpSocket::readyRead()}{readyRead()} signal of the socket, which is emitted whenever
a new datagram arrived, against our own slot processPendingDatagrams().
\snippet broadcastreceiver/src/Receiver.cpp 1
In processPendingDatagrams() we check whether the socket still has pending datagrams and if that's the case, we read the next
datagram from the socket and store its content in the 'status' property. By emitting the 'statusChanged()' signal, we trigger
the reevaluation of all property bindings in the UI where 'status' is used.
*/