1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QtGui>
#include <iostream>
using namespace std;
class Widget : public QWidget
{
public:
Widget(QWidget *parent = nullptr);
};
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
//! [0a]
QStringList fonts = { "Arial", "Helvetica", "Times" };
//! [0a]
//! [0b]
fonts << "Courier" << "Verdana";
//! [0b]
//! [4]
QString str = fonts.join(", ");
// str == "Arial, Helvetica, Times, Courier"
//! [4]
//! [5] //! [6]
QStringList list;
//! [5]
list = str.split(',');
// list: ["Arial", "Helvetica", "Times", "Courier"]
//! [6]
//! [7]
QStringList monospacedFonts = fonts.filter(QRegularExpression("Courier|Fixed"));
//! [7]
//! [8]
QStringList files;
files << "$QTDIR/src/moc/moc.y"
<< "$QTDIR/src/moc/moc.l"
<< "$QTDIR/include/qconfig.h";
files.replaceInStrings("$QTDIR", "/usr/lib/qt");
// files: [ "/usr/lib/qt/src/moc/moc.y", ...]
//! [8]
QString str1, str2, str3;
//! [9]
QStringList longerList = (QStringList() << str1 << str2 << str3);
//! [9]
list.clear();
//! [10]
list << "Bill Murray" << "John Doe" << "Bill Clinton";
//! [11]
QStringList result;
//! [11]
result = list.filter("Bill");
// result: ["Bill Murray", "Bill Clinton"]
//! [10]
result.clear();
//! [12]
foreach (const QString &str, list) {
if (str.contains("Bill"))
result += str;
}
//! [12]
list.clear();
//! [13]
list << "alpha" << "beta" << "gamma" << "epsilon";
list.replaceInStrings("a", "o");
// list == ["olpho", "beto", "gommo", "epsilon"]
//! [13]
list.clear();
//! [16]
list << "alpha" << "beta" << "gamma" << "epsilon";
list.replaceInStrings(QRegularExpression("^a"), "o");
// list == ["olpha", "beta", "gamma", "epsilon"]
//! [16]
list.clear();
//! [17]
list << "Bill Clinton" << "Murray, Bill";
list.replaceInStrings(QRegularExpression("^(.*), (.*)$"), "\\2 \\1");
// list == ["Bill Clinton", "Bill Murray"]
//! [17]
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Widget widget;
widget.show();
return app.exec();
}
|