diff options
author | Oleksii Zbykovskyi <[email protected]> | 2025-06-11 14:49:19 +0200 |
---|---|---|
committer | Oleksii Zbykovskyi <[email protected]> | 2025-06-26 14:27:35 +0200 |
commit | 6a49c7f19f514056a7029de6ae04b424e5affc5a (patch) | |
tree | 14f518d06a67ac854089c5937b0d10933921e0f3 | |
parent | 635f79f16300ed4b53a6665c81d0a37a2de9d78e (diff) |
Add more snippets sources from corelib to the build system
Trying to compile snippets and fixing some bugs with them.
Added some files and targets to CMakeList.txt.
Added SOURCES of files that triggers Widgets in CMakeList.txt.
Handled "no widgets" and "no qml" cases for snippets compiling.
Relocated the necessary header file for qtcast.
All others are just small fixes.
Update: fixed path to the qlogging snippet in docs
Task-number: QTBUG-137566
Change-Id: I6c6068790bf24ca88072cf8ca5c33b1401551452
Reviewed-by: Volker Hilsheimer <[email protected]>
19 files changed, 135 insertions, 122 deletions
diff --git a/src/corelib/doc/snippets/CMakeLists.txt b/src/corelib/doc/snippets/CMakeLists.txt index e52c36ae8da..10bb0b5a444 100644 --- a/src/corelib/doc/snippets/CMakeLists.txt +++ b/src/corelib/doc/snippets/CMakeLists.txt @@ -2,7 +2,29 @@ # SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause add_library(corelib_snippets OBJECT + customtype/customtypeexample.cpp + file/file.cpp + jni/src_qjniobject.cpp + process/process.cpp + qbytearraylist/main.cpp + qdir-listfiles/main.cpp + qdir-namefilters/main.cpp + qelapsedtimer/main.cpp + qloggingcategory/main.cpp + qmessageauthenticationcode/main.cpp + qmetatype/registerConverters.cpp + qprocess-environment/main.cpp qrangemodel/main.cpp + qstack/main.cpp + qstringlist/main.cpp + qstringlistmodel/main.cpp + qversionnumber/main.cpp + qxmlstreamwriter/main.cpp + settings/settings.cpp + sharedemployee/main.cpp + signalsandslots/signalsandslots.cpp + streaming/main.cpp + threads/threads.cpp ) target_link_libraries(corelib_snippets PRIVATE @@ -12,8 +34,38 @@ target_link_libraries(corelib_snippets PRIVATE qt_internal_extend_target(corelib_snippets CONDITION QT_FEATURE_widgets LIBRARIES Qt::Widgets + SOURCES + events/events.cpp + hellotrmain.cpp + fileinfo/main.cpp + pointer/pointer.cpp + qsortfilterproxymodel-details/main.cpp + qstring/main.cpp + qtcast/qtcast.cpp + settings/settings.cpp ) +qt_internal_extend_target(corelib_snippets CONDITION QT_FEATURE_gui + LIBRARIES + Qt::Gui + SOURCES + buffer/buffer.cpp + qdebug/qdebugsnippet.cpp +) + +find_package(Qt6 OPTIONAL_COMPONENTS Qml) + +if(Qt6Qml_FOUND) + qt_internal_extend_target(corelib_snippets + LIBRARIES + Qt::Qml + SOURCES + qlogging/qlogging.cpp + ) +else() + message("Qml was not found") +endif() + if ("${CMAKE_CXX_COMPILE_FEATURES}" MATCHES "cxx_std_23") set_property(TARGET corelib_snippets PROPERTY CXX_STANDARD 23) endif() diff --git a/src/corelib/doc/snippets/buffer/buffer.cpp b/src/corelib/doc/snippets/buffer/buffer.cpp index 160c0327c32..ba40eba2c2f 100644 --- a/src/corelib/doc/snippets/buffer/buffer.cpp +++ b/src/corelib/doc/snippets/buffer/buffer.cpp @@ -1,7 +1,6 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <QApplication> #include <QBuffer> #include <QPalette> @@ -23,13 +22,14 @@ static void main_snippet() static void write_datastream_snippets() { + QPalette palette; //! [1] QByteArray byteArray; QBuffer buffer(&byteArray); buffer.open(QIODevice::WriteOnly); QDataStream out(&buffer); - out << QApplication::palette(); + out << palette; //! [1] } @@ -73,15 +73,3 @@ static void setBuffer_snippet() // byteArray == "abcdef" //! [4] } - -int main(int argc, char *argv[]) -{ - QApplication app(argc, argv); - - main_snippet(); - bytearray_ptr_ctor_snippet(); - write_datastream_snippets(); - read_datastream_snippets(); - setBuffer_snippet(); - return 0; -} diff --git a/src/corelib/doc/snippets/events/events.cpp b/src/corelib/doc/snippets/events/events.cpp index faeb168d803..09bd1d299c9 100644 --- a/src/corelib/doc/snippets/events/events.cpp +++ b/src/corelib/doc/snippets/events/events.cpp @@ -54,7 +54,3 @@ bool MyWidget::event(QEvent *event) return QWidget::event(event); } //! [1] - -int main() -{ -} diff --git a/src/corelib/doc/snippets/file/file.cpp b/src/corelib/doc/snippets/file/file.cpp index d1244f4bf24..3fc03a0064c 100644 --- a/src/corelib/doc/snippets/file/file.cpp +++ b/src/corelib/doc/snippets/file/file.cpp @@ -78,9 +78,3 @@ static void readRegularEmptyFile_snippet() } //! [3] } - -int main() -{ - lineByLine_snippet(); - writeStream_snippet(); -} diff --git a/src/corelib/doc/snippets/hellotrmain.cpp b/src/corelib/doc/snippets/hellotrmain.cpp index 6f08a9761f2..5d6d71ea8f9 100644 --- a/src/corelib/doc/snippets/hellotrmain.cpp +++ b/src/corelib/doc/snippets/hellotrmain.cpp @@ -1,6 +1,9 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +#include <QApplication> +#include <QTranslator> +#include <QPushButton> //! [0] // Required for using the '_L1' string literal. using namespace Qt::StringLiterals; @@ -21,4 +24,3 @@ int main(int argc, char *argv[]) return app.exec(); } //! [0] - diff --git a/src/corelib/doc/snippets/jni/src_qjniobject.cpp b/src/corelib/doc/snippets/jni/src_qjniobject.cpp index 6e66b51383e..33221405220 100644 --- a/src/corelib/doc/snippets/jni/src_qjniobject.cpp +++ b/src/corelib/doc/snippets/jni/src_qjniobject.cpp @@ -1,6 +1,8 @@ // Copyright (C) 2021 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +#include <QtCore/qglobal.h> +#if defined(Q_QDOC) || defined(Q_OS_ANDROID) //! [QJniObject scope] void functionScope() { @@ -61,3 +63,5 @@ private static native void callNativeTwo(int x); } //! [Java native methods] + +#endif diff --git a/src/corelib/doc/snippets/qdir-listfiles/main.cpp b/src/corelib/doc/snippets/qdir-listfiles/main.cpp index 236b32c73d0..d149a72e93a 100644 --- a/src/corelib/doc/snippets/qdir-listfiles/main.cpp +++ b/src/corelib/doc/snippets/qdir-listfiles/main.cpp @@ -4,6 +4,8 @@ //! [0] #include <QDir> #include <iostream> +#include <QCoreApplication> + int main(int argc, char *argv[]) { diff --git a/src/corelib/doc/snippets/code/qlogging/qlogging.cpp b/src/corelib/doc/snippets/qlogging/qlogging.cpp index e61eb70df30..e4c906884f3 100644 --- a/src/corelib/doc/snippets/code/qlogging/qlogging.cpp +++ b/src/corelib/doc/snippets/qlogging/qlogging.cpp @@ -3,7 +3,6 @@ #undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses -#include <QtGui> #include <QtDebug> #include <QQmlComponent> diff --git a/src/corelib/doc/snippets/qmetatype/registerConverters.cpp b/src/corelib/doc/snippets/qmetatype/registerConverters.cpp index f53d04b7a60..dfe802ae1c2 100644 --- a/src/corelib/doc/snippets/qmetatype/registerConverters.cpp +++ b/src/corelib/doc/snippets/qmetatype/registerConverters.cpp @@ -5,6 +5,8 @@ #include <QMetaType> #include <QString> +using namespace Qt::Literals::StringLiterals; + int main() { //! [member] struct Coordinates { @@ -47,16 +49,22 @@ int main() { struct CustomStringType { const char *data() const {return nullptr;} }; + + struct CustomPointType{ + double x; + double y; + }; + //! [unaryfunc] QMetaType::registerConverter<CustomStringType, QString>([](const CustomStringType &str) { return QString::fromUtf8(str.data()); }); - QMetaType::registerConverter<QJsonValue, QPointF>( - [](const QJsonValue &value) -> std::optional<QPointF> { + QMetaType::registerConverter<QJsonValue, CustomPointType>( + [](const QJsonValue &value) -> std::optional<CustomPointType> { const auto object = value.toObject(); if (!object.contains("x") || !object.contains("y")) return std::nullopt; // The conversion fails if the required properties are missing - return QPointF{object["x"].toDouble(), object["y"].toDouble()}; + return CustomPointType{object["x"].toDouble(), object["y"].toDouble()}; }); //! [unaryfunc] } diff --git a/src/corelib/doc/snippets/qsortfilterproxymodel-details/main.cpp b/src/corelib/doc/snippets/qsortfilterproxymodel-details/main.cpp index 962bece2076..52b5749ac0a 100644 --- a/src/corelib/doc/snippets/qsortfilterproxymodel-details/main.cpp +++ b/src/corelib/doc/snippets/qsortfilterproxymodel-details/main.cpp @@ -4,6 +4,8 @@ #include <QtGui> #include <QApplication> #include <QSortFilterProxyModel> +#include <QWidget> +#include <QTreeView> class MyItemModel : public QStandardItemModel { @@ -47,15 +49,7 @@ Widget::Widget(QWidget *parent) //! [4] proxyModel->sort(2, Qt::AscendingOrder); //! [4] //! [5] - proxyModel->setFilterRegularExpression(QRegularExpression("\.png", QRegularExpression::CaseInsensitiveOption)); + proxyModel->setFilterRegularExpression(QRegularExpression("\\.png", QRegularExpression::CaseInsensitiveOption)); proxyModel->setFilterKeyColumn(1); //! [5] } - -int main(int argc, char *argv[]) -{ - QApplication app(argc, argv); - Widget widget; - widget.show(); - return app.exec(); -} diff --git a/src/corelib/doc/snippets/qstack/main.cpp b/src/corelib/doc/snippets/qstack/main.cpp index f1f8e6a0fda..1380dbaf9f1 100644 --- a/src/corelib/doc/snippets/qstack/main.cpp +++ b/src/corelib/doc/snippets/qstack/main.cpp @@ -1,7 +1,7 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <QtGui> +#include <QtCore> #include <iostream> using namespace std; diff --git a/src/corelib/doc/snippets/qstring/main.cpp b/src/corelib/doc/snippets/qstring/main.cpp index 578c7ae53aa..736ce52b256 100644 --- a/src/corelib/doc/snippets/qstring/main.cpp +++ b/src/corelib/doc/snippets/qstring/main.cpp @@ -2,9 +2,11 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include <QtGui> -#include <QApplication> +#include <QWidget> #include <stdio.h> +using namespace Qt::Literals::StringLiterals; + class Widget : public QWidget { public: @@ -95,7 +97,7 @@ void Widget::constCharPointer() void Widget::constCharArray() { //! [1] - static const QChar data[4] = { 0x0055, 0x006e, 0x10e3, 0x03a3 }; + static const QChar data[4] = {QChar(0x0055), QChar(0x006e), QChar(0x10e3), QChar(0x03a3) }; QString str(data, 4); //! [1] } @@ -285,9 +287,9 @@ void Widget::compareSensitiveFunction() //! [16] //! [QtPrivate::compareStrings-QSV-QSV] - int x = QtPrivate::compareStrings(u"aUtO", u"AuTo", Qt::CaseInsensitive); // x == 0 - int y = QtPrivate::compareStrings(u"auto", u"Car", Qt::CaseSensitive); // y > 0 - int z = QtPrivate::compareStrings(u"auto", u"Car", Qt::CaseInsensitive); // z < 0 + int a = QtPrivate::compareStrings(u"aUtO", u"AuTo", Qt::CaseInsensitive); // a == 0 + int b = QtPrivate::compareStrings(u"auto", u"Car", Qt::CaseSensitive); // b > 0 + int c = QtPrivate::compareStrings(u"auto", u"Car", Qt::CaseInsensitive); // c < 0 //! [QtPrivate::compareStrings-QSV-QSV] } @@ -349,7 +351,7 @@ void Widget::fromRawDataFunction() 0x1009, 0x0020, 0x0020}; QString str = QString::fromRawData(unicode, std::size(unicode)); - if (str.contains(pattern) { + if (str.contains(pattern)) { // ... //! [22] //! [23] } @@ -374,7 +376,6 @@ void Widget::firstIndexOfFunction() QString str = "the minimum"; str.indexOf(QRegularExpression("m[aeiou]"), 0); // returns 4 - QString str = "the minimum"; QRegularExpressionMatch match; str.indexOf(QRegularExpression("m[aeiou]"), 0, &match); // returns 4 // match.captured() == mi @@ -424,7 +425,6 @@ void Widget::lastIndexOfFunction() QString str = "the minimum"; str.lastIndexOf(QRegularExpression("m[aeiou]")); // returns 8 - QString str = "the minimum"; QRegularExpressionMatch match; str.lastIndexOf(QRegularExpression("m[aeiou]"), -1, &match); // returns 8 // match.captured() == mu @@ -565,7 +565,7 @@ void Widget::resizeFunction() //! [46] QString t = "Hello"; - r.resize(t.size() + 10, 'X'); + t.resize(t.size() + 10, 'X'); // t == "HelloXXXXXXXXXX" //! [46] @@ -685,6 +685,7 @@ void Widget::splitFunction() void Widget::splitCaseSensitiveFunction() { + { //! [62] QString str = QStringLiteral("a,,b,c"); @@ -694,18 +695,23 @@ void Widget::splitCaseSensitiveFunction() QStringList list2 = str.split(u',', Qt::SkipEmptyParts); // list2: [ "a", "b", "c" ] //! [62] + } + { //! [62-empty] QString str = "abc"; auto parts = str.split(QString()); // parts: {"", "a", "b", "c", ""} //! [62-empty] + } + { //! [62-slashes] QString str = "/a/b/c/"; auto parts = str.split(u'/'); // parts: {"", "a", "b", "c", ""} //! [62-slashes] + } } void Widget::sprintfFunction() @@ -917,12 +923,3 @@ void Widget::sliceFunction() x.slice(4, 3); // x == "app" //! [slice97] } - - -int main(int argc, char *argv[]) -{ - QApplication app(argc, argv); - Widget widget; - widget.show(); - return app.exec(); -} diff --git a/src/corelib/doc/snippets/qstringlist/main.cpp b/src/corelib/doc/snippets/qstringlist/main.cpp index 1791faf5116..9e9e0441457 100644 --- a/src/corelib/doc/snippets/qstringlist/main.cpp +++ b/src/corelib/doc/snippets/qstringlist/main.cpp @@ -1,19 +1,12 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <QtGui> -#include <iostream> +#include <QtCore> + using namespace std; using namespace Qt::StringLiterals; -class Widget : public QWidget -{ -public: - Widget(QWidget *parent = nullptr); -}; - -Widget::Widget(QWidget *parent) - : QWidget(parent) +void examples() { //! [0a] QStringList fonts = { "Arial", "Helvetica", "Times" }; @@ -110,11 +103,3 @@ Widget::Widget(QWidget *parent) //! [19] } } - -int main(int argc, char *argv[]) -{ - QApplication app(argc, argv); - Widget widget; - widget.show(); - return app.exec(); -} diff --git a/src/corelib/doc/snippets/qstringlistmodel/main.cpp b/src/corelib/doc/snippets/qstringlistmodel/main.cpp index b0d14e110e8..aba81619af2 100644 --- a/src/corelib/doc/snippets/qstringlistmodel/main.cpp +++ b/src/corelib/doc/snippets/qstringlistmodel/main.cpp @@ -1,16 +1,9 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <QtGui> +#include <QtCore> -class Widget : public QWidget -{ -public: - Widget(QWidget *parent = nullptr); -}; - -Widget::Widget(QWidget *parent) - : QWidget(parent) +void example() { //! [0] QStringListModel *model = new QStringListModel(); @@ -19,11 +12,3 @@ Widget::Widget(QWidget *parent) model->setStringList(list); //! [0] } - -int main(int argc, char *argv[]) -{ - QApplication app(argc, argv); - Widget widget; - widget.show(); - return app.exec(); -} diff --git a/src/corelib/doc/snippets/qtcast/qtcast.cpp b/src/corelib/doc/snippets/qtcast/qtcast.cpp index 177446aff9c..cf7f722c30f 100644 --- a/src/corelib/doc/snippets/qtcast/qtcast.cpp +++ b/src/corelib/doc/snippets/qtcast/qtcast.cpp @@ -23,7 +23,7 @@ MyWidget::MyWidget() //! [3] QLabel *label = qobject_cast<QLabel *>(obj); //! [3] //! [4] - // label is 0 + // label is nullptr //! [4] //! [5] @@ -35,8 +35,3 @@ MyWidget::MyWidget() } //! [6] } - -int main() -{ - return 0; -} diff --git a/src/corelib/doc/snippets/qtcast/qtcast.h b/src/corelib/doc/snippets/qtcast/qtcast.h new file mode 100644 index 00000000000..faee1cdc09f --- /dev/null +++ b/src/corelib/doc/snippets/qtcast/qtcast.h @@ -0,0 +1,17 @@ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#ifndef QTCAST_H +#define QTCAST_H + +#include <QWidget> + +class MyWidget : public QWidget +{ + Q_OBJECT + +public: + MyWidget(); +}; + +#endif diff --git a/src/corelib/doc/snippets/qversionnumber/main.cpp b/src/corelib/doc/snippets/qversionnumber/main.cpp index b78e14edf15..5897da54aae 100644 --- a/src/corelib/doc/snippets/qversionnumber/main.cpp +++ b/src/corelib/doc/snippets/qversionnumber/main.cpp @@ -39,16 +39,8 @@ void Object::isPrefixOf() //! [2] } -void QObject::parse() +void Object::parse() { - //! [3] - QString string("5.4.0-alpha"); - qsizetype suffixIndex; - QVersionNumber version = QVersionNumber::fromString(string, &suffixIndex); - // version is 5.4.0 - // suffixIndex is 5 - //! [3] - //! [3-latin1-1] QLatin1StringView string("5.4.0-alpha"); qsizetype suffixIndex; @@ -69,12 +61,3 @@ void Object::equivalent() // equal is false //! [4] } - -int main() -{ - Object::genericExample(); - Object::equalityExample(); - Object::isPrefixOf(); - Object::parse(); - Object::equivalent(); -} diff --git a/src/corelib/doc/snippets/settings/settings.cpp b/src/corelib/doc/snippets/settings/settings.cpp index c73b42b2a43..6f52e83fb2a 100644 --- a/src/corelib/doc/snippets/settings/settings.cpp +++ b/src/corelib/doc/snippets/settings/settings.cpp @@ -3,9 +3,6 @@ #include <QtGui> -QWidget *win; -QWidget *panel; - void snippet_ctor1() { //! [0] @@ -37,6 +34,16 @@ void snippet_ctor2() int margin = settings.value("editor/wrapMargin", 80).toInt(); //! [7] } +} + +#if __has_include(<QWidget>) +#include <QWidget> + +void snippet_ctor_widgets() +{ + QWidget *win; + QWidget *panel; + QSettings settings; //! [8] settings.setValue("mainwindow/size", win->size()); @@ -59,6 +66,7 @@ void snippet_ctor2() settings.endGroup(); //! [12] } +#endif void snippet_locations() { @@ -87,6 +95,9 @@ void snippet_locations() } } +#if __has_include(<QWidget>) +#include <QMainWindow> + class MainWindow : public QMainWindow { public: @@ -120,7 +131,7 @@ void MainWindow::readSettings() if (geometry.isEmpty()) setGeometry(200, 200, 400, 400); else - restoreGeometry(geometry) + restoreGeometry(geometry); settings.endGroup(); } //! [17] @@ -147,3 +158,4 @@ void MainWindow::closeEvent(QCloseEvent *event) } } //! [21] +#endif diff --git a/src/corelib/global/qlogging.cpp b/src/corelib/global/qlogging.cpp index 8373f63b86c..55dadf4d6f9 100644 --- a/src/corelib/global/qlogging.cpp +++ b/src/corelib/global/qlogging.cpp @@ -379,7 +379,7 @@ using namespace QtPrivate; One example of direct use is to forward errors that stem from a scripting language, e.g. QML: - \snippet code/qlogging/qlogging.cpp 1 + \snippet qlogging/qlogging.cpp 1 \sa QMessageLogContext, qDebug(), qInfo(), qWarning(), qCritical(), qFatal() */ @@ -449,7 +449,7 @@ void QMessageLogger::info(const char *msg, ...) const This is a typedef for a pointer to a function with the following signature: - \snippet code/qlogging/qlogging.cpp 2 + \snippet qlogging/qlogging.cpp 2 The \c Q_DECLARE_LOGGING_CATEGORY macro generates a function declaration with this signature, and \c Q_LOGGING_CATEGORY generates its definition. |