Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
src_corelib_global_qglobal.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QMetaProperty>
5#include <QtAssert>
6#include <QBrush>
7#include <QFile>
8
9
10//! [1]
12{
13public:
14 enum Option {
15 NoOptions = 0x0,
16 ShowTabs = 0x1,
17 ShowAll = 0x2,
19 };
20 Q_DECLARE_FLAGS(Options, Option)
21 //...
22};
23
24Q_DECLARE_OPERATORS_FOR_FLAGS(MyClass_1::Options)
25//! [1]
26
27#if 0
28//! [meta-object flags]
29Q_FLAG(Options)
30//! [meta-object flags]
31#endif
32
34 bool isOpen() const { return true; }
35 bool isOpenError() const { return false; }
36};
37
39 static DummyDriver d;
40 return &d;
41}
42
44{
45 enum Enum {};
46
47 //! [2]
48 typedef QFlags<Enum> Flags;
49 //! [2]
50
51 //! [4]
53 qWarning("QSqlQuery::exec: database not open");
54 return false;
55 }
56 //! [4]
57
58 {
59 //! [5]
60 qint64 value = Q_INT64_C(932838457459459);
61 //! [5]
62 }
63
64 {
65 //! [6]
66 quint64 value = Q_UINT64_C(932838457459459);
67 //! [6]
68 }
69
70 {
71 //! [8]
72 qint64 value = Q_INT64_C(932838457459459);
73 //! [8]
74 }
75
76 {
77 //! [9]
78 quint64 value = Q_UINT64_C(932838457459459);
79 //! [9]
80 }
81
82 {
83 //! [10]
84 int absoluteValue;
85 int myValue = -4;
86
87 absoluteValue = qAbs(myValue);
88 // absoluteValue == 4
89 //! [10]
90 }
91
92 {
93 //! [11A]
94 double valueA = 2.3;
95 double valueB = 2.7;
96
97 int roundedValueA = qRound(valueA);
98 // roundedValueA = 2
99 int roundedValueB = qRound(valueB);
100 // roundedValueB = 3
101 //! [11A]
102 }
103
104 {
105 //! [11B]
106 float valueA = 2.3f;
107 float valueB = 2.7f;
108
109 int roundedValueA = qRound(valueA);
110 // roundedValueA = 2
111 int roundedValueB = qRound(valueB);
112 // roundedValueB = 3
113 //! [11B]
114 }
115
116 {
117 //! [12A]
118 double valueA = 42949672960.3;
119 double valueB = 42949672960.7;
120
121 qint64 roundedValueA = qRound64(valueA);
122 // roundedValueA = 42949672960
123 qint64 roundedValueB = qRound64(valueB);
124 // roundedValueB = 42949672961
125 //! [12A]
126 }
127
128 {
129 //! [12B]
130 float valueA = 42949672960.3f;
131 float valueB = 42949672960.7f;
132
133 qint64 roundedValueA = qRound64(valueA);
134 // roundedValueA = 42949672960
135 qint64 roundedValueB = qRound64(valueB);
136 // roundedValueB = 42949672961
137 //! [12B]
138 }
139
140 {
141 //! [13]
142 int myValue = 6;
143 int yourValue = 4;
144
145 int minValue = qMin(myValue, yourValue);
146 // minValue == yourValue
147 //! [13]
148 }
149
150 {
151 //! [14]
152 int myValue = 6;
153 int yourValue = 4;
154
155 int maxValue = qMax(myValue, yourValue);
156 // maxValue == myValue
157 //! [14]
158 }
159
160 {
161 //! [15]
162 int myValue = 10;
163 int minValue = 2;
164 int maxValue = 6;
165
166 int boundedValue = qBound(minValue, myValue, maxValue);
167 // boundedValue == 6
168 //! [15]
169 }
170
171 return true;
172}
173
174
175//! [17&19_include_open]
176// File: div.cpp
177
178#include <QtGlobal>
179
180int divide(int a, int b)
181{
182//! [17&19_include_open]
183
184 //! [17assert]
185 Q_ASSERT(b != 0);
186 //! [17assert]
187
188 //! [19assert]
189 Q_ASSERT_X(b != 0, "divide", "division by zero");
190 //! [19assert]
191
192 //! [17&19_return_close]
193 return a / b;
194}
195//! [17&19_return_close]
196
197#if 0
198//! [18]
199ASSERT: "b != 0" in file div.cpp, line 7
200//! [18]
201
202//! [20]
203ASSERT failure in divide: "division by zero", file div.cpp, line 7
204//! [20]
205#endif
206
207
209{
210 //! [21]
211 int *a;
212
213 Q_CHECK_PTR(a = new int[80]); // WRONG!
214
215 a = new (std::nothrow) int[80]; // Right
216 Q_CHECK_PTR(a);
217 //! [21]
218}
219
220//! [22]
221template<typename TInputType>
222const TInputType &myMin(const TInputType &value1, const TInputType &value2)
223{
224 qDebug() << Q_FUNC_INFO << "was called with value1:" << value1 << "value2:" << value2;
225
226 if(value1 < value2)
227 return value1;
228 else
229 return value2;
230}
231//! [22]
232
233
235{
236 QList<int> myList;
237 QBrush myQBrush(Qt::red);
238 int i = 0;
239 //! [24]
240 qDebug("Items in list: %d", myList.size());
241 //! [24]
242
243
244 //! [25]
245 qDebug() << "Brush:" << myQBrush << "Other value:" << i;
246 //! [25]
247
248
249 //! [qInfo_printf]
250 qInfo("Items in list: %d", myList.size());
251 //! [qInfo_printf]
252
253 //! [qInfo_stream]
254 qInfo() << "Brush:" << myQBrush << "Other value:" << i;
255 //! [qInfo_stream]
256}
257
258//! [26]
259void f(int c)
260{
261 if (c > 200)
262 qWarning("f: bad argument, c == %d", c);
263}
264//! [26]
265
267{
268 QBrush myQBrush(Qt::red);
269 int i = 0;
270 //! [27]
271 qWarning() << "Brush:" << myQBrush << "Other value:" << i;
272 //! [27]
273}
274
275//! [28]
276void load(const QString &fileName)
277{
278 QFile file(fileName);
279 if (!file.exists())
280 qCritical("File '%s' does not exist!", qUtf8Printable(fileName));
281}
282//! [28]
283
285{
286 QBrush myQBrush(Qt::red);
287 int i = 0;
288 //! [29]
289 qCritical() << "Brush:" << myQBrush << "Other value:" << i;
290 //! [29]
291}
292
293//! [30]
294int divide_by_zero(int a, int b)
295{
296 if (b == 0) // program error
297 qFatal("divide: cannot divide by zero");
298 return a / b;
299}
300//! [30]
301
303{
304 //! [31]
305 forever {
306 // ...
307 }
308 //! [31]
309}
310
311# if 0
312//! [32]
313CONFIG += no_keywords
314//! [32]
315#endif
316
317namespace snippet_34
318{
320 {
321 public:
322 QString greeting(int type);
323 };
324
325 QString tr(const char *)
326 {
327 return "";
328 }
329
330 //! [34]
332 {
333 static const char *greeting_strings[] = {
334 QT_TR_NOOP("Hello"),
335 QT_TR_NOOP("Goodbye")
336 };
337 return tr(greeting_strings[type]);
338 }
339 //! [34]
340}
341
342
344{
346 {
347 public:
348 static QString status(int type, int count);
349 static const char * const status_strings[];
350 };
351
352 QString tr(const char *, const char *, int)
353 {
354 return "";
355 }
356 //! [qttrnnoop]
357 const char * const StatusClass::status_strings[] = {
358 QT_TR_N_NOOP("There are %n new message(s)"),
359 QT_TR_N_NOOP("There are %n total message(s)")
360 };
361
362 QString StatusClass::status(int type, int count)
363 {
364 return tr(status_strings[type], nullptr, count);
365 }
366 //! [qttrnnoop]
367}
368
369QString translate(const char *, const char *, const char *, int)
370{
371 return "";
372}
373
374//! [qttranslatennoop]
375static const char * const greeting_strings[] = {
376 QT_TRANSLATE_N_NOOP("Welcome Msg", "Hello, you have %n message(s)"),
377 QT_TRANSLATE_N_NOOP("Welcome Msg", "Hi, you have %n message(s)")
378};
379
380QString global_greeting(int type, int msgcnt)
381{
382 return translate("Welcome Msg", greeting_strings[type], nullptr, msgcnt);
383}
384//! [qttranslatennoop]
385
386
388{
389 int n = 0;
390 //! [qttrid]
391 //% "%n fooish bar(s) found.\n"
392 //% "Do you want to continue?"
393 QString text = qtTrId("qtn_foo_bar", n);
394 //! [qttrid]
395}
396
397
399{
400 //! [qttrid_n_noop]
401 static const char * const ids[] = {
402 //% "%n foo(s) found."
403 QT_TRID_N_NOOP("qtn_foo"),
404 //% "%n bar(s) found."
405 QT_TRID_N_NOOP("qtn_bar"),
406 0
407 };
408
409 QString result(int type, int n)
410 {
411 return qtTrId(ids[type], n);
412 }
413 //! [qttrid_n_noop]
414}
415
416QT_BEGIN_NAMESPACE
417
418//! [38]
419struct Point3D
420{
421 int x;
422 int y;
423 int z;
424};
425
427//! [38]
428
429//! [39]
431{
432public:
433 Point2D() { data = new int[2]; }
434 Point2D(const Point2D &other) { /*...*/ }
435 ~Point2D() { delete[] data; }
436
437 Point2D &operator=(const Point2D &other) { /*...*/ }
438
439 int x() const { return data[0]; }
440 int y() const { return data[1]; }
441
442private:
443 int *data;
444};
445
447//! [39]
448
449
450//! [40]
451#if Q_BYTE_ORDER == Q_BIG_ENDIAN
452//...
453#endif
454
455//or
456
457#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
458//...
459#endif
460
461//! [40]
462
463
464//! [41]
465
466#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
467//...
468#endif
469
470//! [41]
471
472
473//! [42]
474#if Q_BYTE_ORDER == Q_BIG_ENDIAN
475//...
476#endif
477
478//! [42]
479
480//! [begin namespace macro]
481namespace QT_NAMESPACE {
482//! [begin namespace macro]
483
484//! [end namespace macro]
485}
486//! [end namespace macro]
487
488namespace snippet_43
489{
490 //! [43]
491 class MyClass : public QObject
492 {
493 private:
495 };
496
497 //! [43]
498}
499
500//! [44]
501class MyClass : public QObject
502{
503private:
504 MyClass(const MyClass &) = delete;
505 MyClass &operator=(const MyClass &) = delete;
506};
507//! [44]
508
509
511{
512 //! [46]
513 // Instead of comparing with 0.0
514 qFuzzyCompare(0.0, 1.0e-200); // This will return false
515 // Compare adding 1 to both values will fix the problem
516 qFuzzyCompare(1 + 0.0, 1 + 1.0e-200); // This will return true
517 //! [46]
518}
519
520//! [49]
521void myMessageHandler(QtMsgType, const QMessageLogContext &, const QString &);
522//! [49]
523
524//! [50]
525class B {/*...*/};
526class C {/*...*/};
527class D {/*...*/};
528struct A : public B {
531};
532//! [50]
533
534//! [51]
535template<> class QTypeInfo<A> : public QTypeInfoMerger<A, B, C, D> {};
536//! [51]
537
538QT_END_NAMESPACE
539
540namespace snippet_52
541{
542 //! [52]
543 struct Foo {
545 void overloadedFunction(int, const QString &);
546 };
547 auto ptr_1 = qOverload<>(&Foo::overloadedFunction);
549 //! [52]
550}
551
552
553//! [54]
554 struct Foo {
555 void overloadedFunction(int, const QString &);
556 void overloadedFunction(int, const QString &) const;
557 };
558 auto ptr_1 = qConstOverload<int, const QString &>(&Foo::overloadedFunction);
559 auto ptr_2 = qNonConstOverload<int, const QString &>(&Foo::overloadedFunction);
560//! [54]
561
562bool isWorkingDay(int day)
563{
564 return false;
565}
566
568{
569 //! [qlikely]
570 // the condition inside the "if" will be successful most of the times
571 for (int i = 1; i <= 365; i++) {
572 if (Q_LIKELY(isWorkingDay(i))) {
573 //...
574 }
575 //...
576 }
577 //! [qlikely]
578}
579
580//! [qunlikely]
581bool readConfiguration(const QFile &file)
582{
583 // We expect to be asked to read an existing file
584 if (Q_UNLIKELY(!file.exists())) {
585 qWarning() << "File not found";
586 return false;
587 }
588
589 //...
590 return true;
591}
592//! [qunlikely]
593
594//! [qunreachable-enum]
601//! [qunreachable-enum]
602
603int rectangle() { return 0; }
604int triangle() { return 1; }
605int circle() { return 2; }
606
608{
609 //! [qunreachable-switch]
610 switch (shape) {
611 case Rectangle:
612 return rectangle();
613 case Triangle:
614 return triangle();
615 case Circle:
616 return circle();
617 case NumShapes:
618 Q_UNREACHABLE();
619 break;
620 }
621 //! [qunreachable-switch]
622 return -1;
623}
624
625
627{
628 const char *varName = "MY_ENV_VAR";
629 bool *ok = nullptr;
630
631 //! [is-empty]
632 bool is_empty = qgetenv(varName).isEmpty();
633 //! [is-empty]
634
635 //! [to-int]
636 int to_int = qgetenv(varName).toInt(ok, 0);
637 //! [to-int]
638
639 //! [int-value_or]
640 auto value = qEnvironmentVariableIntegerValue(varName).value_or(0);
641 //! [int-value_or]
642
643 //! [int-eq0]
644 bool equals_zero = qEnvironmentVariableIntegerValue(varName) == 0;
645 //! [int-eq0]
646
647 //! [is-null]
648 bool is_not_null = !qgetenv(varName).isNull();
649 //! [is-null]
650}
651
653{
654 return "Hello, World!";
655}
656
657void process(const QChar &ch) { }
658
660{
661#if QT_DEPRECATED_SINCE(6, 6)
662 {
663 //! [as-const-0]
664 QString s = "...";
665 for (QChar ch : s) // detaches 's' (performs a deep-copy if 's' was shared)
666 process(ch);
667 for (QChar ch : qAsConst(s)) // ok, no detach attempt
668 process(ch);
669 //! [as-const-0]
670 }
671#endif // QT_DEPRECATED_SINCE(6, 6)
672
673 //! [as-const-1]
674 const QString s = "...";
675 for (QChar ch : s) // ok, no detach attempt on const objects
676 process(ch);
677 //! [as-const-1]
678
679 //! [as-const-2]
680 for (QChar ch : funcReturningQString())
681 process(ch); // OK, the returned object is kept alive for the loop's duration
682 //! [as-const-2]
683
684#if MAKE_ERRORS
685 //! [as-const-3]
686 for (QChar ch : qAsConst(funcReturningQString()))
687 process(ch); // ERROR: ch is copied from deleted memory
688 //! [as-const-3]
689
690 //! [as-const-4]
691 for (QChar ch : qAsConst(funcReturningQString()))
692 process(ch); // ERROR: ch is copied from deleted memory
693 //! [as-const-4]
694#endif
695}
696
698{
699protected:
700#if MAKE_ERRORS
701 //! [qdecloverride]
702 // generate error if this doesn't actually override anything:
703 virtual void override_func() override;
704 //! [qdecloverride]
705#endif
706
707 //! [qdeclfinal-1]
708 // more-derived classes no longer permitted to override this:
709 virtual void final_func() final;
710 //! [qdeclfinal-1]
711};
712//! [qdeclfinal-2]
713 class SomeClass final { // cannot be derived from
714 // ...
715 };
716//! [qdeclfinal-2]
virtual void final_func() final
[qdeclfinal-1]
Point2D(const Point2D &other)
Point2D & operator=(const Point2D &other)
static const char *const status_strings[]
[qttrnnoop]
static QString status(int type, int count)
QString result(int type, int n)
static const char *const ids[]
[qttrid_n_noop]
QString tr(const char *)
[end namespace macro]
QString tr(const char *, const char *, int)
void debug_info_example()
[22]
void qttrid_example()
[qttranslatennoop]
void f(int c)
[26]
bool examples()
[3]
static const char *const greeting_strings[]
[qttranslatennoop]
void warning_example()
[26]
QString translate(const char *, const char *, const char *, int)
int qunreachable_example(Shapes shape)
void load(const QString &fileName)
[28]
void qfuzzycompare_example()
[44]
Shapes
[qunlikely]
int divide(int a, int b)
[17&19_include_open]
const TInputType & myMin(const TInputType &value1, const TInputType &value2)
[22]
void qlikely_example()
QString global_greeting(int type, int msgcnt)
void qchar_examples()
void critical_example()
[28]
Q_DECLARE_TYPEINFO(Point3D, Q_PRIMITIVE_TYPE)
void myMessageHandler(QtMsgType, const QMessageLogContext &, const QString &)
[49]
DummyDriver * driver()
void qgetenv_examples()
Q_DECLARE_TYPEINFO(Point2D, Q_RELOCATABLE_TYPE)
int rectangle()
[qunreachable-enum]
int divide_by_zero(int a, int b)
[30]
void pointer_example()
[17&19_return_close]
bool readConfiguration(const QFile &file)
[qunlikely]
void process(const QChar &ch)
bool isWorkingDay(int day)
[54]
QString funcReturningQString()
void forever_example()
[30]
void overloadedFunction(int, const QString &) const
void overloadedFunction(int, const QString &)
void overloadedFunction(int, const QString &)
void overloadedFunction()