Qt 6.x
The Qt SDK
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.h>
214#include <stdio.h>
215#include <stdlib.h>
216
218{
219 QByteArray localMsg = msg.toLocal8Bit();
220 const char *file = context.file ? context.file : "";
221 const char *function = context.function ? context.function : "";
222 switch (type) {
223 case QtDebugMsg:
224 fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
225 break;
226 case QtInfoMsg:
227 fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
228 break;
229 case QtWarningMsg:
230 fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
231 break;
232 case QtCriticalMsg:
233 fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
234 break;
235 case QtFatalMsg:
236 fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
237 break;
238 }
239}
240
241int main(int argc, char **argv)
242{
244 QApplication app(argc, argv);
245 ...
246 return app.exec();
247}
249
250
252qDebug("Items in list: %d", myList.size());
254
255
257qDebug() << "Brush:" << myQBrush << "Other value:" << i;
259
260
262qInfo("Items in list: %d", myList.size());
264
266qInfo() << "Brush:" << myQBrush << "Other value:" << i;
268
270void f(int c)
271{
272 if (c > 200)
273 qWarning("f: bad argument, c == %d", c);
274}
276
277
279qWarning() << "Brush:" << myQBrush << "Other value:" << i;
281
282
284void load(const QString &fileName)
285{
287 if (!file.exists())
288 qCritical("File '%s' does not exist!", qUtf8Printable(fileName));
289}
291
292
294qCritical() << "Brush:" << myQBrush << "Other value:" << i;
296
297
299int divide(int a, int b)
300{
301 if (b == 0) // program error
302 qFatal("divide: cannot divide by zero");
303 return a / b;
304}
306
307
309forever {
310 ...
311}
313
314
316CONFIG += no_keywords
318
319
321CONFIG += no_keywords
323
324
326QString FriendlyConversation::greeting(int type)
327{
328 static const char *greeting_strings[] = {
329 QT_TR_NOOP("Hello"),
330 QT_TR_NOOP("Goodbye")
331 };
332 return tr(greeting_strings[type]);
333}
335
336
338static const char *greeting_strings[] = {
339 QT_TRANSLATE_NOOP("FriendlyConversation", "Hello"),
340 QT_TRANSLATE_NOOP("FriendlyConversation", "Goodbye")
341};
342
343QString FriendlyConversation::greeting(int type)
344{
345 return tr(greeting_strings[type]);
346}
347
348QString global_greeting(int type)
349{
350 return qApp->translate("FriendlyConversation",
351 greeting_strings[type]);
352}
354
355
357
358static { const char *source; const char *comment; } greeting_strings[] =
359{
360 QT_TRANSLATE_NOOP3("FriendlyConversation", "Hello",
361 "A really friendly hello"),
362 QT_TRANSLATE_NOOP3("FriendlyConversation", "Goodbye",
363 "A really friendly goodbye")
364};
365
366QString FriendlyConversation::greeting(int type)
367{
368 return tr(greeting_strings[type].source,
369 greeting_strings[type].comment);
370}
371
372QString global_greeting(int type)
373{
374 return qApp->translate("FriendlyConversation",
375 greeting_strings[type].source,
376 greeting_strings[type].comment);
377}
379
380
382static const char * const StatusClass::status_strings[] = {
383 QT_TR_N_NOOP("There are %n new message(s)"),
384 QT_TR_N_NOOP("There are %n total message(s)")
385};
386
387QString StatusClass::status(int type, int count)
388{
389 return tr(status_strings[type], nullptr, count);
390}
392
394static const char * const greeting_strings[] = {
395 QT_TRANSLATE_N_NOOP("Welcome Msg", "Hello, you have %n message(s)"),
396 QT_TRANSLATE_N_NOOP("Welcome Msg", "Hi, you have %n message(s)")
397};
398
399QString global_greeting(int type, int msgcnt)
400{
401 return translate("Welcome Msg", greeting_strings[type], nullptr, msgcnt);
402}
404
406static { const char * const source; const char * const comment; } status_strings[] = {
407 QT_TRANSLATE_N_NOOP3("Message Status", "Hello, you have %n message(s)",
408 "A login message status"),
409 QT_TRANSLATE_N_NOOP3("Message status", "You have %n new message(s)",
410 "A new message query status")
411};
412
413QString FriendlyConversation::greeting(int type, int count)
414{
415 return tr(status_strings[type].source,
416 status_strings[type].comment, count);
417}
418
419QString global_greeting(int type, int count)
420{
421 return qApp->translate("Message Status",
422 status_strings[type].source,
423 status_strings[type].comment,
424 count);
425}
427
428
430 //% "%n fooish bar(s) found.\n"
431 //% "Do you want to continue?"
432 QString text = qtTrId("qtn_foo_bar", n);
434
435
437static const char * const ids[] = {
438 //% "This is the first text."
439 QT_TRID_NOOP("qtn_1st_text"),
440 //% "This is the second text."
441 QT_TRID_NOOP("qtn_2nd_text"),
442 0
443};
444
445void TheClass::addLabels()
446{
447 for (int i = 0; ids[i]; ++i)
448 new QLabel(qtTrId(ids[i]), this);
449}
451
453static const char * const ids[] = {
454 //% "%n foo(s) found."
455 QT_TRID_N_NOOP("qtn_foo"),
456 //% "%n bar(s) found."
457 QT_TRID_N_NOOP("qtn_bar"),
458 0
459};
460
461QString result(int type, int n)
462{
463 return qtTrId(ids[type], n);
464}
466
468struct Point2D
469{
470 int x;
471 int y;
472};
473
476
477
479class Point2D
480{
481public:
482 Point2D() { data = new int[2]; }
483 Point2D(const Point2D &other) { ... }
484 ~Point2D() { delete[] data; }
485
486 Point2D &operator=(const Point2D &other) { ... }
487
488 int x() const { return data[0]; }
489 int y() const { return data[1]; }
490
491private:
492 int *data;
493};
494
497
498
500#if Q_BYTE_ORDER == Q_BIG_ENDIAN
501...
502#endif
503
504or
505
506#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
507...
508#endif
509
511
512
514
515#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
516...
517#endif
518
520
521
523#if Q_BYTE_ORDER == Q_BIG_ENDIAN
524...
525#endif
526
528
530namespace QT_NAMESPACE {
532
534}
536
538class MyClass : public QObject
539{
540private:
541 Q_DISABLE_COPY(MyClass)
542};
543
545
547class MyClass : public QObject
548{
549private:
550 MyClass(const MyClass &) = delete;
551 MyClass &operator=(const MyClass &) = delete;
552};
554
556QWidget w = QWidget();
558
560// Instead of comparing with 0.0
561qFuzzyCompare(0.0, 1.0e-200); // This will return false
562// Compare adding 1 to both values will fix the problem
563qFuzzyCompare(1 + 0.0, 1 + 1.0e-200); // This will return true
565
567void myMessageHandler(QtMsgType, const QMessageLogContext &, const QString &);
569
571class B {...};
572class C {...};
573class D {...};
574struct A : public B {
575 C c;
576 D d;
577};
579
581template<> class QTypeInfo<A> : public QTypeInfoMerger<A, B, C, D> {};
583
585 struct Foo {
586 void overloadedFunction();
587 void overloadedFunction(int, const QString &);
588 };
589 ... qOverload<>(&Foo::overloadedFunction)
590 ... qOverload<int, const QString &>(&Foo::overloadedFunction)
592
594 struct Foo {
595 void overloadedFunction(int, const QString &);
596 void overloadedFunction(int, const QString &) const;
597 };
598 ... qConstOverload<int, const QString &>(&Foo::overloadedFunction)
599 ... qNonConstOverload<int, const QString &>(&Foo::overloadedFunction)
601
603 // the condition inside the "if" will be successful most of the times
604 for (int i = 1; i <= 365; i++) {
605 if (Q_LIKELY(isWorkingDay(i))) {
606 ...
607 }
608 ...
609 }
611
613bool readConfiguration(const QFile &file)
614{
615 // We expect to be asked to read an existing file
616 if (Q_UNLIKELY(!file.exists())) {
617 qWarning() << "File not found";
618 return false;
619 }
620
621 ...
622 return true;
623}
625
627 enum Shapes {
628 Rectangle,
629 Triangle,
630 Circle,
631 NumShapes
632 };
634
636 switch (shape) {
637 case Rectangle:
638 return rectangle();
639 case Triangle:
640 return triangle();
641 case Circle:
642 return circle();
643 case NumShapes:
644 Q_UNREACHABLE();
645 break;
646 }
648
650#include <QtGlobal>
651
652#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
653#include <QtWidgets>
654#else
655#include <QtGui>
656#endif
658
660 qgetenv(varName).isEmpty()
662
664 qgetenv(varName).toInt(ok, 0)
666
668 !qgetenv(varName).isNull()
670
672 QString s = ...;
673 for (QChar ch : s) // detaches 's' (performs a deep-copy if 's' was shared)
674 process(ch);
675 for (QChar ch : qAsConst(s)) // ok, no detach attempt
676 process(ch);
678
680 const QString s = ...;
681 for (QChar ch : s) // ok, no detach attempt on const objects
682 process(ch);
684
686 for (QChar ch : funcReturningQString())
687 process(ch); // OK, the returned object is kept alive for the loop's duration
689
691 for (QChar ch : qAsConst(funcReturningQString()))
692 process(ch); // ERROR: ch is copied from deleted memory
694
696 for (QChar ch : qAsConst(funcReturningQString()))
697 process(ch); // ERROR: ch is copied from deleted memory
699
701 try { expr; } catch(...) { qTerminate(); }
703
705 // generate error if this doesn't actually override anything:
706 virtual void MyWidget::paintEvent(QPaintEvent*) override;
708
710 // more-derived classes no longer permitted to override this:
711 virtual void MyWidget::paintEvent(QPaintEvent*) final;
713
715 class QRect final { // cannot be derived from
716 // ...
717 };
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
Definition qbytearray.h:57
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:122
int toInt(bool *ok=nullptr, int base=10) const
Returns the byte array converted to an int using base base, which is ten by default.
bool isEmpty() const noexcept
Returns true if the byte array has size 0; otherwise returns false.
Definition qbytearray.h:106
bool isNull() const noexcept
Returns true if this byte array is null; otherwise returns false.
\inmodule QtCore
Definition qchar.h:48
\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:39
\inmodule QtCore
Definition qobject.h:90
The QPaintEvent class contains event parameters for paint events.
Definition qevent.h:485
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:127
QByteArray toLocal8Bit() const &
Definition qstring.h:567
@ SP_TrashIcon
Definition qstyle.h:729
\inmodule QtCore
Definition qtypeinfo.h:92
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:152
@ AlignLeft
Definition qnamespace.h:143
static void * context
@ Circle
Definition qbezier.cpp:278
#define Q_UNLIKELY(x)
#define Q_LIKELY(x)
#define Q_FUNC_INFO
#define qApp
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction function
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:287
qint64 qRound64(qfloat16 d) noexcept
Definition qfloat16.h:284
int qRound(qfloat16 d) noexcept
Definition qfloat16.h:281
Flags
#define forever
Definition qforeach.h:78
void uint64_t uint64_t uint64_t value2
#define qCritical
Definition qlogging.h:163
#define qInfo
Definition qlogging.h:161
#define qDebug
[1]
Definition qlogging.h:160
QtMsgType
Definition qlogging.h:29
@ QtCriticalMsg
Definition qlogging.h:32
@ QtInfoMsg
Definition qlogging.h:34
@ QtWarningMsg
Definition qlogging.h:31
@ QtFatalMsg
Definition qlogging.h:33
@ QtDebugMsg
Definition qlogging.h:30
#define qWarning
Definition qlogging.h:162
#define qFatal
Definition qlogging.h:164
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
GLfloat GLfloat f
GLenum type
GLuint GLsizei const GLchar * label
[43]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLfloat n
GLint y
GLsizei GLsizei GLchar * source
const GLubyte * c
GLuint in
GLuint64EXT * result
[6]
GLdouble s
[6]
Definition qopenglext.h:235
#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:1395
#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:144
@ Q_RELOCATABLE_TYPE
Definition qtypeinfo.h:145
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:163
#define Q_UINT64_C(c)
Definition qtypes.h:53
unsigned long long quint64
Definition qtypes.h:56
long long qint64
Definition qtypes.h:55
#define Q_INT64_C(c)
Definition qtypes.h:52
static bool translate(xcb_connection_t *connection, xcb_window_t child, xcb_window_t parent, int *x, int *y)
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
[22]
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]
int myValue
[12B]
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