summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorOleksii Zbykovskyi <Oleksii.Zbykovskyi@qt.io>2025-07-16 10:57:53 +0200
committerOleksii Zbykovskyi <Oleksii.Zbykovskyi@qt.io>2025-07-24 18:52:41 +0200
commit80b61558814c435949e70391d7e7d812bc7b85d7 (patch)
tree85109f88462aba4d4db9b2ecf7e0f2c40b18d7c5 /src
parente34914d5bfcecac4c2e242d4653391a62d7fbc81 (diff)
Add snippets to the build system and fix related issues
Created CMakeLists.txt file with all compilable snippets by now, and added it to the build system. Also fixed some previous files to make them build correctly. And fixed some documentation related to the snippets change. Task-number: QTBUG-137566 Change-Id: Ibf4321dd243bdf774c508bc1e43f9019a9074c30 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/doc/snippets/CMakeLists.txt1
-rw-r--r--src/corelib/doc/snippets/code/CMakeLists.txt46
-rw-r--r--src/corelib/doc/snippets/code/doc_src_qplugin.cpp4
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp251
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_global_qglobal_widgets.cpp208
-rw-r--r--src/corelib/global/qflags.qdoc2
-rw-r--r--src/corelib/global/qlogging.cpp2
-rw-r--r--src/corelib/global/qtclasshelpermacros.qdoc2
-rw-r--r--src/corelib/global/qttranslation.qdoc8
-rw-r--r--src/corelib/global/qtversionchecks.cpp4
10 files changed, 285 insertions, 243 deletions
diff --git a/src/corelib/doc/snippets/CMakeLists.txt b/src/corelib/doc/snippets/CMakeLists.txt
index 78cd4f6b001..e7194e4b305 100644
--- a/src/corelib/doc/snippets/CMakeLists.txt
+++ b/src/corelib/doc/snippets/CMakeLists.txt
@@ -66,3 +66,4 @@ add_subdirectory(eventfilters)
add_subdirectory(qmetaobject-invokable)
add_subdirectory(qmetaobject-revision)
add_subdirectory(qprocess)
+add_subdirectory(code)
diff --git a/src/corelib/doc/snippets/code/CMakeLists.txt b/src/corelib/doc/snippets/code/CMakeLists.txt
new file mode 100644
index 00000000000..0f4c45cb64d
--- /dev/null
+++ b/src/corelib/doc/snippets/code/CMakeLists.txt
@@ -0,0 +1,46 @@
+add_library(corelib_snippets_code OBJECT
+ doc_src_containers.cpp
+ doc_src_properties.cpp
+ doc_src_qalgorithms.cpp
+ doc_src_qcache.cpp
+ doc_src_qiterator.cpp
+ doc_src_qnamespace.cpp
+ doc_src_qplugin.cpp
+ doc_src_qset.cpp
+ doc_src_qvarlengtharray.cpp
+ doc_src_resources.cpp
+ src_corelib_animation_qparallelanimationgroup.cpp
+ src_corelib_animation_qsequentialanimationgroup.cpp
+ src_corelib_animation_qvariantanimation.cpp
+ src_corelib_concurrent_qthreadpool.cpp
+ src_corelib_global_qglobal.cpp
+ src_corelib_global_qlogging.cpp
+ src_corelib_global_qnumeric.cpp
+ src_corelib_global_qoperatingsystemversion.cpp
+ src_corelib_global_qrandom.cpp
+)
+
+target_link_libraries(corelib_snippets_code PRIVATE
+ Qt::Core
+)
+
+qt_internal_extend_target(corelib_snippets_code CONDITION QT_FEATURE_widgets
+ LIBRARIES
+ Qt::Widgets
+ SOURCES
+ doc_src_objecttrees.cpp
+ src_corelib_animation_qpropertyanimation.cpp
+ src_corelib_global_qglobal_widgets.cpp
+)
+
+qt_internal_extend_target(corelib_snippets_code CONDITION QT_FEATURE_gui
+ LIBRARIES
+ Qt::Gui
+)
+
+qt_internal_extend_target(corelib_snippets_code CONDITION QT_FEATURE_network
+ LIBRARIES
+ Qt::Network
+)
+
+set_target_properties(corelib_snippets_code PROPERTIES COMPILE_OPTIONS "-w")
diff --git a/src/corelib/doc/snippets/code/doc_src_qplugin.cpp b/src/corelib/doc/snippets/code/doc_src_qplugin.cpp
index 4382d13838a..cd56f0761ba 100644
--- a/src/corelib/doc/snippets/code/doc_src_qplugin.cpp
+++ b/src/corelib/doc/snippets/code/doc_src_qplugin.cpp
@@ -3,6 +3,8 @@
#include <QtPlugin>
+QT_BEGIN_NAMESPACE
+
//! [0]
namespace Foo
{
@@ -24,3 +26,5 @@ class MyInstance : public QObject
//! [2]
Q_IMPORT_PLUGIN(qjpeg)
//! [2]
+
+QT_END_NAMESPACE
diff --git a/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp b/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp
index 61621f54911..d89bf57f600 100644
--- a/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp
@@ -1,19 +1,12 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-#if __has_include(<QLabel>)
-#include <QLabel>
-void label_example()
-{
- QLabel *label = new QLabel;
- //! [0]
- label->setAlignment(Qt::AlignLeft | Qt::AlignTop);
- label->setAlignment({ });
- //! [0]
-}
-#endif
-
#include <QMetaProperty>
+#include <QtAssert>
+#include <QBrush>
+#include <QFile>
+
+
//! [1]
class MyClass_1
{
@@ -178,27 +171,6 @@ bool examples()
return true;
}
-#if __has_include(<QStyle>)
-#include <QStyle>
-
-QStyle *style()
-{
- static QStyle *s = nullptr;
- return s;
-}
-
-void snippet_16()
-{
- //! [16]
- #if QT_VERSION >= QT_VERSION_CHECK(4, 1, 0)
- QIcon icon = style()->standardIcon(QStyle::SP_TrashIcon);
- #else
- QPixmap pixmap = style()->standardPixmap(QStyle::SP_TrashIcon);
- QIcon icon(pixmap);
- #endif
- //! [16]
-}
-#endif
//! [17&19_include_open]
// File: div.cpp
@@ -232,7 +204,6 @@ ASSERT failure in divide: "division by zero", file div.cpp, line 7
//! [20]
#endif
-#include <QtAssert>
void pointer_example()
{
@@ -259,36 +230,6 @@ const TInputType &myMin(const TInputType &value1, const TInputType &value2)
}
//! [22]
-# if __has_include(<QWidget>)
-//! [23]
-#include <QApplication>
-#include <stdio.h>
-#include <stdlib.h>
-
-QtMessageHandler originalHandler = nullptr;
-
-void logToFile(QtMsgType type, const QMessageLogContext &context, const QString &msg)
-{
- QString message = qFormatLogMessage(type, context, msg);
- static FILE *f = fopen("log.txt", "a");
- fprintf(f, "%s\n", qPrintable(message));
- fflush(f);
-
- if (originalHandler)
- originalHandler(type, context, msg);
-}
-
-int main(int argc, char **argv)
-{
- originalHandler = qInstallMessageHandler(logToFile);
- QApplication app(argc, argv);
- // ...
- return app.exec();
-}
-//! [23]
-#endif
-
-#include <QBrush>
void debug_info_example()
{
@@ -331,7 +272,6 @@ void warning_example()
//! [27]
}
-#include <QFile>
//! [28]
void load(const QString &fileName)
{
@@ -399,79 +339,6 @@ namespace snippet_34
//! [34]
}
-#if __has_include(<QApplication>)
-namespace snippet_35
-{
- class FriendlyConversation
- {
- public:
- QString greeting(int type);
- };
-
- QString tr(const char *)
- {
- return "";
- }
-
-
- //! [35]
- static const char *greeting_strings[] = {
- QT_TRANSLATE_NOOP("FriendlyConversation", "Hello"),
- QT_TRANSLATE_NOOP("FriendlyConversation", "Goodbye")
- };
-
- QString FriendlyConversation::greeting(int type)
- {
- return tr(greeting_strings[type]);
- }
-
- QString global_greeting(int type)
- {
- return qApp->translate("FriendlyConversation",
- greeting_strings[type]);
- }
- //! [35]
-}
-
-namespace snippet_36
-{
- class FriendlyConversation
- {
- public:
- QString greeting(int type);
- };
-
- QString tr(const char *text, const char *comment)
- {
- return "";
- }
-
-
- //! [36]
-
- static struct { const char *source; const char *comment; } greeting_strings[] =
- {
- QT_TRANSLATE_NOOP3("FriendlyConversation", "Hello",
- "A really friendly hello"),
- QT_TRANSLATE_NOOP3("FriendlyConversation", "Goodbye",
- "A really friendly goodbye")
- };
-
- QString FriendlyConversation::greeting(int type)
- {
- return tr(greeting_strings[type].source,
- greeting_strings[type].comment);
- }
-
- QString global_greeting(int type)
- {
- return qApp->translate("FriendlyConversation",
- greeting_strings[type].source,
- greeting_strings[type].comment);
- }
- //! [36]
-}
-#endif // __has_include(<QApplication>)
namespace snippet_qttrnnoop
{
@@ -516,44 +383,6 @@ QString global_greeting(int type, int msgcnt)
}
//! [qttranslatennoop]
-#if __has_include(<QApplication>)
-namespace snippet_qttranslatennoop3
-{
- class FriendlyConversation
- {
- public:
- QString greeting(int type, int count);
- };
-
- QString tr(const char *text, const char *comment, int n)
- {
- return "";
- }
-
- //! [qttranslatennoop3]
- static struct { const char * const source; const char * const comment; } status_strings[] = {
- QT_TRANSLATE_N_NOOP3("Message Status", "Hello, you have %n message(s)",
- "A login message status"),
- QT_TRANSLATE_N_NOOP3("Message status", "You have %n new message(s)",
- "A new message query status")
- };
-
- QString FriendlyConversation::greeting(int type, int count)
- {
- return tr(status_strings[type].source,
- status_strings[type].comment, count);
- }
-
- QString global_greeting(int type, int count)
- {
- return qApp->translate("Message Status",
- status_strings[type].source,
- status_strings[type].comment,
- count);
- }
- //! [qttranslatennoop3]
-}
-#endif // __has_include(<QApplication>)
void qttrid_example()
{
@@ -565,35 +394,6 @@ void qttrid_example()
//! [qttrid]
}
-#if __has_include(<QWidget>)
-namespace qttrid_noop
-{
- class TheClass : public QWidget
- {
- public:
- TheClass(QWidget *parent = nullptr) : QWidget(parent) {
- addLabels();
- }
- void addLabels();
- };
-
- //! [qttrid_noop]
- static const char * const ids[] = {
- //% "This is the first text."
- QT_TRID_NOOP("qtn_1st_text"),
- //% "This is the second text."
- QT_TRID_NOOP("qtn_2nd_text"),
- 0
- };
-
- void TheClass::addLabels()
- {
- for (int i = 0; ids[i]; ++i)
- new QLabel(qtTrId(ids[i]), this);
- }
- //! [qttrid_noop]
-}
-#endif
namespace qttrid_n_noop
{
@@ -613,18 +413,18 @@ namespace qttrid_n_noop
//! [qttrid_n_noop]
}
-namespace for_struct
+QT_BEGIN_NAMESPACE
+
+//! [38]
+struct Point3D
{
- //! [38]
- struct Point2D
- {
- int x;
- int y;
- };
+ int x;
+ int y;
+ int z;
+};
- Q_DECLARE_TYPEINFO(Point2D, Q_PRIMITIVE_TYPE);
- //! [38]
-}
+Q_DECLARE_TYPEINFO(Point3D, Q_PRIMITIVE_TYPE);
+//! [38]
//! [39]
class Point2D
@@ -706,14 +506,6 @@ private:
};
//! [44]
-#if __has_include(<QWidget>)
-void qwidget_example()
-{
- //! [45]
- QWidget w = QWidget();
- //! [45]
-}
-#endif
void qfuzzycompare_example()
{
@@ -743,6 +535,8 @@ struct A : public B {
template<> class QTypeInfo<A> : public QTypeInfoMerger<A, B, C, D> {};
//! [51]
+QT_END_NAMESPACE
+
namespace snippet_52
{
//! [52]
@@ -828,17 +622,6 @@ int qunreachable_example(Shapes shape)
return -1;
}
-# if __has_include(<QWidget>)
-//! [qt-version-check]
-#include <QtGlobal>
-
-#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
-#include <QtWidgets>
-#else
-#include <QtGui>
-#endif
-//! [qt-version-check]
-#endif
void qgetenv_examples()
{
diff --git a/src/corelib/doc/snippets/code/src_corelib_global_qglobal_widgets.cpp b/src/corelib/doc/snippets/code/src_corelib_global_qglobal_widgets.cpp
new file mode 100644
index 00000000000..3cc6ae928e6
--- /dev/null
+++ b/src/corelib/doc/snippets/code/src_corelib_global_qglobal_widgets.cpp
@@ -0,0 +1,208 @@
+// Copyright (C) 2025 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+#include <QLabel>
+#include <QStyle>
+
+void label_example()
+{
+ QLabel *label = new QLabel;
+ //! [0]
+ label->setAlignment(Qt::AlignLeft | Qt::AlignTop);
+ label->setAlignment({ });
+ //! [0]
+}
+
+QStyle *style()
+{
+ static QStyle *s = nullptr;
+ return s;
+}
+
+void snippet_16()
+{
+ //! [1]
+ #if QT_VERSION >= QT_VERSION_CHECK(4, 1, 0)
+ QIcon icon = style()->standardIcon(QStyle::SP_TrashIcon);
+ #else
+ QPixmap pixmap = style()->standardPixmap(QStyle::SP_TrashIcon);
+ QIcon icon(pixmap);
+ #endif
+ //! [1]
+}
+
+//! [2]
+#include <QApplication>
+#include <stdio.h>
+#include <stdlib.h>
+
+QtMessageHandler originalHandler = nullptr;
+
+void logToFile(QtMsgType type, const QMessageLogContext &context, const QString &msg)
+{
+ QString message = qFormatLogMessage(type, context, msg);
+ static FILE *f = fopen("log.txt", "a");
+ fprintf(f, "%s\n", qPrintable(message));
+ fflush(f);
+
+ if (originalHandler)
+ originalHandler(type, context, msg);
+}
+
+int main(int argc, char **argv)
+{
+ originalHandler = qInstallMessageHandler(logToFile);
+ QApplication app(argc, argv);
+ // ...
+ return app.exec();
+}
+//! [2]
+
+class FriendlyConversation
+{
+public:
+ QString greeting(int type);
+};
+
+QString tr(const char *)
+{
+ return "";
+}
+
+
+//! [3]
+static const char *greeting_strings[] = {
+ QT_TRANSLATE_NOOP("FriendlyConversation", "Hello"),
+ QT_TRANSLATE_NOOP("FriendlyConversation", "Goodbye")
+};
+
+QString FriendlyConversation::greeting(int type)
+{
+ return tr(greeting_strings[type]);
+}
+
+QString global_greeting(int type)
+{
+ return qApp->translate("FriendlyConversation",
+ greeting_strings[type]);
+}
+//! [3]
+
+
+namespace repetition
+{
+ class FriendlyConversation
+ {
+ public:
+ QString greeting(int type);
+ };
+
+ QString tr(const char *text, const char *comment)
+ {
+ return "";
+ }
+
+
+ //! [4]
+
+ static struct { const char *source; const char *comment; } greeting_strings[] =
+ {
+ QT_TRANSLATE_NOOP3("FriendlyConversation", "Hello",
+ "A really friendly hello"),
+ QT_TRANSLATE_NOOP3("FriendlyConversation", "Goodbye",
+ "A really friendly goodbye")
+ };
+
+ QString FriendlyConversation::greeting(int type)
+ {
+ return tr(greeting_strings[type].source,
+ greeting_strings[type].comment);
+ }
+
+ QString global_greeting(int type)
+ {
+ return qApp->translate("FriendlyConversation",
+ greeting_strings[type].source,
+ greeting_strings[type].comment);
+ }
+ //! [4]
+}
+
+namespace snippet_qttranslatennoop3
+{
+ class FriendlyConversation
+ {
+ public:
+ QString greeting(int type, int count);
+ };
+
+ QString tr(const char *text, const char *comment, int n)
+ {
+ return "";
+ }
+
+ //! [qttranslatennoop]
+ static struct { const char * const source; const char * const comment; } status_strings[] = {
+ QT_TRANSLATE_N_NOOP3("Message Status", "Hello, you have %n message(s)",
+ "A login message status"),
+ QT_TRANSLATE_N_NOOP3("Message status", "You have %n new message(s)",
+ "A new message query status")
+ };
+
+ QString FriendlyConversation::greeting(int type, int count)
+ {
+ return tr(status_strings[type].source,
+ status_strings[type].comment, count);
+ }
+
+ QString global_greeting(int type, int count)
+ {
+ return qApp->translate("Message Status",
+ status_strings[type].source,
+ status_strings[type].comment,
+ count);
+ }
+ //! [qttranslatennoop]
+}
+
+class TheClass : public QWidget
+{
+ public:
+ TheClass(QWidget *parent = nullptr) : QWidget(parent) {
+ addLabels();
+ }
+ void addLabels();
+};
+
+//! [qttrid_noop]
+static const char * const ids[] = {
+ //% "This is the first text."
+ QT_TRID_NOOP("qtn_1st_text"),
+ //% "This is the second text."
+ QT_TRID_NOOP("qtn_2nd_text"),
+ 0
+};
+
+void TheClass::addLabels()
+{
+ for (int i = 0; ids[i]; ++i)
+ new QLabel(qtTrId(ids[i]), this);
+}
+//! [qttrid_noop]
+
+void qwidget_example()
+{
+ //! [5]
+ QWidget w = QWidget();
+ //! [5]
+}
+
+//! [qt-version-check]
+#include <QtGlobal>
+
+#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
+#include <QtWidgets>
+#else
+#include <QtGui>
+#endif
+//! [qt-version-check]
diff --git a/src/corelib/global/qflags.qdoc b/src/corelib/global/qflags.qdoc
index 5f7e68cdcff..2c13aa16639 100644
--- a/src/corelib/global/qflags.qdoc
+++ b/src/corelib/global/qflags.qdoc
@@ -84,7 +84,7 @@
Qt::Alignment parameter, which means that any combination of
Qt::AlignmentFlag values, or \c{{ }}, is legal:
- \snippet code/src_corelib_global_qglobal.cpp 0
+ \snippet code/src_corelib_global_qglobal_widgets.cpp 0
If you try to pass a value from another enum or just a plain
integer other than 0, the compiler will report an error. If you
diff --git a/src/corelib/global/qlogging.cpp b/src/corelib/global/qlogging.cpp
index 55dadf4d6f9..8d0509795a0 100644
--- a/src/corelib/global/qlogging.cpp
+++ b/src/corelib/global/qlogging.cpp
@@ -2272,7 +2272,7 @@ void qErrnoWarning(int code, const char *msg, ...)
Here is an example of a message handler that logs to a local file
before calling the default handler:
- \snippet code/src_corelib_global_qglobal.cpp 23
+ \snippet code/src_corelib_global_qglobal_widgets.cpp 2
Note that the C++ standard guarantees that \c{static FILE *f} is
initialized in a thread-safe way. We can also expect \c{fprintf()}
diff --git a/src/corelib/global/qtclasshelpermacros.qdoc b/src/corelib/global/qtclasshelpermacros.qdoc
index 13745ad393a..3a2426f0174 100644
--- a/src/corelib/global/qtclasshelpermacros.qdoc
+++ b/src/corelib/global/qtclasshelpermacros.qdoc
@@ -32,7 +32,7 @@
But even this might not catch absolutely every case. You might be
tempted to do something like this:
- \snippet code/src_corelib_global_qglobal.cpp 45
+ \snippet code/src_corelib_global_qglobal_widgets.cpp 5
First of all, don't do that. Most compilers will generate code that
uses the copy constructor, so the privacy violation error will be
diff --git a/src/corelib/global/qttranslation.qdoc b/src/corelib/global/qttranslation.qdoc
index b2b0b1f3c3c..622c5916e4b 100644
--- a/src/corelib/global/qttranslation.qdoc
+++ b/src/corelib/global/qttranslation.qdoc
@@ -34,7 +34,7 @@
Example:
- \snippet code/src_corelib_global_qglobal.cpp 35
+ \snippet code/src_corelib_global_qglobal_widgets.cpp 3
\sa QT_TR_NOOP(), QT_TRANSLATE_NOOP3(), {Internationalization with Qt}
*/
@@ -56,7 +56,7 @@
Example:
- \snippet code/src_corelib_global_qglobal.cpp 36
+ \snippet code/src_corelib_global_qglobal_widgets.cpp 4
\sa QT_TR_NOOP(), QT_TRANSLATE_NOOP(), {Internationalization with Qt}
*/
@@ -120,7 +120,7 @@
Example:
- \snippet code/src_corelib_global_qglobal.cpp qttranslatennoop3
+ \snippet code/src_corelib_global_qglobal_widgets.cpp qttranslatennoop
\sa QT_TR_NOOP(), QT_TRANSLATE_NOOP(), QT_TRANSLATE_NOOP3(),
{Internationalization with Qt}
@@ -180,7 +180,7 @@
Example:
- \snippet code/src_corelib_global_qglobal.cpp qttrid_noop
+ \snippet code/src_corelib_global_qglobal_widgets.cpp qttrid_noop
\sa qtTrId(), {Internationalization with Qt}
*/
diff --git a/src/corelib/global/qtversionchecks.cpp b/src/corelib/global/qtversionchecks.cpp
index 3e3f4547f11..807b350ed9b 100644
--- a/src/corelib/global/qtversionchecks.cpp
+++ b/src/corelib/global/qtversionchecks.cpp
@@ -13,7 +13,7 @@
Example:
- \snippet code/src_corelib_global_qglobal.cpp qt-version-check
+ \snippet code/src_corelib_global_qglobal_widgets.cpp qt-version-check
\note the parameters are read as integers in the normal way, so should
normally be written in decimal (so a \c 0x prefix must be used if writing
@@ -40,7 +40,7 @@
Example:
- \snippet code/src_corelib_global_qglobal.cpp 16
+ \snippet code/src_corelib_global_qglobal_widgets.cpp 1
\sa QT_VERSION_STR, QT_VERSION_CHECK(), qVersion()
*/