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 | #include <QtGui>
|
---|
43 | #include <QApplication>
|
---|
44 | #include <stdio.h>
|
---|
45 |
|
---|
46 | class Widget : public QWidget
|
---|
47 | {
|
---|
48 | public:
|
---|
49 | Widget(QWidget *parent = 0);
|
---|
50 |
|
---|
51 | void constCharPointer();
|
---|
52 | void constCharArray();
|
---|
53 | void characterReference();
|
---|
54 | void atFunction();
|
---|
55 | void stringLiteral();
|
---|
56 | void modify();
|
---|
57 | void index();
|
---|
58 | QString boolToString(bool b);
|
---|
59 | void nullVsEmpty();
|
---|
60 |
|
---|
61 | void appendFunction();
|
---|
62 | void argFunction();
|
---|
63 | void chopFunction();
|
---|
64 | void compareFunction();
|
---|
65 | void compareSensitiveFunction();
|
---|
66 | void containsFunction();
|
---|
67 | void countFunction();
|
---|
68 | void dataFunction();
|
---|
69 | void endsWithFunction();
|
---|
70 | void fillFunction();
|
---|
71 | void fromRawDataFunction();
|
---|
72 |
|
---|
73 | void indexOfFunction();
|
---|
74 | void firstIndexOfFunction();
|
---|
75 | void insertFunction();
|
---|
76 | void isNullFunction();
|
---|
77 | void isEmptyFunction();
|
---|
78 | void lastIndexOfFunction();
|
---|
79 | void leftFunction();
|
---|
80 | void leftJustifiedFunction();
|
---|
81 | void leftRefFunction();
|
---|
82 | void midFunction();
|
---|
83 | void midRefFunction();
|
---|
84 | void numberFunction();
|
---|
85 |
|
---|
86 | void prependFunction();
|
---|
87 | void removeFunction();
|
---|
88 | void replaceFunction();
|
---|
89 | void reserveFunction();
|
---|
90 | void resizeFunction();
|
---|
91 | void rightFunction();
|
---|
92 | void rightJustifiedFunction();
|
---|
93 | void rightRefFunction();
|
---|
94 | void sectionFunction();
|
---|
95 | void setNumFunction();
|
---|
96 | void simplifiedFunction();
|
---|
97 |
|
---|
98 | void sizeFunction();
|
---|
99 | void splitFunction();
|
---|
100 | void splitCaseSensitiveFunction();
|
---|
101 | void sprintfFunction();
|
---|
102 | void startsWithFunction();
|
---|
103 | void toDoubleFunction();
|
---|
104 | void toFloatFunction();
|
---|
105 | void toIntFunction();
|
---|
106 | void toLongFunction();
|
---|
107 | void toLongLongFunction();
|
---|
108 |
|
---|
109 | void toLowerFunction();
|
---|
110 | void toShortFunction();
|
---|
111 | void toUIntFunction();
|
---|
112 | void toULongFunction();
|
---|
113 | void toULongLongFunction();
|
---|
114 | void toUShortFunction();
|
---|
115 | void toUpperFunction();
|
---|
116 | void trimmedFunction();
|
---|
117 | void truncateFunction();
|
---|
118 |
|
---|
119 | void plusEqualOperator();
|
---|
120 | void arrayOperator();
|
---|
121 | };
|
---|
122 |
|
---|
123 | Widget::Widget(QWidget *parent)
|
---|
124 | : QWidget(parent)
|
---|
125 | {
|
---|
126 | }
|
---|
127 |
|
---|
128 | void Widget::constCharPointer()
|
---|
129 | {
|
---|
130 | //! [0]
|
---|
131 | QString str = "Hello";
|
---|
132 | //! [0]
|
---|
133 | }
|
---|
134 |
|
---|
135 | void Widget::constCharArray()
|
---|
136 | {
|
---|
137 | //! [1]
|
---|
138 | static const QChar data[4] = { 0x0055, 0x006e, 0x10e3, 0x03a3 };
|
---|
139 | QString str(data, 4);
|
---|
140 | //! [1]
|
---|
141 | }
|
---|
142 |
|
---|
143 | void Widget::characterReference()
|
---|
144 | {
|
---|
145 | //! [2]
|
---|
146 | QString str;
|
---|
147 | str.resize(4);
|
---|
148 |
|
---|
149 | str[0] = QChar('U');
|
---|
150 | str[1] = QChar('n');
|
---|
151 | str[2] = QChar(0x10e3);
|
---|
152 | str[3] = QChar(0x03a3);
|
---|
153 | //! [2]
|
---|
154 | }
|
---|
155 |
|
---|
156 | void Widget::atFunction()
|
---|
157 | {
|
---|
158 | //! [3]
|
---|
159 | QString str;
|
---|
160 |
|
---|
161 | for (int i = 0; i < str.size(); ++i) {
|
---|
162 | if (str.at(i) >= QChar('a') && str.at(i) <= QChar('f'))
|
---|
163 | qDebug() << "Found character in range [a-f]";
|
---|
164 | }
|
---|
165 | //! [3]
|
---|
166 | }
|
---|
167 |
|
---|
168 | void Widget::stringLiteral()
|
---|
169 | {
|
---|
170 | //! [4]
|
---|
171 | QString str;
|
---|
172 |
|
---|
173 | if (str == "auto" || str == "extern"
|
---|
174 | || str == "static" || str == "register") {
|
---|
175 | // ...
|
---|
176 | }
|
---|
177 | //! [4]
|
---|
178 | }
|
---|
179 |
|
---|
180 | void Widget::modify()
|
---|
181 | {
|
---|
182 | //! [5]
|
---|
183 | QString str = "and";
|
---|
184 | str.prepend("rock "); // str == "rock and"
|
---|
185 | str.append(" roll"); // str == "rock and roll"
|
---|
186 | str.replace(5, 3, "&"); // str == "rock & roll"
|
---|
187 | //! [5]
|
---|
188 | }
|
---|
189 |
|
---|
190 | void Widget::index()
|
---|
191 | {
|
---|
192 | //! [6]
|
---|
193 | QString str = "We must be <b>bold</b>, very <b>bold</b>";
|
---|
194 | int j = 0;
|
---|
195 |
|
---|
196 | while ((j = str.indexOf("<b>", j)) != -1) {
|
---|
197 | qDebug() << "Found <b> tag at index position" << j;
|
---|
198 | ++j;
|
---|
199 | }
|
---|
200 | //! [6]
|
---|
201 | }
|
---|
202 |
|
---|
203 | //! [7]
|
---|
204 | QString Widget::boolToString(bool b)
|
---|
205 | {
|
---|
206 | QString result;
|
---|
207 | if (b)
|
---|
208 | result = "True";
|
---|
209 | else
|
---|
210 | result = "False";
|
---|
211 | return result;
|
---|
212 | }
|
---|
213 | //! [7]
|
---|
214 |
|
---|
215 |
|
---|
216 | void Widget::nullVsEmpty()
|
---|
217 | {
|
---|
218 | //! [8]
|
---|
219 | QString().isNull(); // returns true
|
---|
220 | QString().isEmpty(); // returns true
|
---|
221 |
|
---|
222 | QString("").isNull(); // returns false
|
---|
223 | QString("").isEmpty(); // returns true
|
---|
224 |
|
---|
225 | QString("abc").isNull(); // returns false
|
---|
226 | QString("abc").isEmpty(); // returns false
|
---|
227 | //! [8]
|
---|
228 | }
|
---|
229 |
|
---|
230 | void Widget::appendFunction()
|
---|
231 | {
|
---|
232 | //! [9]
|
---|
233 | QString x = "free";
|
---|
234 | QString y = "dom";
|
---|
235 |
|
---|
236 | x.append(y);
|
---|
237 | // x == "freedom"
|
---|
238 | //! [9]
|
---|
239 |
|
---|
240 | //! [10]
|
---|
241 | x.insert(x.size(), y);
|
---|
242 | //! [10]
|
---|
243 | }
|
---|
244 |
|
---|
245 | void Widget::argFunction()
|
---|
246 | {
|
---|
247 | //! [11]
|
---|
248 | QString i; // current file's number
|
---|
249 | QString total; // number of files to process
|
---|
250 | QString fileName; // current file's name
|
---|
251 |
|
---|
252 | QString status = QString("Processing file %1 of %2: %3")
|
---|
253 | .arg(i).arg(total).arg(fileName);
|
---|
254 | //! [11]
|
---|
255 |
|
---|
256 | //! [12] //! [13]
|
---|
257 | QString str;
|
---|
258 | //! [12]
|
---|
259 | str = "%1 %2";
|
---|
260 |
|
---|
261 | str.arg("%1f", "Hello"); // returns "%1f Hello"
|
---|
262 | str.arg("%1f").arg("Hello"); // returns "Hellof %2"
|
---|
263 | //! [13]
|
---|
264 |
|
---|
265 | //! [14]
|
---|
266 | str = QString("Decimal 63 is %1 in hexadecimal")
|
---|
267 | .arg(63, 0, 16);
|
---|
268 | // str == "Decimal 63 is 3f in hexadecimal"
|
---|
269 |
|
---|
270 | QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));
|
---|
271 | str = QString("%1 %L2 %L3")
|
---|
272 | .arg(12345)
|
---|
273 | .arg(12345)
|
---|
274 | .arg(12345, 0, 16);
|
---|
275 | // str == "12345 12,345 3039"
|
---|
276 | //! [14]
|
---|
277 | }
|
---|
278 |
|
---|
279 | void Widget::chopFunction()
|
---|
280 | {
|
---|
281 | //! [15]
|
---|
282 | QString str("LOGOUT\r\n");
|
---|
283 | str.chop(2);
|
---|
284 | // str == "LOGOUT"
|
---|
285 | //! [15]
|
---|
286 | }
|
---|
287 |
|
---|
288 | void Widget::compareFunction()
|
---|
289 | {
|
---|
290 | int x = QString::compare("auto", "auto"); // x == 0
|
---|
291 | int y = QString::compare("auto", "car"); // y < 0
|
---|
292 | int z = QString::compare("car", "auto"); // z > 0
|
---|
293 | }
|
---|
294 |
|
---|
295 | void Widget::compareSensitiveFunction()
|
---|
296 | {
|
---|
297 | //! [16]
|
---|
298 | int x = QString::compare("aUtO", "AuTo", Qt::CaseInsensitive); // x == 0
|
---|
299 | int y = QString::compare("auto", "Car", Qt::CaseSensitive); // y > 0
|
---|
300 | int z = QString::compare("auto", "Car", Qt::CaseInsensitive); // z < 0
|
---|
301 | //! [16]
|
---|
302 | }
|
---|
303 |
|
---|
304 | void Widget::containsFunction()
|
---|
305 | {
|
---|
306 | //! [17]
|
---|
307 | QString str = "Peter Pan";
|
---|
308 | str.contains("peter", Qt::CaseInsensitive); // returns true
|
---|
309 | //! [17]
|
---|
310 | }
|
---|
311 |
|
---|
312 | void Widget::countFunction()
|
---|
313 | {
|
---|
314 | //! [18]
|
---|
315 | QString str = "banana and panama";
|
---|
316 | str.count(QRegExp("a[nm]a")); // returns 4
|
---|
317 | //! [18]
|
---|
318 | }
|
---|
319 |
|
---|
320 | void Widget::dataFunction()
|
---|
321 | {
|
---|
322 | //! [19]
|
---|
323 | QString str = "Hello world";
|
---|
324 | QChar *data = str.data();
|
---|
325 | while (!data->isNull()) {
|
---|
326 | qDebug() << data->unicode();
|
---|
327 | ++data;
|
---|
328 | }
|
---|
329 | //! [19]
|
---|
330 | }
|
---|
331 |
|
---|
332 | void Widget::endsWithFunction()
|
---|
333 | {
|
---|
334 | //! [20]
|
---|
335 | QString str = "Bananas";
|
---|
336 | str.endsWith("anas"); // returns true
|
---|
337 | str.endsWith("pple"); // returns false
|
---|
338 | //! [20]
|
---|
339 | }
|
---|
340 |
|
---|
341 | void Widget::fillFunction()
|
---|
342 | {
|
---|
343 | //! [21]
|
---|
344 | QString str = "Berlin";
|
---|
345 | str.fill('z');
|
---|
346 | // str == "zzzzzz"
|
---|
347 |
|
---|
348 | str.fill('A', 2);
|
---|
349 | // str == "AA"
|
---|
350 | //! [21]
|
---|
351 | }
|
---|
352 |
|
---|
353 | void Widget::fromRawDataFunction()
|
---|
354 | {
|
---|
355 | //! [22]
|
---|
356 | QRegExp pattern;
|
---|
357 | static const QChar unicode[] = {
|
---|
358 | 0x005A, 0x007F, 0x00A4, 0x0060,
|
---|
359 | 0x1009, 0x0020, 0x0020};
|
---|
360 | int size = sizeof(unicode) / sizeof(QChar);
|
---|
361 |
|
---|
362 | QString str = QString::fromRawData(unicode, size);
|
---|
363 | if (str.contains(QRegExp(pattern))) {
|
---|
364 | // ...
|
---|
365 | //! [22] //! [23]
|
---|
366 | }
|
---|
367 | //! [23]
|
---|
368 | }
|
---|
369 |
|
---|
370 | void Widget::indexOfFunction()
|
---|
371 | {
|
---|
372 | //! [24]
|
---|
373 | QString x = "sticky question";
|
---|
374 | QString y = "sti";
|
---|
375 | x.indexOf(y); // returns 0
|
---|
376 | x.indexOf(y, 1); // returns 10
|
---|
377 | x.indexOf(y, 10); // returns 10
|
---|
378 | x.indexOf(y, 11); // returns -1
|
---|
379 | //! [24]
|
---|
380 | }
|
---|
381 |
|
---|
382 | void Widget::firstIndexOfFunction()
|
---|
383 | {
|
---|
384 | //! [25]
|
---|
385 | QString str = "the minimum";
|
---|
386 | str.indexOf(QRegExp("m[aeiou]"), 0); // returns 4
|
---|
387 | //! [25]
|
---|
388 | }
|
---|
389 |
|
---|
390 | void Widget::insertFunction()
|
---|
391 | {
|
---|
392 | //! [26]
|
---|
393 | QString str = "Meal";
|
---|
394 | str.insert(1, QString("ontr"));
|
---|
395 | // str == "Montreal"
|
---|
396 | //! [26]
|
---|
397 | }
|
---|
398 |
|
---|
399 | void Widget::isEmptyFunction()
|
---|
400 | {
|
---|
401 | //! [27]
|
---|
402 | QString().isEmpty(); // returns true
|
---|
403 | QString("").isEmpty(); // returns true
|
---|
404 | QString("x").isEmpty(); // returns false
|
---|
405 | QString("abc").isEmpty(); // returns false
|
---|
406 | //! [27]
|
---|
407 | }
|
---|
408 |
|
---|
409 | void Widget::isNullFunction()
|
---|
410 | {
|
---|
411 | //! [28]
|
---|
412 | QString().isNull(); // returns true
|
---|
413 | QString("").isNull(); // returns false
|
---|
414 | QString("abc").isNull(); // returns false
|
---|
415 | //! [28]
|
---|
416 | }
|
---|
417 |
|
---|
418 | void Widget::lastIndexOfFunction()
|
---|
419 | {
|
---|
420 | //! [29]
|
---|
421 | QString x = "crazy azimuths";
|
---|
422 | QString y = "az";
|
---|
423 | x.lastIndexOf(y); // returns 6
|
---|
424 | x.lastIndexOf(y, 6); // returns 6
|
---|
425 | x.lastIndexOf(y, 5); // returns 2
|
---|
426 | x.lastIndexOf(y, 1); // returns -1
|
---|
427 | //! [29]
|
---|
428 |
|
---|
429 | //! [30]
|
---|
430 | QString str = "the minimum";
|
---|
431 | str.lastIndexOf(QRegExp("m[aeiou]")); // returns 8
|
---|
432 | //! [30]
|
---|
433 | }
|
---|
434 |
|
---|
435 | void Widget::leftFunction()
|
---|
436 | {
|
---|
437 | //! [31]
|
---|
438 | QString x = "Pineapple";
|
---|
439 | QString y = x.left(4); // y == "Pine"
|
---|
440 | //! [31]
|
---|
441 | }
|
---|
442 |
|
---|
443 | void Widget::leftJustifiedFunction()
|
---|
444 | {
|
---|
445 | //! [32]
|
---|
446 | QString s = "apple";
|
---|
447 | QString t = s.leftJustified(8, '.'); // t == "apple..."
|
---|
448 | //! [32]
|
---|
449 |
|
---|
450 | //! [33]
|
---|
451 | QString str = "Pineapple";
|
---|
452 | str = str.leftJustified(5, '.', true); // str == "Pinea"
|
---|
453 | //! [33]
|
---|
|
---|