|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the documentation of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
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 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
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] |
|
454 } |
|
455 |
|
456 void Widget::midFunction() |
|
457 { |
|
458 //! [34] |
|
459 QString x = "Nine pineapples"; |
|
460 QString y = x.mid(5, 4); // y == "pine" |
|
461 QString z = x.mid(5); // z == "pineapples" |
|
462 //! [34] |
|
463 } |
|
464 |
|
465 void Widget::numberFunction() |
|
466 { |
|
467 //! [35] |
|
468 long a = 63; |
|
469 QString s = QString::number(a, 16); // s == "3f" |
|
470 QString t = QString::number(a, 16).toUpper(); // t == "3F" |
|
471 //! [35] |
|
472 } |
|
473 |
|
474 void Widget::prependFunction() |
|
475 { |
|
476 //! [36] |
|
477 QString x = "ship"; |
|
478 QString y = "air"; |
|
479 x.prepend(y); |
|
480 // x == "airship" |
|
481 //! [36] |
|
482 } |
|
483 |
|
484 void Widget::removeFunction() |
|
485 { |
|
486 //! [37] |
|
487 QString s = "Montreal"; |
|
488 s.remove(1, 4); |
|
489 // s == "Meal" |
|
490 //! [37] |
|
491 |
|
492 //! [38] |
|
493 QString t = "Ali Baba"; |
|
494 t.remove(QChar('a'), Qt::CaseInsensitive); |
|
495 // t == "li Bb" |
|
496 //! [38] |
|
497 |
|
498 //! [39] |
|
499 QString r = "Telephone"; |
|
500 r.remove(QRegExp("[aeiou].")); |
|
501 // r == "The" |
|
502 //! [39] |
|
503 } |
|
504 |
|
505 void Widget::replaceFunction() |
|
506 { |
|
507 //! [40] |
|
508 QString x = "Say yes!"; |
|
509 QString y = "no"; |
|
510 x.replace(4, 3, y); |
|
511 // x == "Say no!" |
|
512 //! [40] |
|
513 |
|
514 //! [41] |
|
515 QString str = "colour behaviour flavour neighbour"; |
|
516 str.replace(QString("ou"), QString("o")); |
|
517 // str == "color behavior flavor neighbor" |
|
518 //! [41] |
|
519 |
|
520 //! [42] |
|
521 QString s = "Banana"; |
|
522 s.replace(QRegExp("a[mn]"), "ox"); |
|
523 // s == "Boxoxa" |
|
524 //! [42] |
|
525 |
|
526 //! [43] |
|
527 QString t = "A <i>bon mot</i>."; |
|
528 t.replace(QRegExp("<i>([^<]*)</i>"), "\\emph{\\1}"); |
|
529 // t == "A \\emph{bon mot}." |
|
530 //! [43] |
|
531 |
|
532 //! [86] |
|
533 QString equis = "xxxxxx"; |
|
534 equis.replace("xx", "x"); |
|
535 // equis == "xxx" |
|
536 //! [86] |
|
537 } |
|
538 |
|
539 void Widget::reserveFunction() |
|
540 { |
|
541 //! [44] |
|
542 QString result; |
|
543 int maxSize; |
|
544 bool condition; |
|
545 QChar nextChar; |
|
546 |
|
547 result.reserve(maxSize); |
|
548 |
|
549 while (condition) |
|
550 result.append(nextChar); |
|
551 |
|
552 result.squeeze(); |
|
553 //! [44] |
|
554 } |
|
555 |
|
556 void Widget::resizeFunction() |
|
557 { |
|
558 //! [45] |
|
559 QString s = "Hello world"; |
|
560 s.resize(5); |
|
561 // s == "Hello" |
|
562 |
|
563 s.resize(8); |
|
564 // s == "Hello???" (where ? stands for any character) |
|
565 //! [45] |
|
566 |
|
567 //! [46] |
|
568 QString t = "Hello"; |
|
569 t += QString(10, 'X'); |
|
570 // t == "HelloXXXXXXXXXX" |
|
571 //! [46] |
|
572 |
|
573 //! [47] |
|
574 QString r = "Hello"; |
|
575 r = r.leftJustified(10, ' '); |
|
576 // r == "Hello " |
|
577 //! [47] |
|
578 } |
|
579 |
|
580 void Widget::rightFunction() |
|
581 { |
|
582 //! [48] |
|
583 QString x = "Pineapple"; |
|
584 QString y = x.right(5); // y == "apple" |
|
585 //! [48] |
|
586 } |
|
587 |
|
588 void Widget::rightJustifiedFunction() |
|
589 { |
|
590 //! [49] |
|
591 QString s = "apple"; |
|
592 QString t = s.rightJustified(8, '.'); // t == "...apple" |
|
593 //! [49] |
|
594 |
|
595 //! [50] |
|
596 QString str = "Pineapple"; |
|
597 str = str.rightJustified(5, '.', true); // str == "Pinea" |
|
598 //! [50] |
|
599 } |
|
600 |
|
601 void Widget::sectionFunction() |
|
602 { |
|
603 //! [51] //! [52] |
|
604 QString str; |
|
605 //! [51] |
|
606 QString csv = "forename,middlename,surname,phone"; |
|
607 QString path = "/usr/local/bin/myapp"; // First field is empty |
|
608 QString::SectionFlag flag = QString::SectionSkipEmpty; |
|
609 |
|
610 |
|
611 str = csv.section(',', 2, 2); // str == "surname" |
|
612 str = path.section('/', 3, 4); // str == "bin/myapp" |
|
613 str = path.section('/', 3, 3, flag); // str == "myapp" |
|
614 //! [52] |
|
615 |
|
616 //! [53] |
|
617 str = csv.section(',', -3, -2); // str == "middlename,surname" |
|
618 str = path.section('/', -1); // str == "myapp" |
|
619 //! [53] |
|
620 |
|
621 //! [54] |
|
622 QString data = "forename**middlename**surname**phone"; |
|
623 |
|
624 str = data.section("**", 2, 2); // str == "surname" |
|
625 str = data.section("**", -3, -2); // str == "middlename**surname" |
|
626 //! [54] |
|
627 |
|
628 //! [55] |
|
629 QString line = "forename\tmiddlename surname \t \t phone"; |
|
630 QRegExp sep("\\s+"); |
|
631 str = line.section(sep, 2, 2); // s == "surname" |
|
632 str = line.section(sep, -3, -2); // s == "middlename surname" |
|
633 //! [55] |
|
634 } |
|
635 |
|
636 void Widget::setNumFunction() |
|
637 { |
|
638 //! [56] |
|
639 QString str; |
|
640 str.setNum(1234); // str == "1234" |
|
641 //! [56] |
|
642 } |
|
643 |
|
644 void Widget::simplifiedFunction() |
|
645 { |
|
646 //! [57] |
|
647 QString str = " lots\t of\nwhitespace\r\n "; |
|
648 str = str.simplified(); |
|
649 // str == "lots of whitespace"; |
|
650 //! [57] |
|
651 } |
|
652 |
|
653 void Widget::sizeFunction() |
|
654 { |
|
655 //! [58] |
|
656 QString str = "World"; |
|
657 int n = str.size(); // n == 5 |
|
658 str.data()[0]; // returns 'W' |
|
659 str.data()[4]; // returns 'd' |
|
660 str.data()[5]; // returns '\0' |
|
661 //! [58] |
|
662 } |
|
663 |
|
664 void Widget::splitFunction() |
|
665 { |
|
666 //! [59] |
|
667 QString str; |
|
668 QStringList list; |
|
669 |
|
670 str = "Some text\n\twith strange whitespace."; |
|
671 list = str.split(QRegExp("\\s+")); |
|
672 // list: [ "Some", "text", "with", "strange", "whitespace." ] |
|
673 //! [59] |
|
674 |
|
675 //! [60] |
|
676 str = "This time, a normal English sentence."; |
|
677 list = str.split(QRegExp("\\W+"), QString::SkipEmptyParts); |
|
678 // list: [ "This", "time", "a", "normal", "English", "sentence" ] |
|
679 //! [60] |
|
680 |
|
681 //! [61] |
|
682 str = "Now: this sentence fragment."; |
|
683 list = str.split(QRegExp("\\b")); |
|
684 // list: [ "", "Now", ": ", "this", " ", "sentence", " ", "fragment", "." ] |
|
685 //! [61] |
|
686 } |
|
687 |
|
688 void Widget::splitCaseSensitiveFunction() |
|
689 { |
|
690 //! [62] |
|
691 QString str = "a,,b,c"; |
|
692 |
|
693 QStringList list1 = str.split(","); |
|
694 // list1: [ "a", "", "b", "c" ] |
|
695 |
|
696 QStringList list2 = str.split(",", QString::SkipEmptyParts); |
|
697 // list2: [ "a", "b", "c" ] |
|
698 //! [62] |
|
699 } |
|
700 |
|
701 void Widget::sprintfFunction() |
|
702 { |
|
703 //! [63] |
|
704 size_t BufSize; |
|
705 char buf[BufSize]; |
|
706 |
|
707 ::snprintf(buf, BufSize, "%lld", 123456789LL); |
|
708 QString str = QString::fromAscii(buf); |
|
709 //! [63] |
|
710 |
|
711 //! [64] |
|
712 QString result; |
|
713 QTextStream(&result) << "pi = " << 3.14; |
|
714 // result == "pi = 3.14" |
|
715 //! [64] |
|
716 } |
|
717 |
|
718 void Widget::startsWithFunction() |
|
719 { |
|
720 //! [65] |
|
721 QString str = "Bananas"; |
|
722 str.startsWith("Ban"); // returns true |
|
723 str.startsWith("Car"); // returns false |
|
724 //! [65] |
|
725 } |
|
726 |
|
727 void Widget::toDoubleFunction() |
|
728 { |
|
729 //! [66] |
|
730 QString str = "1234.56"; |
|
731 double val = str.toDouble(); // val == 1234.56 |
|
732 //! [66] |
|
733 |
|
734 //! [67] |
|
735 bool ok; |
|
736 double d; |
|
737 |
|
738 d = QString( "1234.56e-02" ).toDouble(&ok); // ok == true, d == 12.3456 |
|
739 //! [67] |
|
740 |
|
741 //! [68] //! [69] |
|
742 QLocale::setDefault(QLocale::C); |
|
743 d = QString( "1234,56" ).toDouble(&ok); // ok == false |
|
744 //! [68] |
|
745 d = QString( "1234.56" ).toDouble(&ok); // ok == true, d == 1234.56 |
|
746 |
|
747 //! [69] //! [70] |
|
748 QLocale::setDefault(QLocale::German); |
|
749 d = QString( "1234,56" ).toDouble(&ok); // ok == true, d == 1234.56 |
|
750 d = QString( "1234.56" ).toDouble(&ok); // ok == true, d == 1234.56 |
|
751 |
|
752 //! [70] |
|
753 QLocale::setDefault(QLocale::C); |
|
754 d = QString( "1,234,567.89" ).toDouble(&ok); // ok == false |
|
755 } |
|
756 |
|
757 void Widget::toFloatFunction() |
|
758 { |
|
759 //! [71] |
|
760 QString str1 = "1234.56"; |
|
761 str1.toFloat(); // returns 1234.56 |
|
762 |
|
763 bool ok; |
|
764 QString str2 = "R2D2"; |
|
765 str2.toFloat(&ok); // returns 0.0, sets ok to false |
|
766 //! [71] |
|
767 } |
|
768 |
|
769 void Widget::toIntFunction() |
|
770 { |
|
771 //! [72] |
|
772 QString str = "FF"; |
|
773 bool ok; |
|
774 int hex = str.toInt(&ok, 16); // hex == 255, ok == true |
|
775 int dec = str.toInt(&ok, 10); // dec == 0, ok == false |
|
776 //! [72] |
|
777 } |
|
778 |
|
779 void Widget::toLongFunction() |
|
780 { |
|
781 //! [73] |
|
782 QString str = "FF"; |
|
783 bool ok; |
|
784 |
|
785 long hex = str.toLong(&ok, 16); // hex == 255, ok == true |
|
786 long dec = str.toLong(&ok, 10); // dec == 0, ok == false |
|
787 //! [73] |
|
788 } |
|
789 |
|
790 void Widget::toLongLongFunction() |
|
791 { |
|
792 //! [74] |
|
793 QString str = "FF"; |
|
794 bool ok; |
|
795 |
|
796 qint64 hex = str.toLongLong(&ok, 16); // hex == 255, ok == true |
|
797 qint64 dec = str.toLongLong(&ok, 10); // dec == 0, ok == false |
|
798 //! [74] |
|
799 } |
|
800 |
|
801 void Widget::toLowerFunction() |
|
802 { |
|
803 //! [75] |
|
804 QString str = "Qt by NOKIA"; |
|
805 str = str.toLower(); // str == "qy by nokia" |
|
806 //! [75] |
|
807 } |
|
808 |
|
809 void Widget::toShortFunction() |
|
810 { |
|
811 //! [76] |
|
812 QString str = "FF"; |
|
813 bool ok; |
|
814 |
|
815 short hex = str.toShort(&ok, 16); // hex == 255, ok == true |
|
816 short dec = str.toShort(&ok, 10); // dec == 0, ok == false |
|
817 //! [76] |
|
818 } |
|
819 |
|
820 void Widget::toUIntFunction() |
|
821 { |
|
822 //! [77] |
|
823 QString str = "FF"; |
|
824 bool ok; |
|
825 |
|
826 uint hex = str.toUInt(&ok, 16); // hex == 255, ok == true |
|
827 uint dec = str.toUInt(&ok, 10); // dec == 0, ok == false |
|
828 //! [77] |
|
829 } |
|
830 |
|
831 void Widget::toULongFunction() |
|
832 { |
|
833 //! [78] |
|
834 QString str = "FF"; |
|
835 bool ok; |
|
836 |
|
837 ulong hex = str.toULong(&ok, 16); // hex == 255, ok == true |
|
838 ulong dec = str.toULong(&ok, 10); // dec == 0, ok == false |
|
839 //! [78] |
|
840 } |
|
841 |
|
842 void Widget::toULongLongFunction() |
|
843 { |
|
844 //! [79] |
|
845 QString str = "FF"; |
|
846 bool ok; |
|
847 |
|
848 quint64 hex = str.toULongLong(&ok, 16); // hex == 255, ok == true |
|
849 quint64 dec = str.toULongLong(&ok, 10); // dec == 0, ok == false |
|
850 //! [79] |
|
851 } |
|
852 |
|
853 void Widget::toUShortFunction() |
|
854 { |
|
855 //! [80] |
|
856 QString str = "FF"; |
|
857 bool ok; |
|
858 |
|
859 ushort hex = str.toUShort(&ok, 16); // hex == 255, ok == true |
|
860 ushort dec = str.toUShort(&ok, 10); // dec == 0, ok == false |
|
861 //! [80] |
|
862 } |
|
863 |
|
864 void Widget::toUpperFunction() |
|
865 { |
|
866 //! [81] |
|
867 QString str = "TeXt"; |
|
868 str = str.toUpper(); // str == "TEXT" |
|
869 //! [81] |
|
870 } |
|
871 |
|
872 void Widget::trimmedFunction() |
|
873 { |
|
874 //! [82] |
|
875 QString str = " lots\t of\nwhitespace\r\n "; |
|
876 str = str.trimmed(); |
|
877 // str == "lots\t of\nwhitespace" |
|
878 //! [82] |
|
879 } |
|
880 |
|
881 void Widget::truncateFunction() |
|
882 { |
|
883 //! [83] |
|
884 QString str = "Vladivostok"; |
|
885 str.truncate(4); |
|
886 // str == "Vlad" |
|
887 //! [83] |
|
888 } |
|
889 |
|
890 void Widget::plusEqualOperator() |
|
891 { |
|
892 //! [84] |
|
893 QString x = "free"; |
|
894 QString y = "dom"; |
|
895 x += y; |
|
896 // x == "freedom" |
|
897 //! [84] |
|
898 } |
|
899 |
|
900 void Widget::arrayOperator() |
|
901 { |
|
902 //! [85] |
|
903 QString str; |
|
904 |
|
905 if (str[0] == QChar('?')) |
|
906 str[0] = QChar('_'); |
|
907 //! [85] |
|
908 } |
|
909 |
|
910 void Widget::midRefFunction() |
|
911 { |
|
912 //! [midRef] |
|
913 QString x = "Nine pineapples"; |
|
914 QStringRef y = x.midRef(5, 4); // y == "pine" |
|
915 QStringRef z = x.midRef(5); // z == "pineapples" |
|
916 //! [midRef] |
|
917 } |
|
918 |
|
919 void Widget::leftRefFunction() |
|
920 { |
|
921 //! [leftRef] |
|
922 QString x = "Pineapple"; |
|
923 QStringRef y = x.leftRef(4); // y == "Pine" |
|
924 //! [leftRef] |
|
925 } |
|
926 |
|
927 void Widget::rightRefFunction() |
|
928 { |
|
929 //! [rightRef] |
|
930 QString x = "Pineapple"; |
|
931 QStringRef y = x.rightRef(5); // y == "apple" |
|
932 //! [rightRef] |
|
933 } |
|
934 |
|
935 |
|
936 int main(int argc, char *argv[]) |
|
937 { |
|
938 QApplication app(argc, argv); |
|
939 Widget widget; |
|
940 widget.show(); |
|
941 return app.exec(); |
|
942 } |