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//! [meta-object flags]
29{
31public:
32 enum Option {
33 NoOptions = 0x0,
34 ShowTabs = 0x1,
35 ShowAll = 0x2,
37 };
38 Q_DECLARE_FLAGS(Options, Option)
40 //...
41};
42
43Q_DECLARE_OPERATORS_FOR_FLAGS(MyClass_2::Options)
44//! [meta-object flags]
45
47 bool isOpen() const { return true; }
48 bool isOpenError() const { return false; }
49};
50
52 static DummyDriver d;
53 return &d;
54}
55
57{
58 enum Enum {};
59
60 //! [2]
61 typedef QFlags<Enum> Flags;
62 //! [2]
63
64 //! [4]
66 qWarning("QSqlQuery::exec: database not open");
67 return false;
68 }
69 //! [4]
70
71 {
72 //! [5]
73 qint64 value = Q_INT64_C(932838457459459);
74 //! [5]
75 }
76
77 {
78 //! [6]
79 quint64 value = Q_UINT64_C(932838457459459);
80 //! [6]
81 }
82
83 {
84 //! [8]
85 qint64 value = Q_INT64_C(932838457459459);
86 //! [8]
87 }
88
89 {
90 //! [9]
91 quint64 value = Q_UINT64_C(932838457459459);
92 //! [9]
93 }
94
95 {
96 //! [10]
97 int absoluteValue;
98 int myValue = -4;
99
100 absoluteValue = qAbs(myValue);
101 // absoluteValue == 4
102 //! [10]
103 }
104
105 {
106 //! [11A]
107 double valueA = 2.3;
108 double valueB = 2.7;
109
110 int roundedValueA = qRound(valueA);
111 // roundedValueA = 2
112 int roundedValueB = qRound(valueB);
113 // roundedValueB = 3
114 //! [11A]
115 }
116
117 {
118 //! [11B]
119 float valueA = 2.3f;
120 float valueB = 2.7f;
121
122 int roundedValueA = qRound(valueA);
123 // roundedValueA = 2
124 int roundedValueB = qRound(valueB);
125 // roundedValueB = 3
126 //! [11B]
127 }
128
129 {
130 //! [12A]
131 double valueA = 42949672960.3;
132 double valueB = 42949672960.7;
133
134 qint64 roundedValueA = qRound64(valueA);
135 // roundedValueA = 42949672960
136 qint64 roundedValueB = qRound64(valueB);
137 // roundedValueB = 42949672961
138 //! [12A]
139 }
140
141 {
142 //! [12B]
143 float valueA = 42949672960.3f;
144 float valueB = 42949672960.7f;
145
146 qint64 roundedValueA = qRound64(valueA);
147 // roundedValueA = 42949672960
148 qint64 roundedValueB = qRound64(valueB);
149 // roundedValueB = 42949672961
150 //! [12B]
151 }
152
153 {
154 //! [13]
155 int myValue = 6;
156 int yourValue = 4;
157
158 int minValue = qMin(myValue, yourValue);
159 // minValue == yourValue
160 //! [13]
161 }
162
163 {
164 //! [14]
165 int myValue = 6;
166 int yourValue = 4;
167
168 int maxValue = qMax(myValue, yourValue);
169 // maxValue == myValue
170 //! [14]
171 }
172
173 {
174 //! [15]
175 int myValue = 10;
176 int minValue = 2;
177 int maxValue = 6;
178
179 int boundedValue = qBound(minValue, myValue, maxValue);
180 // boundedValue == 6
181 //! [15]
182 }
183
184 return true;
185}
186
187
188//! [17&19_include_open]
189// File: div.cpp
190
191#include <QtGlobal>
192
193int divide(int a, int b)
194{
195//! [17&19_include_open]
196
197 //! [17assert]
198 Q_ASSERT(b != 0);
199 //! [17assert]
200
201 //! [19assert]
202 Q_ASSERT_X(b != 0, "divide", "division by zero");
203 //! [19assert]
204
205 //! [17&19_return_close]
206 return a / b;
207}
208//! [17&19_return_close]
209
210#if 0
211//! [18]
212ASSERT: "b != 0" in file div.cpp, line 7
213//! [18]
214
215//! [20]
216ASSERT failure in divide: "division by zero", file div.cpp, line 7
217//! [20]
218#endif
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)
[17&19_return_close]
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]
bool readConfiguration(const QFile &file)
[qunlikely]
void process(const QChar &ch)
bool isWorkingDay(int day)
[54]
QString funcReturningQString()
void forever_example()
[30]
[meta-object flags]
void overloadedFunction(int, const QString &) const
void overloadedFunction(int, const QString &)
void overloadedFunction(int, const QString &)
void overloadedFunction()