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
222{
223 //! [21]
224 int *a;
225
226 Q_CHECK_PTR(a = new int[80]); // WRONG!
227
228 a = new (std::nothrow) int[80]; // Right
229 Q_CHECK_PTR(a);
230 //! [21]
231}
232
233//! [22]
234template<typename TInputType>
235const TInputType &myMin(const TInputType &value1, const TInputType &value2)
236{
237 qDebug() << Q_FUNC_INFO << "was called with value1:" << value1 << "value2:" << value2;
238
239 if(value1 < value2)
240 return value1;
241 else
242 return value2;
243}
244//! [22]
245
246
248{
249 QList<int> myList;
250 QBrush myQBrush(Qt::red);
251 int i = 0;
252 //! [24]
253 qDebug("Items in list: %d", myList.size());
254 //! [24]
255
256
257 //! [25]
258 qDebug() << "Brush:" << myQBrush << "Other value:" << i;
259 //! [25]
260
261
262 //! [qInfo_printf]
263 qInfo("Items in list: %d", myList.size());
264 //! [qInfo_printf]
265
266 //! [qInfo_stream]
267 qInfo() << "Brush:" << myQBrush << "Other value:" << i;
268 //! [qInfo_stream]
269}
270
271//! [26]
272void f(int c)
273{
274 if (c > 200)
275 qWarning("f: bad argument, c == %d", c);
276}
277//! [26]
278
280{
281 QBrush myQBrush(Qt::red);
282 int i = 0;
283 //! [27]
284 qWarning() << "Brush:" << myQBrush << "Other value:" << i;
285 //! [27]
286}
287
288//! [28]
289void load(const QString &fileName)
290{
291 QFile file(fileName);
292 if (!file.exists())
293 qCritical("File '%s' does not exist!", qUtf8Printable(fileName));
294}
295//! [28]
296
298{
299 QBrush myQBrush(Qt::red);
300 int i = 0;
301 //! [29]
302 qCritical() << "Brush:" << myQBrush << "Other value:" << i;
303 //! [29]
304}
305
306//! [30]
307int divide_by_zero(int a, int b)
308{
309 if (b == 0) // program error
310 qFatal("divide: cannot divide by zero");
311 return a / b;
312}
313//! [30]
314
316{
317 //! [31]
318 forever {
319 // ...
320 }
321 //! [31]
322}
323
324# if 0
325//! [32]
326CONFIG += no_keywords
327//! [32]
328#endif
329
330namespace snippet_34
331{
333 {
334 public:
335 QString greeting(int type);
336 };
337
338 QString tr(const char *)
339 {
340 return "";
341 }
342
343 //! [34]
345 {
346 static const char *greeting_strings[] = {
347 QT_TR_NOOP("Hello"),
348 QT_TR_NOOP("Goodbye")
349 };
350 return tr(greeting_strings[type]);
351 }
352 //! [34]
353}
354
355
357{
359 {
360 public:
361 static QString status(int type, int count);
362 static const char * const status_strings[];
363 };
364
365 QString tr(const char *, const char *, int)
366 {
367 return "";
368 }
369 //! [qttrnnoop]
370 const char * const StatusClass::status_strings[] = {
371 QT_TR_N_NOOP("There are %n new message(s)"),
372 QT_TR_N_NOOP("There are %n total message(s)")
373 };
374
375 QString StatusClass::status(int type, int count)
376 {
377 return tr(status_strings[type], nullptr, count);
378 }
379 //! [qttrnnoop]
380}
381
382QString translate(const char *, const char *, const char *, int)
383{
384 return "";
385}
386
387//! [qttranslatennoop]
388static const char * const greeting_strings[] = {
389 QT_TRANSLATE_N_NOOP("Welcome Msg", "Hello, you have %n message(s)"),
390 QT_TRANSLATE_N_NOOP("Welcome Msg", "Hi, you have %n message(s)")
391};
392
393QString global_greeting(int type, int msgcnt)
394{
395 return translate("Welcome Msg", greeting_strings[type], nullptr, msgcnt);
396}
397//! [qttranslatennoop]
398
399
401{
402 int n = 0;
403 //! [qttrid]
404 //% "%n fooish bar(s) found.\n"
405 //% "Do you want to continue?"
406 QString text = qtTrId("qtn_foo_bar", n);
407 //! [qttrid]
408}
409
410
412{
413 //! [qttrid_n_noop]
414 static const char * const ids[] = {
415 //% "%n foo(s) found."
416 QT_TRID_N_NOOP("qtn_foo"),
417 //% "%n bar(s) found."
418 QT_TRID_N_NOOP("qtn_bar"),
419 0
420 };
421
422 QString result(int type, int n)
423 {
424 return qtTrId(ids[type], n);
425 }
426 //! [qttrid_n_noop]
427}
428
429QT_BEGIN_NAMESPACE
430
431//! [38]
432struct Point3D
433{
434 int x;
435 int y;
436 int z;
437};
438
440//! [38]
441
442//! [39]
444{
445public:
446 Point2D() { data = new int[2]; }
447 Point2D(const Point2D &other) { /*...*/ }
448 ~Point2D() { delete[] data; }
449
450 Point2D &operator=(const Point2D &other) { /*...*/ }
451
452 int x() const { return data[0]; }
453 int y() const { return data[1]; }
454
455private:
456 int *data;
457};
458
460//! [39]
461
462
463//! [40]
464#if Q_BYTE_ORDER == Q_BIG_ENDIAN
465//...
466#endif
467
468//or
469
470#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
471//...
472#endif
473
474//! [40]
475
476
477//! [41]
478
479#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
480//...
481#endif
482
483//! [41]
484
485
486//! [42]
487#if Q_BYTE_ORDER == Q_BIG_ENDIAN
488//...
489#endif
490
491//! [42]
492
493//! [begin namespace macro]
494namespace QT_NAMESPACE {
495//! [begin namespace macro]
496
497//! [end namespace macro]
498}
499//! [end namespace macro]
500
501namespace snippet_43
502{
503 //! [43]
504 class MyClass : public QObject
505 {
506 private:
508 };
509
510 //! [43]
511}
512
513//! [44]
514class MyClass : public QObject
515{
516private:
517 MyClass(const MyClass &) = delete;
518 MyClass &operator=(const MyClass &) = delete;
519};
520//! [44]
521
522
524{
525 //! [46]
526 // Instead of comparing with 0.0
527 qFuzzyCompare(0.0, 1.0e-200); // This will return false
528 // Compare adding 1 to both values will fix the problem
529 qFuzzyCompare(1 + 0.0, 1 + 1.0e-200); // This will return true
530 //! [46]
531}
532
533//! [49]
534void myMessageHandler(QtMsgType, const QMessageLogContext &, const QString &);
535//! [49]
536
537//! [50]
538class B {/*...*/};
539class C {/*...*/};
540class D {/*...*/};
541struct A : public B {
544};
545//! [50]
546
547//! [51]
548template<> class QTypeInfo<A> : public QTypeInfoMerger<A, B, C, D> {};
549//! [51]
550
551QT_END_NAMESPACE
552
553namespace snippet_52
554{
555 //! [52]
556 struct Foo {
558 void overloadedFunction(int, const QString &);
559 };
560 auto ptr_1 = qOverload<>(&Foo::overloadedFunction);
562 //! [52]
563}
564
565
566//! [54]
567 struct Foo {
568 void overloadedFunction(int, const QString &);
569 void overloadedFunction(int, const QString &) const;
570 };
571 auto ptr_1 = qConstOverload<int, const QString &>(&Foo::overloadedFunction);
572 auto ptr_2 = qNonConstOverload<int, const QString &>(&Foo::overloadedFunction);
573//! [54]
574
575bool isWorkingDay(int day)
576{
577 return false;
578}
579
581{
582 //! [qlikely]
583 // the condition inside the "if" will be successful most of the times
584 for (int i = 1; i <= 365; i++) {
585 if (Q_LIKELY(isWorkingDay(i))) {
586 //...
587 }
588 //...
589 }
590 //! [qlikely]
591}
592
593//! [qunlikely]
594bool readConfiguration(const QFile &file)
595{
596 // We expect to be asked to read an existing file
597 if (Q_UNLIKELY(!file.exists())) {
598 qWarning() << "File not found";
599 return false;
600 }
601
602 //...
603 return true;
604}
605//! [qunlikely]
606
607//! [qunreachable-enum]
614//! [qunreachable-enum]
615
616int rectangle() { return 0; }
617int triangle() { return 1; }
618int circle() { return 2; }
619
621{
622 //! [qunreachable-switch]
623 switch (shape) {
624 case Rectangle:
625 return rectangle();
626 case Triangle:
627 return triangle();
628 case Circle:
629 return circle();
630 case NumShapes:
631 Q_UNREACHABLE();
632 break;
633 }
634 //! [qunreachable-switch]
635 return -1;
636}
637
638
640{
641 const char *varName = "MY_ENV_VAR";
642 bool *ok = nullptr;
643
644 //! [is-empty]
645 bool is_empty = qgetenv(varName).isEmpty();
646 //! [is-empty]
647
648 //! [to-int]
649 int to_int = qgetenv(varName).toInt(ok, 0);
650 //! [to-int]
651
652 //! [int-value_or]
653 auto value = qEnvironmentVariableIntegerValue(varName).value_or(0);
654 //! [int-value_or]
655
656 //! [int-eq0]
657 bool equals_zero = qEnvironmentVariableIntegerValue(varName) == 0;
658 //! [int-eq0]
659
660 //! [is-null]
661 bool is_not_null = !qgetenv(varName).isNull();
662 //! [is-null]
663}
664
666{
667 return "Hello, World!";
668}
669
670void process(const QChar &ch) { }
671
673{
674#if QT_DEPRECATED_SINCE(6, 6)
675 {
676 //! [as-const-0]
677 QString s = "...";
678 for (QChar ch : s) // detaches 's' (performs a deep-copy if 's' was shared)
679 process(ch);
680 for (QChar ch : qAsConst(s)) // ok, no detach attempt
681 process(ch);
682 //! [as-const-0]
683 }
684#endif // QT_DEPRECATED_SINCE(6, 6)
685
686 //! [as-const-1]
687 const QString s = "...";
688 for (QChar ch : s) // ok, no detach attempt on const objects
689 process(ch);
690 //! [as-const-1]
691
692 //! [as-const-2]
693 for (QChar ch : funcReturningQString())
694 process(ch); // OK, the returned object is kept alive for the loop's duration
695 //! [as-const-2]
696
697#if MAKE_ERRORS
698 //! [as-const-3]
699 for (QChar ch : qAsConst(funcReturningQString()))
700 process(ch); // ERROR: ch is copied from deleted memory
701 //! [as-const-3]
702
703 //! [as-const-4]
704 for (QChar ch : qAsConst(funcReturningQString()))
705 process(ch); // ERROR: ch is copied from deleted memory
706 //! [as-const-4]
707#endif
708}
709
711{
712protected:
713#if MAKE_ERRORS
714 //! [qdecloverride]
715 // generate error if this doesn't actually override anything:
716 virtual void override_func() override;
717 //! [qdecloverride]
718#endif
719
720 //! [qdeclfinal-1]
721 // more-derived classes no longer permitted to override this:
722 virtual void final_func() final;
723 //! [qdeclfinal-1]
724};
725//! [qdeclfinal-2]
726 class SomeClass final { // cannot be derived from
727 // ...
728 };
729//! [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]
[meta-object flags]
void overloadedFunction(int, const QString &) const
void overloadedFunction(int, const QString &)
void overloadedFunction(int, const QString &)
void overloadedFunction()