Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
6label->setAlignment({ });
8
9
11class MyClass
12{
13public:
14 enum Option {
15 NoOptions = 0x0,
16 ShowTabs = 0x1,
17 ShowAll = 0x2,
18 SqueezeBlank = 0x4
19 };
21 ...
22};
23
24Q_DECLARE_OPERATORS_FOR_FLAGS(MyClass::Options)
26
30
32typedef QFlags<Enum> Flags;
34
36if (!driver()->isOpen() || driver()->isOpenError()) {
37 qWarning("QSqlQuery::exec: database not open");
38 return false;
39}
41
42
44qint64 value = Q_INT64_C(932838457459459);
46
47
49quint64 value = Q_UINT64_C(932838457459459);
51
52
54qint64 value = Q_INT64_C(932838457459459);
56
57
59quint64 value = Q_UINT64_C(932838457459459);
61
62
65int myValue = -4;
66
68// absoluteValue == 4
70
71
73double valueA = 2.3;
74double valueB = 2.7;
75
77// roundedValueA = 2
79// roundedValueB = 3
81
83float valueA = 2.3;
84float valueB = 2.7;
85
87// roundedValueA = 2
89// roundedValueB = 3
91
92
94double valueA = 42949672960.3;
95double valueB = 42949672960.7;
96
98// roundedValueA = 42949672960
100// roundedValueB = 42949672961
102
104float valueA = 42949672960.3;
105float valueB = 42949672960.7;
106
108// roundedValueA = 42949672960
110// roundedValueB = 42949672961
112
113
115int myValue = 6;
116int yourValue = 4;
117
119// minValue == yourValue
121
122
124int myValue = 6;
125int yourValue = 4;
126
128// maxValue == myValue
130
131
133int myValue = 10;
134int minValue = 2;
135int maxValue = 6;
136
138// boundedValue == 6
140
141
143#if QT_VERSION >= QT_VERSION_CHECK(4, 1, 0)
144 QIcon icon = style()->standardIcon(QStyle::SP_TrashIcon);
145#else
146 QPixmap pixmap = style()->standardPixmap(QStyle::SP_TrashIcon);
148#endif
150
151
153// File: div.cpp
154
155#include <QtGlobal>
156
157int divide(int a, int b)
158{
159 Q_ASSERT(b != 0);
160 return a / b;
161}
163
164
166ASSERT: "b != 0" in file div.cpp, line 7
168
169
171// File: div.cpp
172
173#include <QtGlobal>
174
175int divide(int a, int b)
176{
177 Q_ASSERT_X(b != 0, "divide", "division by zero");
178 return a / b;
179}
181
182
184ASSERT failure in divide: "division by zero", file div.cpp, line 7
186
187
189int *a;
190
191Q_CHECK_PTR(a = new int[80]); // WRONG!
192
193a = new (nothrow) int[80]; // Right
196
197
199template<typename TInputType>
200const TInputType &myMin(const TInputType &value1, const TInputType &value2)
201{
202 qDebug() << Q_FUNC_INFO << "was called with value1:" << value1 << "value2:" << value2;
203
204 if(value1 < value2)
205 return value1;
206 else
207 return value2;
208}
210
211
213#include <QApplication>
214#include <stdio.h>
215#include <stdlib.h>
216
218
220{
222 static FILE *f = fopen("log.txt", "a");
223 fprintf(f, "%s\n", qPrintable(message));
224 fflush(f);
225
226 if (originalHandler)
228}
229
230int main(int argc, char **argv)
231{
233 QApplication app(argc, argv);
234 ...
235 return app.exec();
236}
238
239
241qDebug("Items in list: %d", myList.size());
243
244
246qDebug() << "Brush:" << myQBrush << "Other value:" << i;
248
249
251qInfo("Items in list: %d", myList.size());
253
255qInfo() << "Brush:" << myQBrush << "Other value:" << i;
257
259void f(int c)
260{
261 if (c > 200)
262 qWarning("f: bad argument, c == %d", c);
263}
265
266
268qWarning() << "Brush:" << myQBrush << "Other value:" << i;
270
271
273void load(const QString &fileName)
274{
276 if (!file.exists())
277 qCritical("File '%s' does not exist!", qUtf8Printable(fileName));
278}
280
281
283qCritical() << "Brush:" << myQBrush << "Other value:" << i;
285
286
288int divide(int a, int b)
289{
290 if (b == 0) // program error
291 qFatal("divide: cannot divide by zero");
292 return a / b;
293}
295
296
298forever {
299 ...
300}
302
303
305CONFIG += no_keywords
307
308
310CONFIG += no_keywords
312
313
315QString FriendlyConversation::greeting(int type)
316{
317 static const char *greeting_strings[] = {
318 QT_TR_NOOP("Hello"),
319 QT_TR_NOOP("Goodbye")
320 };
321 return tr(greeting_strings[type]);
322}
324
325
327static const char *greeting_strings[] = {
328 QT_TRANSLATE_NOOP("FriendlyConversation", "Hello"),
329 QT_TRANSLATE_NOOP("FriendlyConversation", "Goodbye")
330};
331
332QString FriendlyConversation::greeting(int type)
333{
334 return tr(greeting_strings[type]);
335}
336
337QString global_greeting(int type)
338{
339 return qApp->translate("FriendlyConversation",
340 greeting_strings[type]);
341}
343
344
346
347static { const char *source; const char *comment; } greeting_strings[] =
348{
349 QT_TRANSLATE_NOOP3("FriendlyConversation", "Hello",
350 "A really friendly hello"),
351 QT_TRANSLATE_NOOP3("FriendlyConversation", "Goodbye",
352 "A really friendly goodbye")
353};
354
355QString FriendlyConversation::greeting(int type)
356{
357 return tr(greeting_strings[type].source,
358 greeting_strings[type].comment);
359}
360
361QString global_greeting(int type)
362{
363 return qApp->translate("FriendlyConversation",
364 greeting_strings[type].source,
365 greeting_strings[type].comment);
366}
368
369
371static const char * const StatusClass::status_strings[] = {
372 QT_TR_N_NOOP("There are %n new message(s)"),
373 QT_TR_N_NOOP("There are %n total message(s)")
374};
375
376QString StatusClass::status(int type, int count)
377{
378 return tr(status_strings[type], nullptr, count);
379}
381
383static const char * const greeting_strings[] = {
384 QT_TRANSLATE_N_NOOP("Welcome Msg", "Hello, you have %n message(s)"),
385 QT_TRANSLATE_N_NOOP("Welcome Msg", "Hi, you have %n message(s)")
386};
387
388QString global_greeting(int type, int msgcnt)
389{
390 return translate("Welcome Msg", greeting_strings[type], nullptr, msgcnt);
391}
393
395static { const char * const source; const char * const comment; } status_strings[] = {
396 QT_TRANSLATE_N_NOOP3("Message Status", "Hello, you have %n message(s)",
397 "A login message status"),
398 QT_TRANSLATE_N_NOOP3("Message status", "You have %n new message(s)",
399 "A new message query status")
400};
401
402QString FriendlyConversation::greeting(int type, int count)
403{
404 return tr(status_strings[type].source,
405 status_strings[type].comment, count);
406}
407
408QString global_greeting(int type, int count)
409{
410 return qApp->translate("Message Status",
411 status_strings[type].source,
412 status_strings[type].comment,
413 count);
414}
416
417
419 //% "%n fooish bar(s) found.\n"
420 //% "Do you want to continue?"
421 QString text = qtTrId("qtn_foo_bar", n);
423
424
426static const char * const ids[] = {
427 //% "This is the first text."
428 QT_TRID_NOOP("qtn_1st_text"),
429 //% "This is the second text."
430 QT_TRID_NOOP("qtn_2nd_text"),
431 0
432};
433
434void TheClass::addLabels()
435{
436 for (int i = 0; ids[i]; ++i)
437 new QLabel(qtTrId(ids[i]), this);
438}
440
442static const char * const ids[] = {
443 //% "%n foo(s) found."
444 QT_TRID_N_NOOP("qtn_foo"),
445 //% "%n bar(s) found."
446 QT_TRID_N_NOOP("qtn_bar"),
447 0
448};
449
450QString result(int type, int n)
451{
452 return qtTrId(ids[type], n);
453}
455
457struct Point2D
458{
459 int x;
460 int y;
461};
462
465
466
468class Point2D
469{
470public:
471 Point2D() { data = new int[2]; }
472 Point2D(const Point2D &other) { ... }
473 ~Point2D() { delete[] data; }
474
475 Point2D &operator=(const Point2D &other) { ... }
476
477 int x() const { return data[0]; }
478 int y() const { return data[1]; }
479
480private:
481 int *data;
482};
483
486
487
489#if Q_BYTE_ORDER == Q_BIG_ENDIAN
490...
491#endif
492
493or
494
495#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
496...
497#endif
498
500
501
503
504#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
505...
506#endif
507
509
510
512#if Q_BYTE_ORDER == Q_BIG_ENDIAN
513...
514#endif
515
517
519namespace QT_NAMESPACE {
521
523}
525
527class MyClass : public QObject
528{
529private:
530 Q_DISABLE_COPY(MyClass)
531};
532
534
536class MyClass : public QObject
537{
538private:
539 MyClass(const MyClass &) = delete;
540 MyClass &operator=(const MyClass &) = delete;
541};
543
545QWidget w = QWidget();
547
549// Instead of comparing with 0.0
550qFuzzyCompare(0.0, 1.0e-200); // This will return false
551// Compare adding 1 to both values will fix the problem
552qFuzzyCompare(1 + 0.0, 1 + 1.0e-200); // This will return true
554
556void myMessageHandler(QtMsgType, const QMessageLogContext &, const QString &);
558
560class B {...};
561class C {...};
562class D {...};
563struct A : public B {
564 C c;
565 D d;
566};
568
570template<> class QTypeInfo<A> : public QTypeInfoMerger<A, B, C, D> {};
572
574 struct Foo {
575 void overloadedFunction();
576 void overloadedFunction(int, const QString &);
577 };
578 ... qOverload<>(&Foo::overloadedFunction)
579 ... qOverload<int, const QString &>(&Foo::overloadedFunction)
581
583 struct Foo {
584 void overloadedFunction(int, const QString &);
585 void overloadedFunction(int, const QString &) const;
586 };
587 ... qConstOverload<int, const QString &>(&Foo::overloadedFunction)
588 ... qNonConstOverload<int, const QString &>(&Foo::overloadedFunction)
590
592 // the condition inside the "if" will be successful most of the times
593 for (int i = 1; i <= 365; i++) {
594 if (Q_LIKELY(isWorkingDay(i))) {
595 ...
596 }
597 ...
598 }
600
602bool readConfiguration(const QFile &file)
603{
604 // We expect to be asked to read an existing file
605 if (Q_UNLIKELY(!file.exists())) {
606 qWarning() << "File not found";
607 return false;
608 }
609
610 ...
611 return true;
612}
614
616 enum Shapes {
617 Rectangle,
618 Triangle,
619 Circle,
620 NumShapes
621 };
623
625 switch (shape) {
626 case Rectangle:
627 return rectangle();
628 case Triangle:
629 return triangle();
630 case Circle:
631 return circle();
632 case NumShapes:
633 Q_UNREACHABLE();
634 break;
635 }
637
639#include <QtGlobal>
640
641#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
642#include <QtWidgets>
643#else
644#include <QtGui>
645#endif
647
649 qgetenv(varName).isEmpty()
651
653 qgetenv(varName).toInt(ok, 0)
655
657 !qgetenv(varName).isNull()
659
661 QString s = ...;
662 for (QChar ch : s) // detaches 's' (performs a deep-copy if 's' was shared)
663 process(ch);
664 for (QChar ch : qAsConst(s)) // ok, no detach attempt
665 process(ch);
667
669 const QString s = ...;
670 for (QChar ch : s) // ok, no detach attempt on const objects
671 process(ch);
673
675 for (QChar ch : funcReturningQString())
676 process(ch); // OK, the returned object is kept alive for the loop's duration
678
680 for (QChar ch : qAsConst(funcReturningQString()))
681 process(ch); // ERROR: ch is copied from deleted memory
683
685 for (QChar ch : qAsConst(funcReturningQString()))
686 process(ch); // ERROR: ch is copied from deleted memory
688
690 try { expr; } catch(...) { qTerminate(); }
692
694 // generate error if this doesn't actually override anything:
695 virtual void MyWidget::paintEvent(QPaintEvent*) override;
697
699 // more-derived classes no longer permitted to override this:
700 virtual void MyWidget::paintEvent(QPaintEvent*) final;
702
704 class QRect final { // cannot be derived from
705 // ...
706 };
void paintEvent(QPaintEvent *event) override
[0]
The QApplication class manages the GUI application's control flow and main settings.
static int exec()
Enters the main event loop and waits until exit() is called, then returns the value that was set to e...
\inmodule QtCore
\inmodule QtCore
Definition qfile.h:93
bool exists() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qfile.cpp:351
The QIcon class provides scalable icons in different modes and states.
Definition qicon.h:20
The QLabel widget provides a text or image display.
Definition qlabel.h:20
\inmodule QtCore
Definition qlogging.h:42
\inmodule QtCore
Definition qobject.h:103
The QPaintEvent class contains event parameters for paint events.
Definition qevent.h:486
Returns a copy of the pixmap that is transformed using the given transformation transform and transfo...
Definition qpixmap.h:27
\inmodule QtCore\reentrant
Definition qrect.h:30
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
@ SP_TrashIcon
Definition qstyle.h:731
\inmodule QtCore
Definition qtypeinfo.h:100
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
p1 load("image.bmp")
int main()
[0]
QString text
QtMessageHandler qInstallMessageHandler(QtMessageHandler h)
@ AlignTop
Definition qnamespace.h:153
@ AlignLeft
Definition qnamespace.h:144
static void * context
@ Circle
Definition qbezier.cpp:176
#define Q_UNLIKELY(x)
#define Q_LIKELY(x)
#define Q_FUNC_INFO
#define qApp
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
QT_BEGIN_NAMESPACE Q_NORETURN void qTerminate() noexcept
#define Q_DECLARE_FLAGS(Flags, Enum)
Definition qflags.h:174
#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
Definition qflags.h:194
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Definition qfloat16.h:333
qint64 qRound64(qfloat16 d) noexcept
Definition qfloat16.h:330
int qRound(qfloat16 d) noexcept
Definition qfloat16.h:327
Flags
#define forever
Definition qforeach.h:78
void uint64_t uint64_t uint64_t value2
#define qCritical
Definition qlogging.h:167
Q_CORE_EXPORT QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context, const QString &buf)
#define qInfo
Definition qlogging.h:165
#define qDebug
[1]
Definition qlogging.h:164
QtMsgType
Definition qlogging.h:29
#define qWarning
Definition qlogging.h:166
void(* QtMessageHandler)(QtMsgType, const QMessageLogContext &, const QString &)
Definition qlogging.h:191
#define qFatal
Definition qlogging.h:168
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
constexpr const T & qBound(const T &min, const T &val, const T &max)
Definition qminmax.h:44
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
constexpr T qAbs(const T &t)
Definition qnumeric.h:328
n varying highp vec2 A
GLboolean GLboolean GLboolean b
GLint GLint GLint GLint GLint x
[0]
GLfloat GLfloat GLfloat w
[0]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLenum GLsizei const GLuint * ids
GLenum GLenum GLsizei count
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLfloat GLfloat f
GLenum type
GLuint GLsizei const GLchar * label
[43]
GLuint GLsizei const GLchar * message
GLfloat n
GLint y
GLsizei GLsizei GLchar * source
GLdouble s
[6]
Definition qopenglext.h:235
const GLubyte * c
GLuint in
GLuint64EXT * result
[6]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define Q_ASSERT_X(cond, x, msg)
Definition qrandom.cpp:48
#define qUtf8Printable(string)
Definition qstring.h:1535
#define qPrintable(string)
Definition qstring.h:1531
#define tr(X)
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
#define Q_FLAG(x)
#define QT_TR_NOOP(x)
#define QT_TRANSLATE_NOOP(scope, x)
#define QT_TR_N_NOOP(x)
Q_CORE_EXPORT QString qtTrId(const char *id, int n=-1)
#define QT_TRID_N_NOOP(id)
#define QT_TRANSLATE_N_NOOP3(scope, x, comment)
#define QT_TRANSLATE_N_NOOP(scope, x)
#define QT_TRID_NOOP(id)
#define QT_TRANSLATE_NOOP3(scope, x, comment)
@ Q_PRIMITIVE_TYPE
Definition qtypeinfo.h:157
@ Q_RELOCATABLE_TYPE
Definition qtypeinfo.h:158
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:180
#define Q_UINT64_C(c)
Definition qtypes.h:58
unsigned long long quint64
Definition qtypes.h:61
long long qint64
Definition qtypes.h:60
#define Q_INT64_C(c)
Definition qtypes.h:57
static bool translate(xcb_connection_t *connection, xcb_window_t child, xcb_window_t parent, int *x, int *y)
ASSERT failure in file div line int * a
[20]
const TInputType & myMin(const TInputType &value1, const TInputType &value2)
[21]
double valueA
[10]
int absoluteValue
[9]
ASSERT failure in divide
[19]
void logToFile(QtMsgType type, const QMessageLogContext &context, const QString &msg)
int myValue
[12B]
QtMessageHandler originalHandler
[22]
Q_CHECK_PTR(a=new int[80])
if(qFloatDistance(a, b)<(1<< 7))
[0]
QFile file
[0]
QSharedPointer< T > other(t)
[5]
QApplication app(argc, argv)
[0]
widget render & pixmap
QList< QString > MyClass