1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 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:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 | #include <QApplication>
|
---|
43 | #include <stdio.h>
|
---|
44 |
|
---|
45 | class Widget : public QWidget
|
---|
46 | {
|
---|
47 | public:
|
---|
48 | Widget(QWidget *parent = 0);
|
---|
49 |
|
---|
50 | void constCharPointer();
|
---|
51 | void constCharArray();
|
---|
52 | void characterReference();
|
---|
53 | void atFunction();
|
---|
54 | void stringLiteral();
|
---|
55 | void modify();
|
---|
56 | void index();
|
---|
57 | QString boolToString(bool b);
|
---|
58 | void nullVsEmpty();
|
---|
59 |
|
---|
60 | void appendFunction();
|
---|
61 | void argFunction();
|
---|
62 | void chopFunction();
|
---|
63 | void compareFunction();
|
---|
64 | void compareSensitiveFunction();
|
---|
65 | void containsFunction();
|
---|
66 | void countFunction();
|
---|
67 | void dataFunction();
|
---|
68 | void endsWithFunction();
|
---|
69 | void fillFunction();
|
---|
70 | void fromRawDataFunction();
|
---|
71 |
|
---|
72 | void indexOfFunction();
|
---|
73 | void firstIndexOfFunction();
|
---|
74 | void insertFunction();
|
---|
75 | void isNullFunction();
|
---|
76 | void isEmptyFunction();
|
---|
77 | void lastIndexOfFunction();
|
---|
78 | void leftFunction();
|
---|
79 | void leftJustifiedFunction();
|
---|
80 | void leftRefFunction();
|
---|
81 | void midFunction();
|
---|
82 | void midRefFunction();
|
---|
83 | void numberFunction();
|
---|
84 |
|
---|
85 | void prependFunction();
|
---|
86 | void removeFunction();
|
---|
87 | void replaceFunction();
|
---|
88 | void reserveFunction();
|
---|
89 | void resizeFunction();
|
---|
90 | void rightFunction();
|
---|
91 | void rightJustifiedFunction();
|
---|
92 | void rightRefFunction();
|
---|
93 | void sectionFunction();
|
---|
94 | void setNumFunction();
|
---|
95 | void simplifiedFunction();
|
---|
96 |
|
---|
97 | void sizeFunction();
|
---|
98 | void splitFunction();
|
---|
99 | void splitCaseSensitiveFunction();
|
---|
100 | void sprintfFunction();
|
---|
101 | void startsWithFunction();
|
---|
102 | void toDoubleFunction();
|
---|
103 | void toFloatFunction();
|
---|
104 | void toIntFunction();
|
---|
105 | void toLongFunction();
|
---|
106 | void toLongLongFunction();
|
---|
107 |
|
---|
108 | void toLowerFunction();
|
---|
109 | void toShortFunction();
|
---|
110 | void toUIntFunction();
|
---|
111 | void toULongFunction();
|
---|
112 | void toULongLongFunction();
|
---|
113 | void toUShortFunction();
|
---|
114 | void toUpperFunction();
|
---|
115 | void trimmedFunction();
|
---|
116 | void truncateFunction();
|
---|
117 |
|
---|
118 | void plusEqualOperator();
|
---|
119 | void arrayOperator();
|
---|
120 | };
|
---|
121 |
|
---|
122 | Widget::Widget(QWidget *parent)
|
---|
123 | : QWidget(parent)
|
---|
124 | {
|
---|
125 | }
|
---|
126 |
|
---|
127 | void Widget::constCharPointer()
|
---|
128 | {
|
---|
129 | //! [0]
|
---|
130 | QString str = "Hello";
|
---|
131 | //! [0]
|
---|
132 | }
|
---|
133 |
|
---|
134 | void Widget::constCharArray()
|
---|
135 | {
|
---|
136 | //! [1]
|
---|
137 | static const QChar data[4] = { 0x0055, 0x006e, 0x10e3, 0x03a3 };
|
---|
138 | QString str(data, 4);
|
---|
139 | //! [1]
|
---|
140 | }
|
---|
141 |
|
---|
142 | void Widget::characterReference()
|
---|
143 | {
|
---|
144 | //! [2]
|
---|
145 | QString str;
|
---|
146 | str.resize(4);
|
---|
147 |
|
---|
148 | str[0] = QChar('U');
|
---|
149 | str[1] = QChar('n');
|
---|
150 | str[2] = QChar(0x10e3);
|
---|
151 | str[3] = QChar(0x03a3);
|
---|
152 | //! [2]
|
---|
153 | }
|
---|
154 |
|
---|
155 | void Widget::atFunction()
|
---|
156 | {
|
---|
157 | //! [3]
|
---|
158 | QString str;
|
---|
159 |
|
---|
160 | for (int i = 0; i < str.size(); ++i) {
|
---|
161 | if (str.at(i) >= QChar('a') && str.at(i) <= QChar('f'))
|
---|
162 | qDebug() << "Found character in range [a-f]";
|
---|
163 | }
|
---|
164 | //! [3]
|
---|
165 | }
|
---|
166 |
|
---|
167 | void Widget::stringLiteral()
|
---|
168 | {
|
---|
169 | //! [4]
|
---|
170 | QString str;
|
---|
171 |
|
---|
172 | if (str == "auto" || str == "extern"
|
---|
173 | || str == "static" || str == "register") {
|
---|
174 | // ...
|
---|
175 | }
|
---|
176 | //! [4]
|
---|
177 | }
|
---|
178 |
|
---|
179 | void Widget::modify()
|
---|
180 | {
|
---|
181 | //! [5]
|
---|
182 | QString str = "and";
|
---|
183 | str.prepend("rock "); // str == "rock and"
|
---|
184 | str.append(" roll"); // str == "rock and roll"
|
---|
185 | str.replace(5, 3, "&"); // str == "rock & roll"
|
---|
186 | //! [5]
|
---|
187 | }
|
---|
188 |
|
---|
189 | void Widget::index()
|
---|
190 | {
|
---|
191 | //! [6]
|
---|
192 | QString str = "We must be <b>bold</b>, very <b>bold</b>";
|
---|
193 | int j = 0;
|
---|
194 |
|
---|
195 | while ((j = str.indexOf("<b>", j)) != -1) {
|
---|
196 | qDebug() << "Found <b> tag at index position" << j;
|
---|
197 | ++j;
|
---|
198 | }
|
---|
199 | //! [6]
|
---|
200 | }
|
---|
201 |
|
---|
202 | //! [7]
|
---|
203 | QString Widget::boolToString(bool b)
|
---|
204 | {
|
---|
205 | QString result;
|
---|
206 | if (b)
|
---|
207 | result = "True";
|
---|
208 | else
|
---|
209 | result = "False";
|
---|
210 | return result;
|
---|
211 | }
|
---|
212 | //! [7]
|
---|
213 |
|
---|
214 |
|
---|
215 | void Widget::nullVsEmpty()
|
---|
216 | {
|
---|
217 | //! [8]
|
---|
218 | QString().isNull(); // returns true
|
---|
219 | QString().isEmpty(); // returns true
|
---|
220 |
|
---|
221 | QString("").isNull(); // returns false
|
---|
222 | QString("").isEmpty(); // returns true
|
---|
223 |
|
---|
224 | QString("abc").isNull(); // returns false
|
---|
225 | QString("abc").isEmpty(); // returns false
|
---|
226 | //! [8]
|
---|
227 | }
|
---|
228 |
|
---|
229 | void Widget::appendFunction()
|
---|
230 | {
|
---|
231 | //! [9]
|
---|
232 | QString x = "free";
|
---|
233 | QString y = "dom";
|
---|
234 |
|
---|
235 | x.append(y);
|
---|
236 | // x == "freedom"
|
---|
237 | //! [9]
|
---|
238 |
|
---|
239 | //! [10]
|
---|
240 | x.insert(x.size(), y);
|
---|
241 | //! [10]
|
---|
242 | }
|
---|
243 |
|
---|
244 | void Widget::argFunction()
|
---|
245 | {
|
---|
246 | //! [11]
|
---|
247 | QString i; // current file's number
|
---|
248 | QString total; // number of files to process
|
---|
249 | QString fileName; // current file's name
|
---|
250 |
|
---|
251 | QString status = QString("Processing file %1 of %2: %3")
|
---|
252 | .arg(i).arg(total).arg(fileName);
|
---|
253 | //! [11]
|
---|
254 |
|
---|
255 | //! [12] //! [13]
|
---|
256 | QString str;
|
---|
257 | //! [12]
|
---|
258 | str = "%1 %2";
|
---|
259 |
|
---|
260 | str.arg("%1f", "Hello"); // returns "%1f Hello"
|
---|
261 | str.arg("%1f").arg("Hello"); // returns "Hellof %2"
|
---|
262 | //! [13]
|
---|
263 |
|
---|
264 | //! [14]
|
---|
265 | str = QString("Decimal 63 is %1 in hexadecimal")
|
---|
266 | .arg(63, 0, 16);
|
---|
267 | // str == "Decimal 63 is 3f in hexadecimal"
|
---|
268 |
|
---|
269 | QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));
|
---|
270 | str = QString("%1 %L2 %L3")
|
---|
271 | .arg(12345)
|
---|
272 | .arg(12345)
|
---|
273 | .arg(12345, 0, 16);
|
---|
274 | // str == "12345 12,345 3039"
|
---|
275 | //! [14]
|
---|
276 | }
|
---|
277 |
|
---|
278 | void Widget::chopFunction()
|
---|
279 | {
|
---|
280 | //! [15]
|
---|
281 | QString str("LOGOUT\r\n");
|
---|
282 | str.chop(2);
|
---|
283 | // str == "LOGOUT"
|
---|
284 | //! [15]
|
---|
285 | }
|
---|
286 |
|
---|
287 | void Widget::compareFunction()
|
---|
288 | {
|
---|
289 | int x = QString::compare("auto", "auto"); // x == 0
|
---|
290 | int y = QString::compare("auto", "car"); // y < 0
|
---|
291 | int z = QString::compare("car", "auto"); // z > 0
|
---|
292 | }
|
---|
293 |
|
---|
294 | void Widget::compareSensitiveFunction()
|
---|
295 | {
|
---|
296 | //! [16]
|
---|
297 | int x = QString::compare("aUtO", "AuTo", Qt::CaseInsensitive); // x == 0
|
---|
298 | int y = QString::compare("auto", "Car", Qt::CaseSensitive); // y > 0
|
---|
299 | int z = QString::compare("auto", "Car", Qt::CaseInsensitive); // z < 0
|
---|
300 | //! [16]
|
---|
301 | }
|
---|
302 |
|
---|
303 | void Widget::containsFunction()
|
---|
304 | {
|
---|
305 | //! [17]
|
---|
306 | QString str = "Peter Pan";
|
---|
307 | str.contains("peter", Qt::CaseInsensitive); // returns true
|
---|
308 | //! [17]
|
---|
309 | }
|
---|
310 |
|
---|
311 | void Widget::countFunction()
|
---|
312 | {
|
---|
313 | //! [18]
|
---|
314 | QString str = "banana and panama";
|
---|
315 | str.count(QRegExp("a[nm]a")); // returns 4
|
---|
316 | //! [18]
|
---|
317 | }
|
---|
318 |
|
---|
319 | void Widget::dataFunction()
|
---|
320 | {
|
---|
321 | //! [19]
|
---|
322 | QString str = "Hello world";
|
---|
323 | QChar *data = str.data();
|
---|
324 | while (!data->isNull()) {
|
---|
325 | qDebug() << data->unicode();
|
---|
326 | ++data;
|
---|
327 | }
|
---|
328 | //! [19]
|
---|
329 | }
|
---|
330 |
|
---|
331 | void Widget::endsWithFunction()
|
---|
332 | {
|
---|
333 | //! [20]
|
---|
334 | QString str = "Bananas";
|
---|
335 | str.endsWith("anas"); // returns true
|
---|
336 | str.endsWith("pple"); // returns false
|
---|
337 | //! [20]
|
---|
338 | }
|
---|
339 |
|
---|
340 | void Widget::fillFunction()
|
---|
341 | {
|
---|
342 | //! [21]
|
---|
343 | QString str = "Berlin";
|
---|
344 | str.fill('z');
|
---|
345 | // str == "zzzzzz"
|
---|
346 |
|
---|
347 | str.fill('A', 2);
|
---|
348 | // str == "AA"
|
---|
349 | //! [21]
|
---|
350 | }
|
---|
351 |
|
---|
352 | void Widget::fromRawDataFunction()
|
---|
353 | {
|
---|
354 | //! [22]
|
---|
355 | QRegExp pattern;
|
---|
356 | static const QChar unicode[] = {
|
---|
357 | 0x005A, 0x007F, 0x00A4, 0x0060,
|
---|
358 | 0x1009, 0x0020, 0x0020};
|
---|
359 | int size = sizeof(unicode) / sizeof(QChar);
|
---|
360 |
|
---|
361 | QString str = QString::fromRawData(unicode, size);
|
---|
362 | if (str.contains(QRegExp(pattern))) {
|
---|
363 | // ...
|
---|
364 | //! [22] //! [23]
|
---|
365 | }
|
---|
366 | //! [23]
|
---|
367 | }
|
---|
368 |
|
---|
369 | void Widget::indexOfFunction()
|
---|
370 | {
|
---|
371 | //! [24]
|
---|
372 | QString x = "sticky question";
|
---|
373 | QString y = "sti";
|
---|
374 | x.indexOf(y); // returns 0
|
---|
375 | x.indexOf(y, 1); // returns 10
|
---|
376 | x.indexOf(y, 10); // returns 10
|
---|
377 | x.indexOf(y, 11); // returns -1
|
---|
378 | //! [24]
|
---|
379 | }
|
---|
380 |
|
---|
381 | void Widget::firstIndexOfFunction()
|
---|
382 | {
|
---|
383 | //! [25]
|
---|
384 | QString str = "the minimum";
|
---|
385 | str.indexOf(QRegExp("m[aeiou]"), 0); // returns 4
|
---|
386 | //! [25]
|
---|
387 | }
|
---|
388 |
|
---|
389 | void Widget::insertFunction()
|
---|
390 | {
|
---|
391 | //! [26]
|
---|
392 | QString str = "Meal";
|
---|
393 | str.insert(1, QString("ontr"));
|
---|
394 | // str == "Montreal"
|
---|
395 | //! [26]
|
---|
396 | }
|
---|
397 |
|
---|
398 | void Widget::isEmptyFunction()
|
---|
399 | {
|
---|
400 | //! [27]
|
---|
401 | QString().isEmpty(); // returns true
|
---|
402 | QString("").isEmpty(); // returns true
|
---|
403 | QString("x").isEmpty(); // returns false
|
---|
404 | QString("abc").isEmpty(); // returns false
|
---|
405 | //! [27]
|
---|
406 | }
|
---|
407 |
|
---|
408 | void Widget::isNullFunction()
|
---|
409 | {
|
---|
410 | //! [28]
|
---|
411 | QString().isNull(); // returns true
|
---|
412 | QString("").isNull(); // returns false
|
---|
413 | QString("abc").isNull(); // returns false
|
---|
414 | //! [28]
|
---|
415 | }
|
---|
416 |
|
---|
417 | void Widget::lastIndexOfFunction()
|
---|
418 | {
|
---|
419 | //! [29]
|
---|
420 | QString x = "crazy azimuths";
|
---|
421 | QString y = "az";
|
---|
422 | x.lastIndexOf(y); // returns 6
|
---|
423 | x.lastIndexOf(y, 6); // returns 6
|
---|
424 | x.lastIndexOf(y, 5); // returns 2
|
---|
425 | x.lastIndexOf(y, 1); // returns -1
|
---|
426 | //! [29]
|
---|
427 |
|
---|
428 | //! [30]
|
---|
429 | QString str = "the minimum";
|
---|
430 | str.lastIndexOf(QRegExp("m[aeiou]")); // returns 8
|
---|
431 | //! [30]
|
---|
432 | }
|
---|
433 |
|
---|
434 | void Widget::leftFunction()
|
---|
435 | {
|
---|
436 | //! [31]
|
---|
437 | QString x = "Pineapple";
|
---|
438 | QString y = x.left(4); // y == "Pine"
|
---|
439 | //! [31]
|
---|
440 | }
|
---|
441 |
|
---|
442 | void Widget::leftJustifiedFunction()
|
---|
443 | {
|
---|
444 | //! [32]
|
---|
445 | QString s = "apple";
|
---|
446 | QString t = s.leftJustified(8, '.'); // t == "apple..."
|
---|
447 | //! [32]
|
---|
448 |
|
---|
449 | //! [33]
|
---|
450 | QString str = "Pineapple";
|
---|
451 | str = str.leftJustified(5, '.', true); // str == "Pinea"
|
---|
452 | //! [33]
|
---|
453 | }
|
---|
454 |
|
---|
455 | void Widget::midFunction()
|
---|
456 | {
|
---|
457 | //! [34]
|
---|
458 | QString x = "Nine pineapples";
|
---|
459 | QString y = x.mid(5, 4); // y == "pine"
|
---|
460 | QString z = x.mid(5); // z == "pineapples"
|
---|
461 | //! [34]
|
---|
462 | }
|
---|
463 |
|
---|
464 | void Widget::numberFunction()
|
---|
465 | {
|
---|
466 | //! [35]
|
---|
467 | long a = 63;
|
---|
468 | QString s = QString::number(a, 16); // s == "3f"
|
---|
469 | QString t = QString::number(a, 16).toUpper(); // t == "3F"
|
---|
470 | //! [35]
|
---|
471 | }
|
---|
472 |
|
---|
473 | void Widget::prependFunction()
|
---|
474 | {
|
---|
475 | //! [36]
|
---|
476 | QString x = "ship";
|
---|
477 | QString y = "air";
|
---|
478 | x.prepend(y);
|
---|
479 | // x == "airship"
|
---|
480 | //! [36]
|
---|
481 | }
|
---|
482 |
|
---|
483 | void Widget::removeFunction()
|
---|
484 | {
|
---|
485 | //! [37]
|
---|
486 | QString s = "Montreal";
|
---|
487 | s.remove(1, 4);
|
---|
488 | // s == "Meal"
|
---|
489 | //! [37]
|
---|
490 |
|
---|
491 | //! [38]
|
---|
492 | QString t = "Ali Baba";
|
---|
493 | t.remove(QChar('a'), Qt::CaseInsensitive);
|
---|
494 | // t == "li Bb"
|
---|
495 | //! [38]
|
---|
496 |
|
---|
497 | //! [39]
|
---|
498 | QString r = "Telephone";
|
---|
499 | r.remove(QRegExp("[aeiou]."));
|
---|
500 | // r == "The"
|
---|
501 | //! [39]
|
---|
502 | }
|
---|
503 |
|
---|
504 | void Widget::replaceFunction()
|
---|
505 | {
|
---|
506 | //! [40]
|
---|
507 | QString x = "Say yes!";
|
---|
508 | QString y = "no";
|
---|
509 | x.replace(4, 3, y);
|
---|
510 | // x == "Say no!"
|
---|
511 | //! [40]
|
---|
512 |
|
---|
513 | //! [41]
|
---|
514 | QString str = "colour behaviour flavour neighbour";
|
---|
515 | str.replace(QString("ou"), QString("o"));
|
---|
516 | // str == "color behavior flavor neighbor"
|
---|
517 | //! [41]
|
---|
518 |
|
---|
519 | //! [42]
|
---|
520 | QString s = "Banana";
|
---|
521 | s.replace(QRegExp("a[mn]"), "ox");
|
---|
522 | // s == "Boxoxa"
|
---|
523 | //! [42]
|
---|
524 |
|
---|
525 | //! [43]
|
---|
526 | QString t = "A <i>bon mot</i>.";
|
---|
527 | t.replace(QRegExp("<i>([^<]*)</i>"), "\\emph{\\1}");
|
---|
528 | // t == "A \\emph{bon mot}."
|
---|
529 | //! [43]
|
---|
530 |
|
---|
531 | //! [86]
|
---|
532 | QString equis = "xxxxxx";
|
---|
533 | equis.replace("xx", "x");
|
---|
534 | // equis == "xxx"
|
---|
535 | //! [86]
|
---|
536 | }
|
---|
537 |
|
---|
538 | void Widget::reserveFunction()
|
---|
539 | {
|
---|
540 | //! [44]
|
---|
541 | QString result;
|
---|
542 | int maxSize;
|
---|
543 | bool condition;
|
---|
544 | QChar nextChar;
|
---|
545 |
|
---|
546 | result.reserve(maxSize);
|
---|
547 |
|
---|
548 | while (condition)
|
---|
549 | result.append(nextChar);
|
---|
550 |
|
---|
551 | result.squeeze();
|
---|
552 | //! [44]
|
---|
553 | }
|
---|
554 |
|
---|
555 | void Widget::resizeFunction()
|
---|
556 | {
|
---|
557 | //! [45]
|
---|
558 | QString s = "Hello world";
|
---|
559 | s.resize(5);
|
---|
560 | // s == "Hello"
|
---|
561 |
|
---|
562 | s.resize(8);
|
---|
563 | // s == "Hello???" (where ? stands for any character)
|
---|
564 | //! [45]
|
---|
565 |
|
---|
566 | //! [46]
|
---|
567 | QString t = "Hello";
|
---|
568 | t += QString(10, 'X');
|
---|
569 | // t == "HelloXXXXXXXXXX"
|
---|
570 | //! [46]
|
---|
571 |
|
---|
572 | //! [47]
|
---|
573 | QString r = "Hello";
|
---|
574 | r = r.leftJustified(10, ' ');
|
---|
575 | // r == "Hello "
|
---|
576 | //! [47]
|
---|
577 | }
|
---|
578 |
|
---|
579 | void Widget::rightFunction()
|
---|
580 | {
|
---|
581 | //! [48]
|
---|
582 | QString x = "Pineapple";
|
---|
583 | QString y = x.right(5); // y == "apple"
|
---|
584 | //! [48]
|
---|
585 | }
|
---|
586 |
|
---|
587 | void Widget::rightJustifiedFunction()
|
---|
588 | {
|
---|
589 | //! [49]
|
---|
590 | QString s = "apple";
|
---|
591 | QString t = s.rightJustified(8, '.'); // t == "...apple"
|
---|
592 | //! [49]
|
---|
593 |
|
---|
594 | //! [50]
|
---|
595 | QString str = "Pineapple";
|
---|
596 | str = str.rightJustified(5, '.', true); // str == "Pinea"
|
---|
597 | //! [50]
|
---|
598 | }
|
---|
599 |
|
---|
600 | void Widget::sectionFunction()
|
---|
601 | {
|
---|
602 | //! [51] //! [52]
|
---|
603 | QString str;
|
---|
604 | //! [51]
|
---|
605 | QString csv = "forename,middlename,surname,phone";
|
---|
606 | QString path = "/usr/local/bin/myapp"; // First field is empty
|
---|
607 | QString::SectionFlag flag = QString::SectionSkipEmpty;
|
---|
608 |
|
---|
609 |
|
---|
610 | str = csv.section(',', 2, 2); // str == "surname"
|
---|
611 | str = path.section('/', 3, 4); // str == "bin/myapp"
|
---|
612 | str = path.section('/', 3, 3, flag); // str == "myapp"
|
---|
613 | //! [52]
|
---|
614 |
|
---|
615 | //! [53]
|
---|
616 | str = csv.section(',', -3, -2); // str == "middlename,surname"
|
---|
617 | str = path.section('/', -1); // str == "myapp"
|
---|
618 | //! [53]
|
---|
619 |
|
---|
620 | //! [54]
|
---|
621 | QString data = "forename**middlename**surname**phone";
|
---|
622 |
|
---|
623 | str = data.section("**", 2, 2); // str == "surname"
|
---|
624 | str = data.section("**", -3, -2); // str == "middlename**surname"
|
---|
625 | //! [54]
|
---|
626 |
|
---|
627 | //! [55]
|
---|
628 | QString line = "forename\tmiddlename surname \t \t phone";
|
---|
629 | QRegExp sep("\\s+");
|
---|
630 | str = line.section(sep, 2, 2); // s == "surname"
|
---|
631 | str = line.section(sep, -3, -2); // s == "middlename surname"
|
---|
632 | //! [55]
|
---|
633 | }
|
---|
634 |
|
---|
635 | void Widget::setNumFunction()
|
---|
636 | {
|
---|
637 | //! [56]
|
---|
638 | QString str;
|
---|
639 | str.setNum(1234); // str == "1234"
|
---|
640 | //! [56]
|
---|
641 | }
|
---|
642 |
|
---|
643 | void Widget::simplifiedFunction()
|
---|
644 | {
|
---|
645 | //! [57]
|
---|
646 | QString str = " lots\t of\nwhitespace\r\n ";
|
---|
647 | str = str.simplified();
|
---|
648 | // str == "lots of whitespace";
|
---|
649 | //! [57]
|
---|
650 | }
|
---|
651 |
|
---|
652 | void Widget::sizeFunction()
|
---|
653 | {
|
---|
654 | //! [58]
|
---|
655 | QString str = "World";
|
---|
656 | int n = str.size(); // n == 5
|
---|
657 | str.data()[0]; // returns 'W'
|
---|
658 | str.data()[4]; // returns 'd'
|
---|
659 | str.data()[5]; // returns '\0'
|
---|
660 | //! [58]
|
---|
661 | }
|
---|
662 |
|
---|
663 | void Widget::splitFunction()
|
---|
664 | {
|
---|
665 | //! [59]
|
---|
666 | QString str;
|
---|
667 | QStringList list;
|
---|
668 |
|
---|
669 | str = "Some text\n\twith strange whitespace.";
|
---|
670 | list = str.split(QRegExp("\\s+"));
|
---|
671 | // list: [ "Some", "text", "with", "strange", "whitespace." ]
|
---|
672 | //! [59]
|
---|
673 |
|
---|
674 | //! [60]
|
---|
675 | str = "This time, a normal English sentence.";
|
---|
676 | list = str.split(QRegExp("\\W+"), QString::SkipEmptyParts);
|
---|
677 | // list: [ "This", "time", "a", "normal", "English", "sentence" ]
|
---|
678 | //! [60]
|
---|
679 |
|
---|
680 | //! [61]
|
---|
681 | str = "Now: this sentence fragment.";
|
---|
682 | list = str.split(QRegExp("\\b"));
|
---|
683 | // list: [ "", "Now", ": ", "this", " ", "sentence", " ", "fragment", "." ]
|
---|
684 | //! [61]
|
---|
685 | }
|
---|
686 |
|
---|
687 | void Widget::splitCaseSensitiveFunction()
|
---|
688 | {
|
---|
689 | //! [62]
|
---|
690 | QString str = "a,,b,c";
|
---|
691 |
|
---|
692 | QStringList list1 = str.split(",");
|
---|
693 | // list1: [ "a", "", "b", "c" ]
|
---|
694 |
|
---|
695 | QStringList list2 = str.split(",", QString::SkipEmptyParts);
|
---|
696 | // list2: [ "a", "b", "c" ]
|
---|
697 | //! [62]
|
---|
698 | }
|
---|
699 |
|
---|
700 | void Widget::sprintfFunction()
|
---|
701 | {
|
---|
702 | //! [63]
|
---|
703 | size_t BufSize;
|
---|
704 | char buf[BufSize];
|
---|
705 |
|
---|
706 | ::snprintf(buf, BufSize, "%lld", 123456789LL);
|
---|
707 | QString str = QString::fromAscii(buf);
|
---|
708 | //! [63]
|
---|
709 |
|
---|
710 | //! [64]
|
---|
711 | QString result;
|
---|
712 | QTextStream(&result) << "pi = " << 3.14;
|
---|
713 | // result == "pi = 3.14"
|
---|
714 | //! [64]
|
---|
715 | }
|
---|
716 |
|
---|
717 | void Widget::startsWithFunction()
|
---|
718 | {
|
---|
719 | //! [65]
|
---|
720 | QString str = "Bananas";
|
---|
721 | str.startsWith("Ban"); // returns true
|
---|
722 | str.startsWith("Car"); // returns false
|
---|
723 | //! [65]
|
---|
724 | }
|
---|
725 |
|
---|
726 | void Widget::toDoubleFunction()
|
---|
727 | {
|
---|
728 | //! [66]
|
---|
729 | QString str = "1234.56";
|
---|
730 | double val = str.toDouble(); // val == 1234.56
|
---|
731 | //! [66]
|
---|
732 |
|
---|
733 | //! [67]
|
---|
734 | bool ok;
|
---|
735 | double d;
|
---|
736 |
|
---|
737 | d = QString( "1234.56e-02" ).toDouble(&ok); // ok == true, d == 12.3456
|
---|
738 | //! [67]
|
---|
739 |
|
---|
740 | //! [68] //! [69]
|
---|
741 | QLocale::setDefault(QLocale::C);
|
---|
742 | d = QString( "1234,56" ).toDouble(&ok); // ok == false
|
---|
743 | //! [68]
|
---|
744 | d = QString( "1234.56" ).toDouble(&ok); // ok == true, d == 1234.56
|
---|
745 |
|
---|
746 | //! [69] //! [70]
|
---|
747 | QLocale::setDefault(QLocale::German);
|
---|
748 | d = QString( "1234,56" ).toDouble(&ok); // ok == true, d == 1234.56
|
---|
749 | d = QString( "1234.56" ).toDouble(&ok); // ok == true, d == 1234.56
|
---|
750 |
|
---|
751 | //! [70]
|
---|
752 | QLocale::setDefault(QLocale::C);
|
---|
753 | d = QString( "1,234,567.89" ).toDouble(&ok); // ok == false
|
---|
754 | }
|
---|
755 |
|
---|
756 | void Widget::toFloatFunction()
|
---|
757 | {
|
---|
758 | //! [71]
|
---|
759 | QString str1 = "1234.56";
|
---|
760 | str1.toFloat(); // returns 1234.56
|
---|
761 |
|
---|
762 | bool ok;
|
---|
763 | QString str2 = "R2D2";
|
---|
764 | str2.toFloat(&ok); // returns 0.0, sets ok to false
|
---|
765 | //! [71]
|
---|
766 | }
|
---|
767 |
|
---|
768 | void Widget::toIntFunction()
|
---|
769 | {
|
---|
770 | //! [72]
|
---|
771 | QString str = "FF";
|
---|
772 | bool ok;
|
---|
773 | int hex = str.toInt(&ok, 16); // hex == 255, ok == true
|
---|
774 | int dec = str.toInt(&ok, 10); // dec == 0, ok == false
|
---|
775 | //! [72]
|
---|
776 | }
|
---|
777 |
|
---|
778 | void Widget::toLongFunction()
|
---|
779 | {
|
---|
780 | //! [73]
|
---|
781 | QString str = "FF";
|
---|
782 | bool ok;
|
---|
783 |
|
---|
784 | long hex = str.toLong(&ok, 16); // hex == 255, ok == true
|
---|
785 | long dec = str.toLong(&ok, 10); // dec == 0, ok == false
|
---|
786 | //! [73]
|
---|
787 | }
|
---|
788 |
|
---|
789 | void Widget::toLongLongFunction()
|
---|
790 | {
|
---|
791 | //! [74]
|
---|
792 | QString str = "FF";
|
---|
793 | bool ok;
|
---|
794 |
|
---|
795 | qint64 hex = str.toLongLong(&ok, 16); // hex == 255, ok == true
|
---|
796 | qint64 dec = str.toLongLong(&ok, 10); // dec == 0, ok == false
|
---|
797 | //! [74]
|
---|
798 | }
|
---|
799 |
|
---|
800 | void Widget::toLowerFunction()
|
---|
801 | {
|
---|
802 | //! [75]
|
---|
803 | QString str = "Qt by NOKIA";
|
---|
804 | str = str.toLower(); // str == "qt by nokia"
|
---|
805 | //! [75]
|
---|
806 | }
|
---|
807 |
|
---|
808 | void Widget::toShortFunction()
|
---|
809 | {
|
---|
810 | //! [76]
|
---|
811 | QString str = "FF";
|
---|
812 | bool ok;
|
---|
813 |
|
---|
814 | short hex = str.toShort(&ok, 16); // hex == 255, ok == true
|
---|
815 | short dec = str.toShort(&ok, 10); // dec == 0, ok == false
|
---|
816 | //! [76]
|
---|
817 | }
|
---|
818 |
|
---|
819 | void Widget::toUIntFunction()
|
---|
820 | {
|
---|
821 | //! [77]
|
---|
822 | QString str = "FF";
|
---|
823 | bool ok;
|
---|
824 |
|
---|
825 | uint hex = str.toUInt(&ok, 16); // hex == 255, ok == true
|
---|
826 | uint dec = str.toUInt(&ok, 10); // dec == 0, ok == false
|
---|
827 | //! [77]
|
---|
828 | }
|
---|
829 |
|
---|
830 | void Widget::toULongFunction()
|
---|
831 | {
|
---|
832 | //! [78]
|
---|
833 | QString str = "FF";
|
---|
834 | bool ok;
|
---|
835 |
|
---|
836 | ulong hex = str.toULong(&ok, 16); // hex == 255, ok == true
|
---|
837 | ulong dec = str.toULong(&ok, 10); // dec == 0, ok == false
|
---|
838 | //! [78]
|
---|
839 | }
|
---|
840 |
|
---|
841 | void Widget::toULongLongFunction()
|
---|
842 | {
|
---|
843 | //! [79]
|
---|
844 | QString str = "FF";
|
---|
845 | bool ok;
|
---|
846 |
|
---|
847 | quint64 hex = str.toULongLong(&ok, 16); // hex == 255, ok == true
|
---|
848 | quint64 dec = str.toULongLong(&ok, 10); // dec == 0, ok == false
|
---|
849 | //! [79]
|
---|
850 | }
|
---|
851 |
|
---|
852 | void Widget::toUShortFunction()
|
---|
853 | {
|
---|
854 | //! [80]
|
---|
855 | QString str = "FF";
|
---|
856 | bool ok;
|
---|
857 |
|
---|
858 | ushort hex = str.toUShort(&ok, 16); // hex == 255, ok == true
|
---|
859 | ushort dec = str.toUShort(&ok, 10); // dec == 0, ok == false
|
---|
860 | //! [80]
|
---|
861 | }
|
---|
862 |
|
---|
863 | void Widget::toUpperFunction()
|
---|
864 | {
|
---|
865 | //! [81]
|
---|
866 | QString str = "TeXt";
|
---|
867 | str = str.toUpper(); // str == "TEXT"
|
---|
868 | //! [81]
|
---|
869 | }
|
---|
870 |
|
---|
871 | void Widget::trimmedFunction()
|
---|
872 | {
|
---|
873 | //! [82]
|
---|
874 | QString str = " lots\t of\nwhitespace\r\n ";
|
---|
875 | str = str.trimmed();
|
---|
876 | // str == "lots\t of\nwhitespace"
|
---|
877 | //! [82]
|
---|
878 | }
|
---|
879 |
|
---|
880 | void Widget::truncateFunction()
|
---|
881 | {
|
---|
882 | //! [83]
|
---|
883 | QString str = "Vladivostok";
|
---|
884 | str.truncate(4);
|
---|
885 | // str == "Vlad"
|
---|
886 | //! [83]
|
---|
887 | }
|
---|
888 |
|
---|
889 | void Widget::plusEqualOperator()
|
---|
890 | {
|
---|
891 | //! [84]
|
---|
892 | QString x = "free";
|
---|
893 | QString y = "dom";
|
---|
894 | x += y;
|
---|
895 | // x == "freedom"
|
---|
896 | //! [84]
|
---|
897 | }
|
---|
898 |
|
---|
899 | void Widget::arrayOperator()
|
---|
900 | {
|
---|
901 | //! [85]
|
---|
902 | QString str;
|
---|
903 |
|
---|
904 | if (str[0] == QChar('?'))
|
---|
905 | str[0] = QChar('_');
|
---|
906 | //! [85]
|
---|
907 | }
|
---|
908 |
|
---|
909 | void Widget::midRefFunction()
|
---|
910 | {
|
---|
911 | //! [midRef]
|
---|
912 | QString x = "Nine pineapples";
|
---|
913 | QStringRef y = x.midRef(5, 4); // y == "pine"
|
---|
914 | QStringRef z = x.midRef(5); // z == "pineapples"
|
---|
915 | //! [midRef]
|
---|
916 | }
|
---|
917 |
|
---|
918 | void Widget::leftRefFunction()
|
---|
919 | {
|
---|
920 | //! [leftRef]
|
---|
921 | QString x = "Pineapple";
|
---|
922 | QStringRef y = x.leftRef(4); // y == "Pine"
|
---|
923 | //! [leftRef]
|
---|
924 | }
|
---|
925 |
|
---|
926 | void Widget::rightRefFunction()
|
---|
927 | {
|
---|
928 | //! [rightRef]
|
---|
929 | QString x = "Pineapple";
|
---|
930 | QStringRef y = x.rightRef(5); // y == "apple"
|
---|
931 | //! [rightRef]
|
---|
932 | }
|
---|
933 |
|
---|
934 |
|
---|
935 | int main(int argc, char *argv[])
|
---|
936 | {
|
---|
937 | QApplication app(argc, argv);
|
---|
938 | Widget widget;
|
---|
939 | widget.show();
|
---|
940 | return app.exec();
|
---|
941 | }
|
---|