source: trunk/doc/src/snippets/code/src_qdbus_qdbusargument.cpp@ 728

Last change on this file since 728 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 5.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42//! [0]
43struct MyStructure
44{
45 int count;
46 QString name;
47};
48Q_DECLARE_METATYPE(MyStructure)
49
50// Marshall the MyStructure data into a D-Bus argument
51QDBusArgument &operator<<(QDBusArgument &argument, const MyStructure &mystruct)
52{
53 argument.beginStructure();
54 argument << mystruct.count << mystruct.name;
55 argument.endStructure();
56 return argument;
57}
58
59// Retrieve the MyStructure data from the D-Bus argument
60const QDBusArgument &operator>>(const QDBusArgument &argument, MyStructure &mystruct)
61{
62 argument.beginStructure();
63 argument >> mystruct.count >> mystruct.name;
64 argument.endStructure();
65 return argument;
66}
67//! [0]
68
69
70//! [1]
71qDBusRegisterMetaType<MyStructure>();
72//! [1]
73
74
75//! [2]
76MyType item = qdbus_cast<Type>(argument);
77//! [2]
78
79
80//! [3]
81MyType item;
82argument >> item;
83//! [3]
84
85
86//! [4]
87QDBusArgument &operator<<(QDBusArgument &argument, const MyStructure &mystruct)
88{
89 argument.beginStructure();
90 argument << mystruct.member1 << mystruct.member2 << ... ;
91 argument.endStructure();
92 return argument;
93}
94//! [4]
95
96
97//! [5]
98QDBusArgument &operator<<(QDBusArgument &argument, const MyStructure &mystruct)
99{
100 argument.beginStructure();
101 argument << mystruct.member1 << mystruct.member2;
102
103 argument.beginStructure();
104 argument << mystruct.member3.subMember1 << mystruct.member3.subMember2;
105 argument.endStructure();
106
107 argument << mystruct.member4;
108 argument.endStructure();
109 return argument;
110}
111//! [5]
112
113
114//! [6]
115// append an array of MyElement types
116QDBusArgument &operator<<(QDBusArgument &argument, const MyArray &myarray)
117{
118 argument.beginArray( qMetaTypeId<MyElement>() );
119 for ( int i = 0; i < myarray.length; ++i )
120 argument << myarray.elements[i];
121 argument.endArray();
122 return argument;
123}
124//! [6]
125
126
127//! [7]
128// append a dictionary that associates ints to MyValue types
129QDBusArgument &operator<<(QDBusArgument &argument, const MyDictionary &mydict)
130{
131 argument.beginMap( QVariant::Int, qMetaTypeId<MyValue>() );
132 for ( int i = 0; i < mydict.length; ++i ) {
133 argument.beginMapEntry();
134 argument << mydict.data[i].key << mydict.data[i].value;
135 argument.endMapEntry();
136 }
137 argument.endMap();
138 return argument;
139}
140//! [7]
141
142
143//! [8]
144const QDBusArgument &operator>>(const QDBusArgument &argument, MyStructure &mystruct)
145{
146 argument.beginStructure()
147 argument >> mystruct.member1 >> mystruct.member2 >> mystruct.member3 >> ...;
148 argument.endStructure();
149 return argument;
150}
151//! [8]
152
153
154//! [9]
155// extract a MyArray array of MyElement elements
156const QDBusArgument &operator>>(const QDBusArgument &argument, MyArray &myarray)
157{
158 argument.beginArray();
159 myarray.clear();
160
161 while ( !argument.atEnd() ) {
162 MyElement element;
163 argument >> element;
164 myarray.append( element );
165 }
166
167 argument.endArray();
168 return argument;
169}
170//! [9]
171
172
173//! [10]
174// extract a MyDictionary map that associates ints to MyValue elements
175const QDBusArgument &operator>>(const QDBusArgument &argument, MyDictionary &mydict)
176{
177 argument.beginMap();
178 mydict.clear();
179
180 while ( !argMap.atEnd() ) {
181 int key;
182 MyValue value;
183 argument.beginMapEntry();
184 argument >> key >> value;
185 argument.endMapEntry();
186 mydict.append( key, value );
187 }
188
189 argument.endMap();
190 return argument;
191}
192//! [10]
Note: See TracBrowser for help on using the repository browser.