1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the documentation of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
21 ** the names of its contributors may be used to endorse or promote
22 ** products derived from this software without specific prior written
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
42 #include <QApplication>
45 class Widget : public QWidget
48 Widget(QWidget *parent = 0);
50 void constCharPointer();
51 void constCharArray();
52 void characterReference();
57 QString boolToString(bool b);
60 void appendFunction();
63 void compareFunction();
64 void compareSensitiveFunction();
65 void containsFunction();
68 void endsWithFunction();
70 void fromRawDataFunction();
72 void indexOfFunction();
73 void firstIndexOfFunction();
74 void insertFunction();
75 void isNullFunction();
76 void isEmptyFunction();
77 void lastIndexOfFunction();
79 void leftJustifiedFunction();
80 void leftRefFunction();
82 void midRefFunction();
83 void numberFunction();
85 void prependFunction();
86 void removeFunction();
87 void replaceFunction();
88 void reserveFunction();
89 void resizeFunction();
91 void rightJustifiedFunction();
92 void rightRefFunction();
93 void sectionFunction();
94 void setNumFunction();
95 void simplifiedFunction();
99 void splitCaseSensitiveFunction();
100 void sprintfFunction();
101 void startsWithFunction();
102 void toDoubleFunction();
103 void toFloatFunction();
104 void toIntFunction();
105 void toLongFunction();
106 void toLongLongFunction();
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();
118 void plusEqualOperator();
119 void arrayOperator();
122 Widget::Widget(QWidget *parent)
127 void Widget::constCharPointer()
130 QString str = "Hello";
134 void Widget::constCharArray()
137 static const QChar data[4] = { 0x0055, 0x006e, 0x10e3, 0x03a3 };
138 QString str(data, 4);
142 void Widget::characterReference()
150 str[2] = QChar(0x10e3);
151 str[3] = QChar(0x03a3);
155 void Widget::atFunction()
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]";
167 void Widget::stringLiteral()
172 if (str == "auto" || str == "extern"
173 || str == "static" || str == "register") {
179 void Widget::modify()
183 str.prepend("rock "); // str == "rock and"
184 str.append(" roll"); // str == "rock and roll"
185 str.replace(5, 3, "&"); // str == "rock & roll"
192 QString str = "We must be <b>bold</b>, very <b>bold</b>";
195 while ((j = str.indexOf("<b>", j)) != -1) {
196 qDebug() << "Found <b> tag at index position" << j;
203 QString Widget::boolToString(bool b)
215 void Widget::nullVsEmpty()
218 QString().isNull(); // returns true
219 QString().isEmpty(); // returns true
221 QString("").isNull(); // returns false
222 QString("").isEmpty(); // returns true
224 QString("abc").isNull(); // returns false
225 QString("abc").isEmpty(); // returns false
229 void Widget::appendFunction()
240 x.insert(x.size(), y);
244 void Widget::argFunction()
247 QString i; // current file's number
248 QString total; // number of files to process
249 QString fileName; // current file's name
251 QString status = QString("Processing file %1 of %2: %3")
252 .arg(i).arg(total).arg(fileName);
260 str.arg("%1f", "Hello"); // returns "%1f Hello"
261 str.arg("%1f").arg("Hello"); // returns "Hellof %2"
265 str = QString("Decimal 63 is %1 in hexadecimal")
267 // str == "Decimal 63 is 3f in hexadecimal"
269 QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));
270 str = QString("%1 %L2 %L3")
274 // str == "12345 12,345 3039"
278 void Widget::chopFunction()
281 QString str("LOGOUT\r\n");
287 void Widget::compareFunction()
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
294 void Widget::compareSensitiveFunction()
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
303 void Widget::containsFunction()
306 QString str = "Peter Pan";
307 str.contains("peter", Qt::CaseInsensitive); // returns true
311 void Widget::countFunction()
314 QString str = "banana and panama";
315 str.count(QRegExp("a[nm]a")); // returns 4
319 QString str = "banana and panama";
320 str.count(QRegularExpression("a[nm]a")); // returns 4
324 void Widget::dataFunction()
327 QString str = "Hello world";
328 QChar *data = str.data();
329 while (!data->isNull()) {
330 qDebug() << data->unicode();
336 void Widget::endsWithFunction()
339 QString str = "Bananas";
340 str.endsWith("anas"); // returns true
341 str.endsWith("pple"); // returns false
345 void Widget::fillFunction()
348 QString str = "Berlin";
357 void Widget::fromRawDataFunction()
361 static const QChar unicode[] = {
362 0x005A, 0x007F, 0x00A4, 0x0060,
363 0x1009, 0x0020, 0x0020};
364 int size = sizeof(unicode) / sizeof(QChar);
366 QString str = QString::fromRawData(unicode, size);
367 if (str.contains(QRegExp(pattern))) {
374 void Widget::indexOfFunction()
377 QString x = "sticky question";
379 x.indexOf(y); // returns 0
380 x.indexOf(y, 1); // returns 10
381 x.indexOf(y, 10); // returns 10
382 x.indexOf(y, 11); // returns -1
386 void Widget::firstIndexOfFunction()
389 QString str = "the minimum";
390 str.indexOf(QRegExp("m[aeiou]"), 0); // returns 4
394 QString str = "the minimum";
395 str.indexOf(QRegularExpression("m[aeiou]"), 0); // returns 4
399 void Widget::insertFunction()
402 QString str = "Meal";
403 str.insert(1, QString("ontr"));
408 void Widget::isEmptyFunction()
411 QString().isEmpty(); // returns true
412 QString("").isEmpty(); // returns true
413 QString("x").isEmpty(); // returns false
414 QString("abc").isEmpty(); // returns false
418 void Widget::isNullFunction()
421 QString().isNull(); // returns true
422 QString("").isNull(); // returns false
423 QString("abc").isNull(); // returns false
427 void Widget::lastIndexOfFunction()
430 QString x = "crazy azimuths";
432 x.lastIndexOf(y); // returns 6
433 x.lastIndexOf(y, 6); // returns 6
434 x.lastIndexOf(y, 5); // returns 2
435 x.lastIndexOf(y, 1); // returns -1
439 QString str = "the minimum";
440 str.lastIndexOf(QRegExp("m[aeiou]")); // returns 8
444 QString str = "the minimum";
445 str.lastIndexOf(QRegularExpression("m[aeiou]")); // returns 8
449 void Widget::leftFunction()
452 QString x = "Pineapple";
453 QString y = x.left(4); // y == "Pine"
457 void Widget::leftJustifiedFunction()
461 QString t = s.leftJustified(8, '.'); // t == "apple..."
465 QString str = "Pineapple";
466 str = str.leftJustified(5, '.', true); // str == "Pinea"
470 void Widget::midFunction()
473 QString x = "Nine pineapples";
474 QString y = x.mid(5, 4); // y == "pine"
475 QString z = x.mid(5); // z == "pineapples"
479 void Widget::numberFunction()
483 QString s = QString::number(a, 16); // s == "3f"
484 QString t = QString::number(a, 16).toUpper(); // t == "3F"
488 void Widget::prependFunction()
498 void Widget::removeFunction()
501 QString s = "Montreal";
507 QString t = "Ali Baba";
508 t.remove(QChar('a'), Qt::CaseInsensitive);
513 QString r = "Telephone";
514 r.remove(QRegExp("[aeiou]."));
519 QString r = "Telephone";
520 r.remove(QRegularExpression("[aeiou]."));
525 void Widget::replaceFunction()
528 QString x = "Say yes!";
535 QString str = "colour behaviour flavour neighbour";
536 str.replace(QString("ou"), QString("o"));
537 // str == "color behavior flavor neighbor"
541 QString s = "Banana";
542 s.replace(QRegExp("a[mn]"), "ox");
547 QString t = "A <i>bon mot</i>.";
548 t.replace(QRegExp("<i>([^<]*)</i>"), "\\emph{\\1}");
549 // t == "A \\emph{bon mot}."
553 QString equis = "xxxxxx";
554 equis.replace("xx", "x");
559 QString s = "Banana";
560 s.replace(QRegularExpression("a[mn]"), "ox");
565 QString t = "A <i>bon mot</i>.";
566 t.replace(QRegularExpression("<i>([^<]*)</i>"), "\\emph{\\1}");
567 // t == "A \\emph{bon mot}."
571 void Widget::reserveFunction()
579 result.reserve(maxSize);
582 result.append(nextChar);
588 void Widget::resizeFunction()
591 QString s = "Hello world";
596 // s == "Hello???" (where ? stands for any character)
601 t += QString(10, 'X');
602 // t == "HelloXXXXXXXXXX"
607 r = r.leftJustified(10, ' ');
612 void Widget::rightFunction()
615 QString x = "Pineapple";
616 QString y = x.right(5); // y == "apple"
620 void Widget::rightJustifiedFunction()
624 QString t = s.rightJustified(8, '.'); // t == "...apple"
628 QString str = "Pineapple";
629 str = str.rightJustified(5, '.', true); // str == "Pinea"
633 void Widget::sectionFunction()
638 QString csv = "forename,middlename,surname,phone";
639 QString path = "/usr/local/bin/myapp"; // First field is empty
640 QString::SectionFlag flag = QString::SectionSkipEmpty;
643 str = csv.section(',', 2, 2); // str == "surname"
644 str = path.section('/', 3, 4); // str == "bin/myapp"
645 str = path.section('/', 3, 3, flag); // str == "myapp"
649 str = csv.section(',', -3, -2); // str == "middlename,surname"
650 str = path.section('/', -1); // str == "myapp"
654 QString data = "forename**middlename**surname**phone";
656 str = data.section("**", 2, 2); // str == "surname"
657 str = data.section("**", -3, -2); // str == "middlename**surname"
661 QString line = "forename\tmiddlename surname \t \t phone";
663 str = line.section(sep, 2, 2); // str == "surname"
664 str = line.section(sep, -3, -2); // str == "middlename surname"
668 QString line = "forename\tmiddlename surname \t \t phone";
669 QRegularExpression sep("\\s+");
670 str = line.section(sep, 2, 2); // str == "surname"
671 str = line.section(sep, -3, -2); // str == "middlename surname"
675 void Widget::setNumFunction()
679 str.setNum(1234); // str == "1234"
683 void Widget::simplifiedFunction()
686 QString str = " lots\t of\nwhitespace\r\n ";
687 str = str.simplified();
688 // str == "lots of whitespace";
692 void Widget::sizeFunction()
695 QString str = "World";
696 int n = str.size(); // n == 5
697 str.data()[0]; // returns 'W'
698 str.data()[4]; // returns 'd'
699 str.data()[5]; // returns '\0'
703 void Widget::splitFunction()
709 str = "Some text\n\twith strange whitespace.";
710 list = str.split(QRegExp("\\s+"));
711 // list: [ "Some", "text", "with", "strange", "whitespace." ]
715 str = "This time, a normal English sentence.";
716 list = str.split(QRegExp("\\W+"), QString::SkipEmptyParts);
717 // list: [ "This", "time", "a", "normal", "English", "sentence" ]
721 str = "Now: this sentence fragment.";
722 list = str.split(QRegExp("\\b"));
723 // list: [ "", "Now", ": ", "this", " ", "sentence", " ", "fragment", "." ]
730 str = "Some text\n\twith strange whitespace.";
731 list = str.split(QRegularExpression("\\s+"));
732 // list: [ "Some", "text", "with", "strange", "whitespace." ]
736 str = "This time, a normal English sentence.";
737 list = str.split(QRegularExpression("\\W+"), QString::SkipEmptyParts);
738 // list: [ "This", "time", "a", "normal", "English", "sentence" ]
742 str = "Now: this sentence fragment.";
743 list = str.split(QRegularExpression("\\b"));
744 // list: [ "", "Now", ": ", "this", " ", "sentence", " ", "fragment", "." ]
748 void Widget::splitCaseSensitiveFunction()
751 QString str = "a,,b,c";
753 QStringList list1 = str.split(",");
754 // list1: [ "a", "", "b", "c" ]
756 QStringList list2 = str.split(",", QString::SkipEmptyParts);
757 // list2: [ "a", "b", "c" ]
761 void Widget::sprintfFunction()
767 ::snprintf(buf, BufSize, "%lld", 123456789LL);
768 QString str = QString::fromAscii(buf);
773 QTextStream(&result) << "pi = " << 3.14;
774 // result == "pi = 3.14"
778 void Widget::startsWithFunction()
781 QString str = "Bananas";
782 str.startsWith("Ban"); // returns true
783 str.startsWith("Car"); // returns false
787 void Widget::toDoubleFunction()
790 QString str = "1234.56";
791 double val = str.toDouble(); // val == 1234.56
798 d = QString( "1234.56e-02" ).toDouble(&ok); // ok == true, d == 12.3456
802 d = QString( "1234,56" ).toDouble(&ok); // ok == false
803 d = QString( "1234.56" ).toDouble(&ok); // ok == true, d == 1234.56
807 d = QString( "1,234,567.89" ).toDouble(&ok); // ok == false
808 d = QString( "1234567.89" ).toDouble(&ok); // ok == true
812 void Widget::toFloatFunction()
815 QString str1 = "1234.56";
816 str1.toFloat(); // returns 1234.56
819 QString str2 = "R2D2";
820 str2.toFloat(&ok); // returns 0.0, sets ok to false
824 void Widget::toIntFunction()
829 int hex = str.toInt(&ok, 16); // hex == 255, ok == true
830 int dec = str.toInt(&ok, 10); // dec == 0, ok == false
834 void Widget::toLongFunction()
840 long hex = str.toLong(&ok, 16); // hex == 255, ok == true
841 long dec = str.toLong(&ok, 10); // dec == 0, ok == false
845 void Widget::toLongLongFunction()
851 qint64 hex = str.toLongLong(&ok, 16); // hex == 255, ok == true
852 qint64 dec = str.toLongLong(&ok, 10); // dec == 0, ok == false
856 void Widget::toLowerFunction()
859 QString str = "Qt by NOKIA";
860 str = str.toLower(); // str == "qt by nokia"
864 void Widget::toShortFunction()
870 short hex = str.toShort(&ok, 16); // hex == 255, ok == true
871 short dec = str.toShort(&ok, 10); // dec == 0, ok == false
875 void Widget::toUIntFunction()
881 uint hex = str.toUInt(&ok, 16); // hex == 255, ok == true
882 uint dec = str.toUInt(&ok, 10); // dec == 0, ok == false
886 void Widget::toULongFunction()
892 ulong hex = str.toULong(&ok, 16); // hex == 255, ok == true
893 ulong dec = str.toULong(&ok, 10); // dec == 0, ok == false
897 void Widget::toULongLongFunction()
903 quint64 hex = str.toULongLong(&ok, 16); // hex == 255, ok == true
904 quint64 dec = str.toULongLong(&ok, 10); // dec == 0, ok == false
908 void Widget::toUShortFunction()
914 ushort hex = str.toUShort(&ok, 16); // hex == 255, ok == true
915 ushort dec = str.toUShort(&ok, 10); // dec == 0, ok == false
919 void Widget::toUpperFunction()
922 QString str = "TeXt";
923 str = str.toUpper(); // str == "TEXT"
927 void Widget::trimmedFunction()
930 QString str = " lots\t of\nwhitespace\r\n ";
932 // str == "lots\t of\nwhitespace"
936 void Widget::truncateFunction()
939 QString str = "Vladivostok";
945 void Widget::plusEqualOperator()
955 void Widget::arrayOperator()
960 if (str[0] == QChar('?'))
965 void Widget::midRefFunction()
968 QString x = "Nine pineapples";
969 QStringRef y = x.midRef(5, 4); // y == "pine"
970 QStringRef z = x.midRef(5); // z == "pineapples"
974 void Widget::leftRefFunction()
977 QString x = "Pineapple";
978 QStringRef y = x.leftRef(4); // y == "Pine"
982 void Widget::rightRefFunction()
985 QString x = "Pineapple";
986 QStringRef y = x.rightRef(5); // y == "apple"
991 int main(int argc, char *argv[])
993 QApplication app(argc, argv);