diff options
Diffstat (limited to 'src')
58 files changed, 1509 insertions, 1007 deletions
diff --git a/src/android/REUSE.toml b/src/android/REUSE.toml index b4734ffee84..04b82f125e9 100644 --- a/src/android/REUSE.toml +++ b/src/android/REUSE.toml @@ -1,7 +1,7 @@ version = 1 [[annotations]] -path = ["jar/build.gradle", "jar/settings.gradle", "templates/build.gradle", "templates_cmake/**"] +path = ["jar/build.gradle", "jar/settings.gradle", "templates/build.gradle", "templates_cmake/**", "templates_aar/**", "templates/**", "**.xml"] precedence = "closest" comment = "double check" SPDX-FileCopyrightText = "Copyright (C) The Qt Company Ltd." @@ -13,3 +13,9 @@ comment = "documentation" precedence = "closest" SPDX-FileCopyrightText = "Copyright (C) The Qt Company Ltd." SPDX-License-Identifier = "LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only" + +[[annotations]] +path = ["**.gitignore"] +precedence = "closest" +SPDX-FileCopyrightText = "Copyright (C) 2024 The Qt Company Ltd." +SPDX-License-Identifier = "LicenseRef-Qt-Commercial OR BSD-3-Clause" diff --git a/src/android/jar/src/org/qtproject/qt/android/QtView.java b/src/android/jar/src/org/qtproject/qt/android/QtView.java index 9d7850cb517..afe4d005402 100644 --- a/src/android/jar/src/org/qtproject/qt/android/QtView.java +++ b/src/android/jar/src/org/qtproject/qt/android/QtView.java @@ -1,6 +1,6 @@ // Copyright (C) 2024 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only -// Qt-Security score:critical reason:executing-external-code +// Qt-Security score:critical reason:execute-external-code package org.qtproject.qt.android; diff --git a/src/corelib/animation/qpropertyanimation.cpp b/src/corelib/animation/qpropertyanimation.cpp index b4c6b6ff8a6..73ce8ca02c1 100644 --- a/src/corelib/animation/qpropertyanimation.cpp +++ b/src/corelib/animation/qpropertyanimation.cpp @@ -21,7 +21,12 @@ makes it possible to animate many of Qt's widgets. Let's look at an example: - \snippet code/src_corelib_animation_qpropertyanimation.cpp 0 + \snippet code/src_corelib_animation_qpropertyanimation.cpp includes + \snippet code/src_corelib_animation_qpropertyanimation.cpp class_decl + \snippet code/src_corelib_animation_qpropertyanimation.cpp ctor_impl + \snippet code/src_corelib_animation_qpropertyanimation.cpp first_example + \snippet code/src_corelib_animation_qpropertyanimation.cpp ctor_close + \snippet code/src_corelib_animation_qpropertyanimation.cpp main \note You can also control an animation's lifespan by choosing a \l{QAbstractAnimation::DeletionPolicy}{delete policy} while starting the diff --git a/src/corelib/doc/snippets/CMakeLists.txt b/src/corelib/doc/snippets/CMakeLists.txt index 937433f2e21..78cd4f6b001 100644 --- a/src/corelib/doc/snippets/CMakeLists.txt +++ b/src/corelib/doc/snippets/CMakeLists.txt @@ -39,6 +39,7 @@ qt_internal_extend_target(corelib_snippets CONDITION QT_FEATURE_widgets hellotrmain.cpp fileinfo/main.cpp pointer/pointer.cpp + qsignalmapper/buttonwidget.cpp qsortfilterproxymodel-details/main.cpp qstring/main.cpp qtcast/qtcast.cpp @@ -53,6 +54,8 @@ qt_internal_extend_target(corelib_snippets CONDITION QT_FEATURE_gui qdebug/qdebugsnippet.cpp ) +set_target_properties(corelib_snippets PROPERTIES COMPILE_OPTIONS "-w") + 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/code/doc_src_containers.cpp b/src/corelib/doc/snippets/code/doc_src_containers.cpp index b5684624929..167437c0151 100644 --- a/src/corelib/doc/snippets/code/doc_src_containers.cpp +++ b/src/corelib/doc/snippets/code/doc_src_containers.cpp @@ -3,6 +3,8 @@ #undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses +#include <QtCore> + //! [0] class Employee { @@ -18,225 +20,225 @@ private: }; //! [0] -//! [range_for] -QList<QString> list = {"A", "B", "C", "D"}; -for (const auto &item : list) { - ... -} -//! [range_for] - -//! [range_for_as_const] -QList<QString> list = {"A", "B", "C", "D"}; -for (const auto &item : std::as_const(list)) { - ... -} -//! [range_for_as_const] - -//! [index] -QList<QString> list = {"A", "B", "C", "D"}; -for (qsizetype i = 0; i < list.size(); ++i) { - const auto &item = list.at(i); - ... -} -//! [index] - -//! [1] -QList<QString> list = {"A", "B", "C", "D"}; - -QListIterator<QString> i(list); -while (i.hasNext()) - QString s = i.next(); -//! [1] - - -//! [2] -QListIterator<QString> i(list); -i.toBack(); -while (i.hasPrevious()) - QString s = i.previous(); -//! [2] - - -//! [3] -QMutableListIterator<int> i(list); -while (i.hasNext()) { - if (i.next() % 2 != 0) - i.remove(); -} -//! [3] - - -//! [4] -QMutableListIterator<int> i(list); -i.toBack(); -while (i.hasPrevious()) { - if (i.previous() % 2 != 0) - i.remove(); -} -//! [4] - - -//! [5] -QMutableListIterator<int> i(list); -while (i.hasNext()) { - if (i.next() > 128) - i.setValue(128); -} -//! [5] - - -//! [6] -QMutableListIterator<int> i(list); -while (i.hasNext()) - i.next() *= 2; -//! [6] - - -//! [7] -QMap<QString, QString> map = { - {"Paris", "France"}, - {"Guatemala City", "Guatemala"}, - {"Mexico City", "Mexico"}, - {"Moscow", "Russia"} -}; -... - -QMutableMapIterator<QString, QString> i(map); -while (i.hasNext()) { - if (i.next().key().endsWith("City")) - i.remove(); -} -//! [7] - - -//! [8] -QMap<int, QWidget *> map; -QHash<int, QWidget *> hash; - -QMapIterator<int, QWidget *> i(map); -while (i.hasNext()) { - i.next(); - hash.insert(i.key(), i.value()); -} -//! [8] - - -//! [9] -QMutableMapIterator<int, QWidget *> i(map); -while (i.findNext(widget)) - i.remove(); -//! [9] +void examles() +{ + { + //! [range_for] + QList<QString> list = {"A", "B", "C", "D"}; + for (const auto &item : list) { + //... + } + //! [range_for] + } + { + //! [range_for_as_const] + QList<QString> list = {"A", "B", "C", "D"}; + for (const auto &item : std::as_const(list)) { + //... + } + //! [range_for_as_const] + } -//! [10] -QList<QString> list = {"A", "B", "C", "D"}; + { + //! [index] + QList<QString> list = {"A", "B", "C", "D"}; + for (qsizetype i = 0; i < list.size(); ++i) { + const auto &item = list.at(i); + //... + } + //! [index] + } -for (auto i = list.begin(), end = list.end(); i != end; ++i) - *i = (*i).toLower(); -//! [10] + { + //! [1] + QList<QString> list = {"A", "B", "C", "D"}; + QListIterator<QString> i(list); + while (i.hasNext()) + QString s = i.next(); + //! [1] + } -//! [11] -QList<QString> list = {"A", "B", "C", "D"}; + { + QList<QString> list = {"A", "B", "C", "D"}; -for (auto i = list.rbegin(), rend = list.rend(); i != rend; ++i) - *i = i->toLower(); -//! [11] + //! [2] + QListIterator<QString> i(list); + i.toBack(); + while (i.hasPrevious()) + QString s = i.previous(); + //! [2] + } + { + QList<int> list = {1, 2, 3, 4}; + { + //! [3] + QMutableListIterator<int> i(list); + while (i.hasNext()) { + if (i.next() % 2 != 0) + i.remove(); + } + //! [3] + } + + { + //! [4] + QMutableListIterator<int> i(list); + i.toBack(); + while (i.hasPrevious()) { + if (i.previous() % 2 != 0) + i.remove(); + } + //! [4] + } + + { + //! [5] + QMutableListIterator<int> i(list); + while (i.hasNext()) { + if (i.next() > 128) + i.setValue(128); + } + //! [5] + } + + { + //! [6] + QMutableListIterator<int> i(list); + while (i.hasNext()) + i.next() *= 2; + //! [6] + } + } -//! [12] -for (auto i = list.cbegin(), end = list.cend(); i != end; ++i) - qDebug() << *i; -//! [12] + { + //! [7] + QMap<QString, QString> map = { + {"Paris", "France"}, + {"Guatemala City", "Guatemala"}, + {"Mexico City", "Mexico"}, + {"Moscow", "Russia"} + }; + //... + + QMutableMapIterator<QString, QString> i(map); + while (i.hasNext()) { + if (i.next().key().endsWith("City")) + i.remove(); + } + //! [7] + } + { + //! [10] + QList<QString> list = {"A", "B", "C", "D"}; -//! [13] -QMap<int, int> map; -... -for (auto i = map.cbegin(), end = map.cend(); i != end; ++i) - qDebug() << i.key() << ':' << i.value(); -//! [13] + for (auto i = list.begin(), end = list.end(); i != end; ++i) + *i = (*i).toLower(); + //! [10] + } + { + //! [11] + QList<QString> list = {"A", "B", "C", "D"}; -//! [14] -// RIGHT -const QList<int> sizes = splitter->sizes(); -for (auto i = sizes.begin(), end = sizes.end(); i != end; ++i) - ... + for (auto i = list.rbegin(), rend = list.rend(); i != rend; ++i) + *i = i->toLower(); + //! [11] -// WRONG -for (auto i = splitter->sizes().begin(); - i != splitter->sizes().end(); ++i) - ... -//! [14] + //! [12] + for (auto i = list.cbegin(), end = list.cend(); i != end; ++i) + qDebug() << *i; + //! [12] -//! [15] -QList<QString> values; -... -QString str; -foreach (str, values) - qDebug() << str; -//! [15] + //! [13] + QMap<int, int> map; + //... + for (auto i = map.cbegin(), end = map.cend(); i != end; ++i) + qDebug() << i.key() << ':' << i.value(); + //! [13] + } -//! [16] -QList<QString> values; -... -QListIterator<QString> i(values); -while (i.hasNext()) { - QString s = i.next(); - qDebug() << s; -} -//! [16] + { + //! [15] + QList<QString> values; + //... + QString str; + foreach (str, values) + qDebug() << str; + //! [15] + } + { + //! [16] + QList<QString> values; + //... + QListIterator<QString> i(values); + while (i.hasNext()) { + QString s = i.next(); + qDebug() << s; + } + //! [16] + } -//! [17] -QList<QString> values; -... -foreach (const QString &str, values) - qDebug() << str; -//! [17] + { + //! [17] + QList<QString> values; + //... + foreach (const QString &str, values) + qDebug() << str; + //! [17] + } + { + //! [18] + QList<QString> values; + //... + foreach (const QString &str, values) { + if (str.isEmpty()) + break; + qDebug() << str; + } + //! [18] + } -//! [18] -QList<QString> values; -... -foreach (const QString &str, values) { - if (str.isEmpty()) - break; - qDebug() << str; -} -//! [18] + { + //! [19] + QMap<QString, int> map; + //... + foreach (const QString &str, map.keys()) + qDebug() << str << ':' << map.value(str); + //! [19] + } + { + //! [20] + QMultiMap<QString, int> map; + //... + foreach (const QString &str, map.uniqueKeys()) { + foreach (int i, map.values(str)) + qDebug() << str << ':' << i; + } + //! [20] + } -//! [19] -QMap<QString, int> map; -... -foreach (const QString &str, map.keys()) - qDebug() << str << ':' << map.value(str); -//! [19] + { + #if 0 + //! [22] + CONFIG += no_keywords + //! [22] -//! [20] -QMultiMap<QString, int> map; -... -foreach (const QString &str, map.uniqueKeys()) { - foreach (int i, map.values(str)) - qDebug() << str << ':' << i; + //! [cmake_no_keywords] + target_compile_definitions(my_app PRIVATE QT_NO_KEYWORDS) + //! [cmake_no_keywords] + #endif + } } -//! [20] - - -//! [22] -CONFIG += no_keywords -//! [22] - - -//! [cmake_no_keywords] -target_compile_definitions(my_app PRIVATE QT_NO_KEYWORDS) -//! [cmake_no_keywords] - //! [23] QString onlyLetters(const QString &in) @@ -250,61 +252,111 @@ QString onlyLetters(const QString &in) } //! [23] -//! [24] -QList<int> a, b; -a.resize(100000); // make a big list filled with 0. - -QList<int>::iterator i = a.begin(); -// WRONG way of using the iterator i: -b = a; -/* - Now we should be careful with iterator i since it will point to shared data - If we do *i = 4 then we would change the shared instance (both vectors) - The behavior differs from STL containers. Avoid doing such things in Qt. -*/ - -a[0] = 5; -/* - Container a is now detached from the shared data, - and even though i was an iterator from the container a, it now works as an iterator in b. - Here the situation is that (*i) == 0. -*/ - -b.clear(); // Now the iterator i is completely invalid. - -int j = *i; // Undefined behavior! -/* - The data from b (which i pointed to) is gone. - This would be well-defined with STL containers (and (*i) == 5), - but with QList this is likely to crash. -*/ -//! [24] - -//! [25] -QList<int> list = {1, 2, 3, 4, 4, 5}; -QSet<int> set(list.cbegin(), list.cend()); -/* - Will generate a QSet containing 1, 2, 3, 4, 5. -*/ -//! [25] - -//! [26] -QList<int> list = {2, 3, 1}; - -std::sort(list.begin(), list.end()); -/* - Sort the list, now contains { 1, 2, 3 } -*/ - -std::reverse(list.begin(), list.end()); -/* - Reverse the list, now contains { 3, 2, 1 } -*/ - -int even_elements = - std::count_if(list.begin(), list.end(), [](int element) { return (element % 2 == 0); }); -/* - Count how many elements that are even numbers, 1 -*/ - -//! [26] +void wrap() +{ + //! [24] + QList<int> a, b; + a.resize(100000); // make a big list filled with 0. + + QList<int>::iterator i = a.begin(); + // WRONG way of using the iterator i: + b = a; + /* + Now we should be careful with iterator i since it will point to shared data + If we do *i = 4 then we would change the shared instance (both vectors) + The behavior differs from STL containers. Avoid doing such things in Qt. + */ + + a[0] = 5; + /* + Container a is now detached from the shared data, + and even though i was an iterator from the container a, it now works as an iterator in b. + Here the situation is that (*i) == 0. + */ + + b.clear(); // Now the iterator i is completely invalid. + + int j = *i; // Undefined behavior! + /* + The data from b (which i pointed to) is gone. + This would be well-defined with STL containers (and (*i) == 5), + but with QList this is likely to crash. + */ + //! [24] + + { + //! [25] + QList<int> list = {1, 2, 3, 4, 4, 5}; + QSet<int> set(list.cbegin(), list.cend()); + /* + Will generate a QSet containing 1, 2, 3, 4, 5. + */ + //! [25] + } + + //! [26] + QList<int> list = {2, 3, 1}; + + std::sort(list.begin(), list.end()); + /* + Sort the list, now contains { 1, 2, 3 } + */ + + std::reverse(list.begin(), list.end()); + /* + Reverse the list, now contains { 3, 2, 1 } + */ + + int even_elements = + std::count_if(list.begin(), list.end(), [](int element) { return (element % 2 == 0); }); + /* + Count how many elements that are even numbers, 1 + */ + //! [26] +} + +#if __has_include(<QWidget>) + +#include <QWidget> +#include <QSplitter> + +void examples_with_widgets() +{ + { + //! [8] + QMap<int, QWidget *> map; + QHash<int, QWidget *> hash; + + QMapIterator<int, QWidget *> i(map); + while (i.hasNext()) { + i.next(); + hash.insert(i.key(), i.value()); + } + //! [8] + } + + { + QMap<int, QWidget *> map; + QWidget* widget; + //! [9] + QMutableMapIterator<int, QWidget *> i(map); + while (i.findNext(widget)) + i.remove(); + //! [9] + } + + QSplitter* splitter; + + //! [14] + // RIGHT + const QList<int> sizes = splitter->sizes(); + for (auto i = sizes.begin(), end = sizes.end(); i != end; ++i) + {/*...*/} + + // WRONG + for (auto i = splitter->sizes().begin(); + i != splitter->sizes().end(); ++i) + {/*...*/} + //! [14] +} +#endif diff --git a/src/corelib/doc/snippets/code/doc_src_groups.cpp b/src/corelib/doc/snippets/code/doc_src_groups.cpp index a0901904db0..2d7c2f96056 100644 --- a/src/corelib/doc/snippets/code/doc_src_groups.cpp +++ b/src/corelib/doc/snippets/code/doc_src_groups.cpp @@ -1,6 +1,15 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +#include <QPen> +#include <QPainter> + +class QPenPrivate { +public: + int ref = 1; + Qt::PenStyle style; +}; + //! [0] void QPen::setStyle(Qt::PenStyle style) { @@ -11,19 +20,21 @@ void QPen::setStyle(Qt::PenStyle style) void QPen::detach() { if (d->ref != 1) { - ... // perform a deep copy + //... // perform a deep copy } } //! [0] +void example() +{ + //! [1] + QPixmap p1, p2; + p1.load("image.bmp"); + p2 = p1; // p1 and p2 share data -//! [1] -QPixmap p1, p2; -p1.load("image.bmp"); -p2 = p1; // p1 and p2 share data - -QPainter paint; -paint.begin(&p2); // cuts p2 loose from p1 -paint.drawText(0,50, "Hi"); -paint.end(); -//! [1] + QPainter paint; + paint.begin(&p2); // cuts p2 loose from p1 + paint.drawText(0,50, "Hi"); + paint.end(); + //! [1] +} diff --git a/src/corelib/doc/snippets/code/doc_src_objecttrees.cpp b/src/corelib/doc/snippets/code/doc_src_objecttrees.cpp index b7c71ccb6b0..8b55ad9b157 100644 --- a/src/corelib/doc/snippets/code/doc_src_objecttrees.cpp +++ b/src/corelib/doc/snippets/code/doc_src_objecttrees.cpp @@ -1,23 +1,31 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//![0] +#include <QPushButton> + +//![open] int main() { - QWidget window; - QPushButton quit("Quit", &window); - ... -} -//![0] +//![open] + { + //![example1] + QWidget window; + QPushButton quit("Quit", &window); + //... + //![example1] + } -//![1] -int main() -{ - QPushButton quit("Quit"); - QWidget window; + { + //![example2] + QPushButton quit("Quit"); + QWidget window; + + quit.setParent(&window); + //... + //![example2] + } - quit.setParent(&window); - ... +//![close] } -//![1] +//![close] diff --git a/src/corelib/doc/snippets/code/doc_src_properties.cpp b/src/corelib/doc/snippets/code/doc_src_properties.cpp index 190a8437101..eafa7acda3b 100644 --- a/src/corelib/doc/snippets/code/doc_src_properties.cpp +++ b/src/corelib/doc/snippets/code/doc_src_properties.cpp @@ -1,6 +1,7 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +#ifdef QPROPERTY_MACRO //! [0] Q_PROPERTY(type name (READ getFunction [WRITE setFunction] | @@ -25,33 +26,40 @@ Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled) Q_PROPERTY(QCursor cursor READ cursor WRITE setCursor RESET unsetCursor) //! [1] - //! [2] Q_PROPERTY(QDate date READ getDate WRITE setDate) //! [2] +#endif +#if __has_include(<QPushButton>) +#include <QPushButton> +void button_example() +{ + //! [3] + QPushButton *button = new QPushButton; + QObject *object = button; -//! [3] -QPushButton *button = new QPushButton; -QObject *object = button; - -button->setDown(true); -object->setProperty("down", true); -//! [3] - - -//! [4] -QObject *object = ... -const QMetaObject *metaobject = object->metaObject(); -int count = metaobject->propertyCount(); -for (int i=0; i<count; ++i) { - QMetaProperty metaproperty = metaobject->property(i); - const char *name = metaproperty.name(); - QVariant value = object->property(name); - ... + button->setDown(true); + object->setProperty("down", true); + //! [3] } -//! [4] +#endif +#include <QMetaProperty> +void qobject_example() +{ + //! [4] + QObject *object = new QObject; + const QMetaObject *metaobject = object->metaObject(); + int count = metaobject->propertyCount(); + for (int i=0; i<count; ++i) { + QMetaProperty metaproperty = metaobject->property(i); + const char *name = metaproperty.name(); + QVariant value = object->property(name); + //... + } + //! [4] +} //! [5] class MyClass : public QObject @@ -85,16 +93,17 @@ private: }; //! [5] +void example(){ + //! [6] + MyClass *myinstance = new MyClass; + QObject *object = myinstance; -//! [6] -MyClass *myinstance = new MyClass; -QObject *object = myinstance; - -myinstance->setPriority(MyClass::VeryHigh); -object->setProperty("priority", "VeryHigh"); -//! [6] - + myinstance->setPriority(MyClass::VeryHigh); + object->setProperty("priority", "VeryHigh"); + //! [6] +} +#ifdef QPROPERTY_MACRO //! [7] Q_CLASSINFO("DefaultProperty", "content") //! [7] @@ -103,7 +112,7 @@ Q_CLASSINFO("DefaultProperty", "content") Q_PROPERTY(QColor color MEMBER m_color NOTIFY colorChanged) Q_PROPERTY(qreal spacing MEMBER m_spacing NOTIFY spacingChanged) Q_PROPERTY(QString text MEMBER m_text NOTIFY textChanged) - ... + //... signals: void colorChanged(); void spacingChanged(); @@ -114,4 +123,4 @@ private: qreal m_spacing; QString m_text; //! [8] - +#endif diff --git a/src/corelib/doc/snippets/code/doc_src_qalgorithms.cpp b/src/corelib/doc/snippets/code/doc_src_qalgorithms.cpp index f2b94a74165..9155345508a 100644 --- a/src/corelib/doc/snippets/code/doc_src_qalgorithms.cpp +++ b/src/corelib/doc/snippets/code/doc_src_qalgorithms.cpp @@ -1,11 +1,17 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [1] -QList<Employee *> list; -list.append(new Employee("Blackpool", "Stephen")); -list.append(new Employee("Twist", "Oliver")); +#include <QList> +#include <QColor> -qDeleteAll(list.begin(), list.end()); -list.clear(); -//! [1] +void example() +{ + //! [1] + QList<QColor *> list; + list.append(new QColor(Qt::blue)); + list.append(new QColor(Qt::yellow)); + + qDeleteAll(list.begin(), list.end()); + list.clear(); + //! [1] +} diff --git a/src/corelib/doc/snippets/code/doc_src_qcache.cpp b/src/corelib/doc/snippets/code/doc_src_qcache.cpp index a15a8027600..3157981f3fa 100644 --- a/src/corelib/doc/snippets/code/doc_src_qcache.cpp +++ b/src/corelib/doc/snippets/code/doc_src_qcache.cpp @@ -1,20 +1,34 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [0] -QCache<int, Employee> cache; -//! [0] +#include <QCache> +struct Employee { + void setId(int id); + int id() const { return 0; } + void setName(const QString &name); +}; -//! [1] -Employee *employee = new Employee; -employee->setId(37); -employee->setName("Richard Schmit"); -... -cache.insert(employee->id(), employee); -//! [1] +struct MyDataStructure { }; +void example() +{ + //! [0] + QCache<int, Employee> cache; + //! [0] -//! [2] -QCache<int, MyDataStructure> cache(5000); -//! [2] + + //! [1] + Employee *employee = new Employee; + employee->setId(37); + employee->setName("Richard Schmit"); + //... + cache.insert(employee->id(), employee); + //! [1] + + { + //! [2] + QCache<int, MyDataStructure> cache(5000); + //! [2] + } +} diff --git a/src/corelib/doc/snippets/code/doc_src_qiterator.cpp b/src/corelib/doc/snippets/code/doc_src_qiterator.cpp index 0d921b87e67..209e98af522 100644 --- a/src/corelib/doc/snippets/code/doc_src_qiterator.cpp +++ b/src/corelib/doc/snippets/code/doc_src_qiterator.cpp @@ -1,278 +1,349 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -//! [0] -QList<float> list; -... -QListIterator<float> i(list); -while (i.hasNext()) - float f = i.next(); -//! [0] - - -//! [1] -QListIterator<float> i(list); -i.toBack(); -while (i.hasPrevious()) - float f = i.previous(); -//! [1] - -//! [6] -QSet<QString> set; -... -QSetIterator<QString> i(set); -while (i.hasNext()) - float f = i.next(); -//! [6] - -//! [8] -QList<float> list; -... -QMutableListIterator<float> i(list); -while (i.hasNext()) - float f = i.next(); -//! [8] - - -//! [9] -QMutableListIterator<float> i(list); -i.toBack(); -while (i.hasPrevious()) - float f = i.previous(); -//! [9] - - -//! [10] -QMutableListIterator<int> i(list); -while (i.hasNext()) { - int val = i.next(); - if (val < 0) { - i.setValue(-val); - } else if (val == 0) { - i.remove(); +#include <QList> +#include <QSet> + +void example() +{ + { + //! [0] + QList<float> list; + //... + QListIterator<float> i(list); + while (i.hasNext()) + float f = i.next(); + //! [0] } -} -//! [10] - -//! [17] -QSet<float> set; -... -QMutableSetIterator<float> i(set); -while (i.hasNext()) - float f = i.next(); -//! [17] -//! [19] -QMutableListIterator<int> i(list); -while (i.hasNext()) { - int val = i.next(); - if (val < -32768 || val > 32767) - i.remove(); -} -//! [19] - -//! [22] -QMutableSetIterator<int> i(set); -while (i.hasNext()) { - int val = i.next(); - if (val < -32768 || val > 32767) - i.remove(); -} -//! [22] - - -//! [23] -QMutableListIterator<double> i(list); -while (i.hasNext()) { - double val = i.next(); - i.setValue(std::sqrt(val)); -} -//! [23] - -//! [26] -QMap<int, QWidget *> map; -... -QMapIterator<int, QWidget *> i(map); -while (i.hasNext()) { - i.next(); - qDebug() << i.key() << ": " << i.value(); -} -//! [26] - - -//! [27] -QMapIterator<int, QWidget *> i(map); -i.toBack(); -while (i.hasPrevious()) { - i.previous(); - qDebug() << i.key() << ": " << i.value(); -} -//! [27] - - -//! [28] -QMapIterator<int, QWidget *> i(map); -while (i.findNext(widget)) { - qDebug() << "Found widget " << widget << " under key " - << i.key(); -} -//! [28] - -//! [26multi] -QMultiMap<int, QWidget *> multimap; -... -QMultiMapIterator<int, QWidget *> i(multimap); -while (i.hasNext()) { - i.next(); - qDebug() << i.key() << ": " << i.value(); -} -//! [26multi] + QList<float> list; + { + //! [1] + QListIterator<float> i(list); + i.toBack(); + while (i.hasPrevious()) + float f = i.previous(); + //! [1] + } -//! [27multi] -QMultiMapIterator<int, QWidget *> i(multimap); -i.toBack(); -while (i.hasPrevious()) { - i.previous(); - qDebug() << i.key() << ": " << i.value(); -} -//! [27multi] + { + //! [6] + QSet<QString> set; + //... + QSetIterator<QString> i(set); + while (i.hasNext()) + QString f = i.next(); + //! [6] + } + { + //! [8] + QList<float> list; + //... + QMutableListIterator<float> i(list); + while (i.hasNext()) + float f = i.next(); + //! [8] + } -//! [28multi] -QMultiMapIterator<int, QWidget *> i(multimap); -while (i.findNext(widget)) { - qDebug() << "Found widget " << widget << " under key " - << i.key(); -} -//! [28multi] + { + //! [9] + QMutableListIterator<float> i(list); + i.toBack(); + while (i.hasPrevious()) + float f = i.previous(); + //! [9] + } + { + QList<int> list = {1, 2, 3, 4, 5}; + //! [10] + QMutableListIterator<int> i(list); + while (i.hasNext()) { + int val = i.next(); + if (val < 0) { + i.setValue(-val); + } else if (val == 0) { + i.remove(); + } + } + //! [10] + } -//! [29] -QHash<int, QWidget *> hash; -... -QHashIterator<int, QWidget *> i(hash); -while (i.hasNext()) { - i.next(); - qDebug() << i.key() << ": " << i.value(); -} -//! [29] + { + //! [17] + QSet<float> set; + //... + QMutableSetIterator<float> i(set); + while (i.hasNext()) + float f = i.next(); + //! [17] + } + { + QList<int> list = {1, 2, 3, 4, 5}; + //! [19] + QMutableListIterator<int> i(list); + while (i.hasNext()) { + int val = i.next(); + if (val < -32768 || val > 32767) + i.remove(); + } + //! [19] + } -//! [31] -QHashIterator<int, QWidget *> i(hash); -while (i.findNext(widget)) { - qDebug() << "Found widget " << widget << " under key " - << i.key(); -} -//! [31] + { + QSet<int> set; + //! [22] + QMutableSetIterator<int> i(set); + while (i.hasNext()) { + int val = i.next(); + if (val < -32768 || val > 32767) + i.remove(); + } + //! [22] + } + { + QList<double> list; + //! [23] + QMutableListIterator<double> i(list); + while (i.hasNext()) { + double val = i.next(); + i.setValue(std::sqrt(val)); + } + //! [23] + } -//! [32] -QMap<int, QWidget *> map; -... -QMutableMapIterator<int, QWidget *> i(map); -while (i.hasNext()) { - i.next(); - qDebug() << i.key() << ": " << i.value(); + { + //! [25] + QList<int> list; + //... + QListIterator<int> i(list); + while (i.hasNext()) + int val = i.next(); + //! [25] + } } -//! [32] - -//! [33] -QMutableMapIterator<int, QWidget *> i(map); -i.toBack(); -while (i.hasPrevious()) { - i.previous(); - qDebug() << i.key() << ": " << i.value(); -} -//! [33] +#if __has_include(<QWidget>) +#include <QWidget> +void example_widgets() +{ + QMap<int, QWidget *> map; + { + //! [26] + QMap<int, QWidget *> map; + //... + QMapIterator<int, QWidget *> i(map); + while (i.hasNext()) { + i.next(); + qDebug() << i.key() << ": " << i.value(); + } + //! [26] + } + { + //! [27] + QMapIterator<int, QWidget *> i(map); + i.toBack(); + while (i.hasPrevious()) { + i.previous(); + qDebug() << i.key() << ": " << i.value(); + } + //! [27] + } -//! [34] -QMutableMapIterator<int, QWidget *> i(map); -while (i.findNext(widget)) { - qDebug() << "Found widget " << widget << " under key " - << i.key(); -} -//! [34] + QWidget *widget; + { + //! [28] + QMapIterator<int, QWidget *> i(map); + while (i.findNext(widget)) { + qDebug() << "Found widget " << widget << " under key " + << i.key(); + } + //! [28] + } + { + //! [26multi] + QMultiMap<int, QWidget *> multimap; + //... + QMultiMapIterator<int, QWidget *> i(multimap); + while (i.hasNext()) { + i.next(); + qDebug() << i.key() << ": " << i.value(); + } + //! [26multi] + } -//! [35] -QMutableMapIterator<QString, QString> i(map); -while (i.hasNext()) { - i.next(); - if (i.key() == i.value()) - i.remove(); -} -//! [35] + QMultiMap<int, QWidget *> multimap; + { + //! [27multi] + QMultiMapIterator<int, QWidget *> i(multimap); + i.toBack(); + while (i.hasPrevious()) { + i.previous(); + qDebug() << i.key() << ": " << i.value(); + } + //! [27multi] + } + { + //! [28multi] + QMultiMapIterator<int, QWidget *> i(multimap); + while (i.findNext(widget)) { + qDebug() << "Found widget " << widget << " under key " + << i.key(); + } + //! [28multi] + } -//! [32multi] -QMultiMap<int, QWidget *> multimap; -... -QMutableMultiMapIterator<int, QWidget *> i(multimap); -while (i.hasNext()) { - i.next(); - qDebug() << i.key() << ": " << i.value(); -} -//! [32multi] + { + //! [29] + QHash<int, QWidget *> hash; + //... + QHashIterator<int, QWidget *> i(hash); + while (i.hasNext()) { + i.next(); + qDebug() << i.key() << ": " << i.value(); + } + //! [29] + } + QHash<int, QWidget *> hash; + { + //! [31] + QHashIterator<int, QWidget *> i(hash); + while (i.findNext(widget)) { + qDebug() << "Found widget " << widget << " under key " + << i.key(); + } + //! [31] + } -//! [33multi] -QMutableMultiMapIterator<int, QWidget *> i(multimap); -i.toBack(); -while (i.hasPrevious()) { - i.previous(); - qDebug() << i.key() << ": " << i.value(); -} -//! [33multi] + { + //! [32] + QMap<int, QWidget *> map; + //... + QMutableMapIterator<int, QWidget *> i(map); + while (i.hasNext()) { + i.next(); + qDebug() << i.key() << ": " << i.value(); + } + //! [32] + } + { + //! [33] + QMutableMapIterator<int, QWidget *> i(map); + i.toBack(); + while (i.hasPrevious()) { + i.previous(); + qDebug() << i.key() << ": " << i.value(); + } + //! [33] + } -//! [34multi] -QMutableMultiMapIterator<int, QWidget *> i(multimap); -while (i.findNext(widget)) { - qDebug() << "Found widget " << widget << " under key " - << i.key(); -} -//! [34multi] + { + //! [34] + QMutableMapIterator<int, QWidget *> i(map); + while (i.findNext(widget)) { + qDebug() << "Found widget " << widget << " under key " + << i.key(); + } + //! [34] + } + { + QMap<QString, QString> map; + //! [35] + QMutableMapIterator<QString, QString> i(map); + while (i.hasNext()) { + i.next(); + if (i.key() == i.value()) + i.remove(); + } + //! [35] + } -//! [35multi] -QMutableMultiMapIterator<QString, QString> i(multimap); -while (i.hasNext()) { - i.next(); - if (i.key() == i.value()) - i.remove(); -} -//! [35multi] + { + //! [32multi] + QMultiMap<int, QWidget *> multimap; + //... + QMutableMultiMapIterator<int, QWidget *> i(multimap); + while (i.hasNext()) { + i.next(); + qDebug() << i.key() << ": " << i.value(); + } + //! [32multi] + } + { + //! [33multi] + QMutableMultiMapIterator<int, QWidget *> i(multimap); + i.toBack(); + while (i.hasPrevious()) { + i.previous(); + qDebug() << i.key() << ": " << i.value(); + } + //! [33multi] + } -//! [36] -QHash<int, QWidget *> hash; -... -QMutableHashIterator<QString, QWidget *> i(hash); -while (i.hasNext()) { - i.next(); - qDebug() << i.key() << ": " << i.value(); -} -//! [36] + { + //! [34multi] + QMutableMultiMapIterator<int, QWidget *> i(multimap); + while (i.findNext(widget)) { + qDebug() << "Found widget " << widget << " under key " + << i.key(); + } + //! [34multi] + } + { + QMultiMap<QString, QString> multimap; + //! [35multi] + QMutableMultiMapIterator<QString, QString> i(multimap); + while (i.hasNext()) { + i.next(); + if (i.key() == i.value()) + i.remove(); + } + //! [35multi] + } -//! [38] -QMutableHashIterator<int, QWidget *> i(hash); -while (i.findNext(widget)) { - qDebug() << "Found widget " << widget << " under key " - << i.key(); -} -//! [38] + { + //! [36] + QHash<int, QWidget *> hash; + //... + QMutableHashIterator<int, QWidget *> i(hash); + while (i.hasNext()) { + i.next(); + qDebug() << i.key() << ": " << i.value(); + } + //! [36] + } + { + //! [38] + QMutableHashIterator<int, QWidget *> i(hash); + while (i.findNext(widget)) { + qDebug() << "Found widget " << widget << " under key " + << i.key(); + } + //! [38] + } -//! [39] -QMutableHashIterator<QString, QString> i(hash); -while (i.hasNext()) { - i.next(); - if (i.key() == i.value()) - i.remove(); + { + QHash<QString, QString> hash; + //! [39] + QMutableHashIterator<QString, QString> i(hash); + while (i.hasNext()) { + i.next(); + if (i.key() == i.value()) + i.remove(); + } + //! [39] + } } -//! [39] +#endif diff --git a/src/corelib/doc/snippets/code/doc_src_qnamespace.cpp b/src/corelib/doc/snippets/code/doc_src_qnamespace.cpp index 10845855840..d753874f0dd 100644 --- a/src/corelib/doc/snippets/code/doc_src_qnamespace.cpp +++ b/src/corelib/doc/snippets/code/doc_src_qnamespace.cpp @@ -1,6 +1,8 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +#include <QEvent> + //! [1] enum CustomEventPriority { diff --git a/src/corelib/doc/snippets/code/doc_src_qplugin.cpp b/src/corelib/doc/snippets/code/doc_src_qplugin.cpp index 3bca27b9661..4382d13838a 100644 --- a/src/corelib/doc/snippets/code/doc_src_qplugin.cpp +++ b/src/corelib/doc/snippets/code/doc_src_qplugin.cpp @@ -1,10 +1,12 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +#include <QtPlugin> + //! [0] namespace Foo { - struct MyInterface { ... }; + struct MyInterface { /*...*/ }; } Q_DECLARE_INTERFACE(Foo::MyInterface, "org.examples.MyInterface") diff --git a/src/corelib/doc/snippets/code/doc_src_qset.cpp b/src/corelib/doc/snippets/code/doc_src_qset.cpp index 98a6f336f5a..8342e1d6f98 100644 --- a/src/corelib/doc/snippets/code/doc_src_qset.cpp +++ b/src/corelib/doc/snippets/code/doc_src_qset.cpp @@ -1,107 +1,134 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [0] -QSet<QString> set; -//! [0] +#include <QtCore> +#include <iostream> +using namespace std; +void snippets_0_3() +{ + //! [0] + QSet<QString> set; + //! [0] -//! [1] -set.insert("one"); -set.insert("three"); -set.insert("seven"); -//! [1] + //! [1] + set.insert("one"); + set.insert("three"); + set.insert("seven"); + //! [1] -//! [2] -set << "twelve" << "fifteen" << "nineteen"; -//! [2] + //! [2] + set << "twelve" << "fifteen" << "nineteen"; + //! [2] -//! [3] -if (!set.contains("ninety-nine")) - ... -//! [3] - -//! [4] -QSetIterator<QWidget *> i(set); -while (i.hasNext()) { - QWidget *w = i.next(); - qDebug() << w; + //! [3] + if (!set.contains("ninety-nine")) + {/*...*/} + //! [3] } -//! [4] - -//! [5] -for (auto i = set.cbegin(), end = set.cend(); i != end; ++i) - qDebug() << *i; -//! [5] +#if __has_include(<QWidget>) +#include <QWidget> -//! [6] -QSet<QString> set; -... -for (const auto &value : set) - qDebug() << value; -//! [6] +void example_widgets() +{ + QSet<QWidget *> set; + //! [4] + QSetIterator<QWidget *> i(set); + while (i.hasNext()) { + QWidget *w = i.next(); + qDebug() << w; + } + //! [4] +} +#endif + +void snippets_5_12() +{ + QSet<QString> set; + + //! [5] + for (auto i = set.cbegin(), end = set.cend(); i != end; ++i) + qDebug() << *i; + //! [5] + + { + //! [6] + QSet<QString> set; + //... + for (const auto &value : set) + qDebug() << value; + //! [6] + } + { + QString values[3]; + //! [7] + QSet<QString> set; + set.reserve(20000); + for (int i = 0; i < 20000; ++i) + set.insert(values[i]); + //! [7] + } -//! [7] -QSet<QString> set; -set.reserve(20000); -for (int i = 0; i < 20000; ++i) - set.insert(values[i]); -//! [7] + { + //! [8] + QSet<QString> set = {"January", "February", /*...*/ "December"}; + // i is a QSet<QString>::iterator + for (auto i = set.begin(), end = set.end(); i != end; ++i) + qDebug() << *i; + //! [8] + } -//! [8] -QSet<QString> set = {"January", "February", ... "December"} + { + //! [9] + QSet<QString> set = {"January", "February", /*...*/ "December"}; + + auto i = set.begin(); + while (i != set.end()) { + if ((*i).startsWith('J')) { + i = set.erase(i); + } else { + ++i; + } + } + //! [9] + } -// i is a QSet<QString>::iterator -for (auto i = set.begin(), end = set.end(); i != end; ++i) - qDebug() << *i; -//! [8] + { + //! [10] + QSet<QString> set; + //... + const auto predicate = [](const QString &s) { return s.compare("Jeanette", Qt::CaseInsensitive) == 0; }; + QSet<QString>::iterator it = std::find_if(set.begin(), set.end(), predicate); + if (it != set.end()) + cout << "Found Jeanette" << endl; + //! [10] + } + { + //! [11] + QSet<QString> set = {"January", "February", /*...*/ "December"}; -//! [9] -QSet<QString> set = {"January", "February", ... "December"}; + // i is QSet<QString>::const_iterator + for (auto i = set.cbegin(), end = set.cend(); i != end; ++i) + qDebug() << *i; + //! [11] + } -auto i = set.begin(); -while (i != set.end()) { - if ((*i).startsWith('J')) { - i = set.erase(i); - } else { - ++i; + { + //! [12] + QSet<QString> set; + //... + const auto predicate = [](const QString &s) { return s.compare("Jeanette", Qt::CaseInsensitive) == 0; }; + QSet<QString>::const_iterator it = std::find_if(set.cbegin(), set.cend(), predicate); + if (it != set.constEnd()) + cout << "Found Jeanette" << endl; + //! [12] } } -//! [9] - - -//! [10] -QSet<QString> set; -... -const auto predicate = [](const QString &s) { return s.compare("Jeanette", Qt::CaseInsensitive) == 0; }; -QSet<QString>::iterator it = std::find_if(set.begin(), set.end(), predicate); -if (it != set.end()) - cout << "Found Jeanette" << endl; -//! [10] - - -//! [11] -QSet<QString> set = {"January", "February", ... "December"}; - -// i is QSet<QString>::const_iterator -for (auto i = set.cbegin(), end = set.cend(); i != end; ++i) - qDebug() << *i; -//! [11] - - -//! [12] -QSet<QString> set; -... -const auto predicate = [](const QString &s) { return s.compare("Jeanette", Qt::CaseInsensitive) == 0; }; -QSet<QString>::const_iterator it = std::find_if(set.cbegin(), set.cend(), predicate); -if (it != set.constEnd()) - cout << "Found Jeanette" << endl; -//! [12] diff --git a/src/corelib/doc/snippets/code/doc_src_qvarlengtharray.cpp b/src/corelib/doc/snippets/code/doc_src_qvarlengtharray.cpp index 080874fc77d..4d2ccc82c2e 100644 --- a/src/corelib/doc/snippets/code/doc_src_qvarlengtharray.cpp +++ b/src/corelib/doc/snippets/code/doc_src_qvarlengtharray.cpp @@ -1,21 +1,24 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +#include <QVarLengthArray> + +#if 0 //! [0] -int myfunc(int n) +int myfunc_wrong(int n) { int table[n + 1]; // WRONG - ... + //... return table[n]; } //! [0] - +#endif //! [1] -int myfunc(int n) +int myfunc_correct(int n) { int *table = new int[n + 1]; - ... + //... int ret = table[n]; delete[] table; return ret; @@ -24,18 +27,21 @@ int myfunc(int n) //! [2] -int myfunc(int n) +int myfunc_q(int n) { QVarLengthArray<int, 1024> array(n + 1); - ... + //... return array[n]; } //! [2] -//! [3] -QVarLengthArray<int> array(10); -int *data = array.data(); -for (int i = 0; i < 10; ++i) - data[i] = 2 * i; -//! [3] +void example() +{ + //! [3] + QVarLengthArray<int> array(10); + int *data = array.data(); + for (int i = 0; i < 10; ++i) + data[i] = 2 * i; + //! [3] +} diff --git a/src/corelib/doc/snippets/code/doc_src_resources.cpp b/src/corelib/doc/snippets/code/doc_src_resources.cpp index 04ecf810ec1..9a244b9239d 100644 --- a/src/corelib/doc/snippets/code/doc_src_resources.cpp +++ b/src/corelib/doc/snippets/code/doc_src_resources.cpp @@ -1,10 +1,25 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [4] -QResource::registerResource("/path/to/myresource.rcc"); -//! [4] +#include <QResource> +#include <QFile> +void wrap() +{ + //! [4] + QResource::registerResource("/path/to/myresource.rcc"); + //! [4] +} + +class BaseClass { + public: + BaseClass() {} +}; + +class MyClass : BaseClass { + public: + MyClass(); +}; //! [5] MyClass::MyClass() : BaseClass() @@ -12,6 +27,6 @@ MyClass::MyClass() : BaseClass() Q_INIT_RESOURCE(resources); QFile file(":/myfile.dat"); - ... + //... } //! [5] diff --git a/src/corelib/doc/snippets/code/src_corelib_animation_qparallelanimationgroup.cpp b/src/corelib/doc/snippets/code/src_corelib_animation_qparallelanimationgroup.cpp index 6328cb00119..db3a9ee2dc0 100644 --- a/src/corelib/doc/snippets/code/src_corelib_animation_qparallelanimationgroup.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_animation_qparallelanimationgroup.cpp @@ -1,10 +1,15 @@ // Copyright (C) 2018 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [0] - QParallelAnimationGroup *group = new QParallelAnimationGroup; - group->addAnimation(anim1); - group->addAnimation(anim2); +#include <QParallelAnimationGroup> - group->start(); -//! [0] +void example(QAbstractAnimation *anim1, QAbstractAnimation *anim2) +{ + //! [0] + QParallelAnimationGroup *group = new QParallelAnimationGroup; + group->addAnimation(anim1); + group->addAnimation(anim2); + + group->start(); + //! [0] +} diff --git a/src/corelib/doc/snippets/code/src_corelib_animation_qpropertyanimation.cpp b/src/corelib/doc/snippets/code/src_corelib_animation_qpropertyanimation.cpp index 4b77ab607db..7f259e99fb8 100644 --- a/src/corelib/doc/snippets/code/src_corelib_animation_qpropertyanimation.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_animation_qpropertyanimation.cpp @@ -1,28 +1,102 @@ // Copyright (C) 2018 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +#include <QParallelAnimationGroup> +#include <QSequentialAnimationGroup> -//! [0] +//! [includes] #include <QApplication> #include <QPushButton> #include <QPropertyAnimation> +//![includes] + +//! [class_decl] class MyButtonWidget : public QWidget { public: MyButtonWidget(QWidget *parent = nullptr); }; +//! [class_decl] + +//! [ctor_impl] MyButtonWidget::MyButtonWidget(QWidget *parent) : QWidget(parent) { - QPushButton *button = new QPushButton(tr("Animated Button"), this); - QPropertyAnimation *anim = new QPropertyAnimation(button, "pos", this); - anim->setDuration(10000); - anim->setStartValue(QPoint(0, 0)); - anim->setEndValue(QPoint(100, 250)); - anim->start(); +//![ctor_impl] + { + //! [first_example] + QPushButton *button = new QPushButton(tr("Animated Button"), this); + QPropertyAnimation *anim = new QPropertyAnimation(button, "pos", this); + anim->setDuration(10000); + anim->setStartValue(QPoint(0, 0)); + anim->setEndValue(QPoint(100, 250)); + anim->start(); + //! [first_example] + } + + { + //! [easing-curve] + QPushButton *button = new QPushButton(tr("Animated Button"), this); + QPropertyAnimation *anim = new QPropertyAnimation(button, "pos", this); + anim->setDuration(10000); + anim->setStartValue(QPoint(0, 0)); + anim->setEndValue(QPoint(100, 250)); + anim->setEasingCurve(QEasingCurve::OutBounce); + anim->start(); + //! [easing-curve] + } + + { + //! [animation-group1] + QPushButton *bonnie = new QPushButton(tr("Bonnie"), this); + QPushButton *clyde = new QPushButton(tr("Clyde"), this); + + QPropertyAnimation *anim1 = new QPropertyAnimation(bonnie, "pos", this); + anim1->setDuration(3000); + anim1->setStartValue(QPoint(0, 0)); + anim1->setEndValue(QPoint(100, 250)); + + QPropertyAnimation *anim2 = new QPropertyAnimation(clyde, "pos", this); + anim2->setDuration(3000); + anim2->setStartValue(QPoint(100, 250)); + anim2->setEndValue(QPoint(500, 500)); + + QParallelAnimationGroup *parallelAnim = new QParallelAnimationGroup; + parallelAnim->addAnimation(anim1); + parallelAnim->addAnimation(anim2); + parallelAnim->start(); + //! [animation-group1] + } + + { + //! [animation-group2] + QPushButton *bonnie = new QPushButton(tr("Bonnie"), this); + QPushButton *clyde = new QPushButton(tr("Clyde"), this); + + QPropertyAnimation *anim1 = new QPropertyAnimation(bonnie, "pos", this); + anim1->setDuration(3000); + anim1->setStartValue(QPoint(0, 0)); + anim1->setEndValue(QPoint(100, 250)); + + QPropertyAnimation *anim2 = new QPropertyAnimation(clyde, "pos", this); + anim2->setDuration(3000); + anim2->setStartValue(QPoint(0, 0)); + anim2->setEndValue(QPoint(200, 250)); + + QSequentialAnimationGroup *sequenceAnim = new QSequentialAnimationGroup; + sequenceAnim->addAnimation(anim1); + sequenceAnim->addAnimation(anim2); + sequenceAnim->start(); + //! [animation-group2] + } + +//! [ctor_close] } +//! [ctor_close] + +//! [main] int main(int argc, char *argv[]) { QApplication a(argc, argv); @@ -31,65 +105,4 @@ int main(int argc, char *argv[]) buttonAnimWidget.show(); return a.exec(); } -//! [0] - - -//! [easing-curve] -MyButtonWidget::MyButtonWidget(QWidget *parent) : QWidget(parent) -{ - QPushButton *button = new QPushButton(tr("Animated Button"), this); - QPropertyAnimation *anim = new QPropertyAnimation(button, "pos", this); - anim->setDuration(10000); - anim->setStartValue(QPoint(0, 0)); - anim->setEndValue(QPoint(100, 250)); - anim->setEasingCurve(QEasingCurve::OutBounce); - anim->start(); -} -//! [easing-curve] - - -//! [animation-group1] -MyButtonWidget::MyButtonWidget(QWidget *parent) : QWidget(parent) -{ - QPushButton *bonnie = new QPushButton(tr("Bonnie"), this); - QPushButton *clyde = new QPushButton(tr("Clyde"), this); - - QPropertyAnimation *anim1 = new QPropertyAnimation(bonnie, "pos", this); - anim1->setDuration(3000); - anim1->setStartValue(QPoint(0, 0)); - anim1->setEndValue(QPoint(100, 250)); - - QPropertyAnimation *anim2 = new QPropertyAnimation(clyde, "pos", this); - anim2->setDuration(3000); - anim2->setStartValue(QPoint(100, 250)); - anim2->setEndValue(QPoint(500, 500)); - - QParallelAnimationGroup *parallelAnim = new QParallelAnimationGroup; - parallelAnim->addAnimation(anim1); - parallelAnim->addAnimation(anim2); - parallelAnim->start(); -} -//! [animation-group1] - -//! [animation-group2] -MyButtonWidget::MyButtonWidget(QWidget *parent) : QWidget(parent) -{ - QPushButton *bonnie = new QPushButton(tr("Bonnie"), this); - QPushButton *clyde = new QPushButton(tr("Clyde"), this); - - QPropertyAnimation *anim1 = new QPropertyAnimation(bonnie, "pos", this); - anim1->setDuration(3000); - anim1->setStartValue(QPoint(0, 0)); - anim1->setEndValue(QPoint(100, 250)); - - QPropertyAnimation *anim2 = new QPropertyAnimation(clyde, "pos", this); - anim2->setDuration(3000); - anim2->setStartValue(QPoint(0, 0)); - anim2->setEndValue(QPoint(200, 250)); - - QSequentialAnimationGroup *sequenceAnim = new QSequentialAnimationGroup; - sequenceAnim->addAnimation(anim1); - sequenceAnim->addAnimation(anim2); - sequenceAnim->start(); -} -//! [animation-group2] +//! [main] diff --git a/src/corelib/doc/snippets/code/src_corelib_animation_qsequentialanimationgroup.cpp b/src/corelib/doc/snippets/code/src_corelib_animation_qsequentialanimationgroup.cpp index a429fe20b2d..c00a723a26c 100644 --- a/src/corelib/doc/snippets/code/src_corelib_animation_qsequentialanimationgroup.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_animation_qsequentialanimationgroup.cpp @@ -1,6 +1,10 @@ // Copyright (C) 2018 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +#include <QSequentialAnimationGroup> + +void example(QAbstractAnimation *anim1, QAbstractAnimation *anim2) +{ //! [0] QSequentialAnimationGroup *group = new QSequentialAnimationGroup; @@ -9,3 +13,4 @@ group->start(); //! [0] +} diff --git a/src/corelib/doc/snippets/code/src_corelib_animation_qvariantanimation.cpp b/src/corelib/doc/snippets/code/src_corelib_animation_qvariantanimation.cpp index be7f549f655..be7e51e1b44 100644 --- a/src/corelib/doc/snippets/code/src_corelib_animation_qvariantanimation.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_animation_qvariantanimation.cpp @@ -1,14 +1,22 @@ // Copyright (C) 2018 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +#include <QVariantAnimation> +#include <QColor> + //! [0] - QVariant myColorInterpolator(const QColor &start, const QColor &end, qreal progress) - { - ... - return QColor(...); - } - ... +QVariant myColorInterpolator(const QColor &start, const QColor &end, qreal progress) +{ + // ... + return QColor(/*...*/); +} +// ... +void someFunc() +{ + // ... qRegisterAnimationInterpolator<QColor>(myColorInterpolator); + // ... +} //! [0] //! [1] diff --git a/src/corelib/doc/snippets/code/src_corelib_concurrent_qthreadpool.cpp b/src/corelib/doc/snippets/code/src_corelib_concurrent_qthreadpool.cpp index 4cba70e62d4..56a0532ac21 100644 --- a/src/corelib/doc/snippets/code/src_corelib_concurrent_qthreadpool.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_concurrent_qthreadpool.cpp @@ -1,6 +1,10 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +#include <QThreadPool> +#include <QRunnable> +#include <QDebug> + //! [0] class HelloWorldTask : public QRunnable { @@ -10,7 +14,12 @@ class HelloWorldTask : public QRunnable } }; -HelloWorldTask *hello = new HelloWorldTask(); -// QThreadPool takes ownership and deletes 'hello' automatically -QThreadPool::globalInstance()->start(hello); +int main() +{ + //... + HelloWorldTask *hello = new HelloWorldTask(); + // QThreadPool takes ownership and deletes 'hello' automatically + QThreadPool::globalInstance()->start(hello); + //... +} //! [0] diff --git a/src/corelib/doc/snippets/eventfilters/CMakeLists.txt b/src/corelib/doc/snippets/eventfilters/CMakeLists.txt index ea381e425a5..7da83cbdf6e 100644 --- a/src/corelib/doc/snippets/eventfilters/CMakeLists.txt +++ b/src/corelib/doc/snippets/eventfilters/CMakeLists.txt @@ -1,17 +1,19 @@ -add_library(snippets_eventfilters OBJECT) +add_library(corelib_snippets_eventfilters OBJECT) -target_link_libraries(snippets_eventfilters PRIVATE +set_target_properties(corelib_snippets_eventfilters PROPERTIES COMPILE_OPTIONS "-w") + +target_link_libraries(corelib_snippets_eventfilters PRIVATE Qt::Core ) -qt_internal_extend_target(snippets_eventfilters CONDITION QT_FEATURE_widgets +qt_internal_extend_target(corelib_snippets_eventfilters CONDITION QT_FEATURE_widgets LIBRARIES Qt::Widgets SOURCE main.cpp ) -qt_internal_extend_target(snippets_eventfilters CONDITION QT_FEATURE_gui +qt_internal_extend_target(corelib_snippets_eventfilters CONDITION QT_FEATURE_gui LIBRARIES Qt::Gui SOURCES diff --git a/src/corelib/doc/snippets/qmetaobject-invokable/CMakeLists.txt b/src/corelib/doc/snippets/qmetaobject-invokable/CMakeLists.txt index 76bb79951a3..972bf43d9b1 100644 --- a/src/corelib/doc/snippets/qmetaobject-invokable/CMakeLists.txt +++ b/src/corelib/doc/snippets/qmetaobject-invokable/CMakeLists.txt @@ -2,13 +2,15 @@ if(NOT QT_FEATURE_widgets) return() endif() -add_library(snippets_qmetaobject-invokable OBJECT) +add_library(corelib_snippets_qmetaobject-invokable OBJECT) -target_link_libraries(snippets_qmetaobject-invokable PRIVATE +set_target_properties(corelib_snippets_qmetaobject-invokable PROPERTIES COMPILE_OPTIONS "-w") + +target_link_libraries(corelib_snippets_qmetaobject-invokable PRIVATE Qt::Core ) -qt_internal_extend_target(snippets_qmetaobject-invokable CONDITION QT_FEATURE_widgets +qt_internal_extend_target(corelib_snippets_qmetaobject-invokable CONDITION QT_FEATURE_widgets LIBRARIES Qt::Widgets SOURCES diff --git a/src/corelib/doc/snippets/qmetaobject-revision/CMakeLists.txt b/src/corelib/doc/snippets/qmetaobject-revision/CMakeLists.txt index 0cadf9380b1..369b2e15315 100644 --- a/src/corelib/doc/snippets/qmetaobject-revision/CMakeLists.txt +++ b/src/corelib/doc/snippets/qmetaobject-revision/CMakeLists.txt @@ -2,13 +2,15 @@ if(NOT QT_FEATURE_widgets) return() endif() -add_library(snippets_qmetaobject-revision OBJECT) +add_library(corelib_snippets_qmetaobject-revision OBJECT) -target_link_libraries(snippets_qmetaobject-revision PRIVATE +set_target_properties(corelib_snippets_qmetaobject-revision PROPERTIES COMPILE_OPTIONS "-w") + +target_link_libraries(corelib_snippets_qmetaobject-revision PRIVATE Qt::Core ) -qt_internal_extend_target(snippets_qmetaobject-revision CONDITION QT_FEATURE_widgets +qt_internal_extend_target(corelib_snippets_qmetaobject-revision CONDITION QT_FEATURE_widgets LIBRARIES Qt::Widgets SOURCES diff --git a/src/corelib/doc/snippets/qprocess/CMakeLists.txt b/src/corelib/doc/snippets/qprocess/CMakeLists.txt index d410d479f7f..d00e051ed9b 100644 --- a/src/corelib/doc/snippets/qprocess/CMakeLists.txt +++ b/src/corelib/doc/snippets/qprocess/CMakeLists.txt @@ -1,10 +1,12 @@ set(CMAKE_UNITY_BUILD OFF) -add_library(snippets_qprocess OBJECT +add_library(corelib_snippets_qprocess OBJECT qprocess-createprocessargumentsmodifier.cpp qprocess-simpleexecution.cpp ) -target_link_libraries(snippets_qprocess PRIVATE +set_target_properties(corelib_snippets_qprocess PROPERTIES COMPILE_OPTIONS "-w") + +target_link_libraries(corelib_snippets_qprocess PRIVATE Qt::Core ) diff --git a/src/corelib/doc/snippets/qsignalmapper/CMakeLists.txt b/src/corelib/doc/snippets/qsignalmapper/CMakeLists.txt deleted file mode 100644 index 086ddbf9c5e..00000000000 --- a/src/corelib/doc/snippets/qsignalmapper/CMakeLists.txt +++ /dev/null @@ -1,18 +0,0 @@ -if(NOT TARGET Qt::Widgets) - return() -endif() - -add_library(snippets_qsignalmapper OBJECT - buttonwidget.cpp -) - -add_compile_definitions(EXAMPLE_ONE) - -target_link_libraries(snippets_qsignalmapper PRIVATE - Qt::Core -) - -qt_internal_extend_target(snippets_qsignalmapper CONDITION QT_FEATURE_widgets - LIBRARIES - Qt::Widgets -) diff --git a/src/corelib/doc/snippets/qsignalmapper/buttonwidget.cpp b/src/corelib/doc/snippets/qsignalmapper/buttonwidget.cpp index 1f9e53f1385..cdbf83a977d 100644 --- a/src/corelib/doc/snippets/qsignalmapper/buttonwidget.cpp +++ b/src/corelib/doc/snippets/qsignalmapper/buttonwidget.cpp @@ -5,39 +5,40 @@ #include <QtWidgets> -#ifdef EXAMPLE_ONE -//! [0] -ButtonWidget::ButtonWidget(const QStringList &texts, QWidget *parent) - : QWidget(parent) -{ - signalMapper = new QSignalMapper(this); + //! [OpenCtor] + ButtonWidget::ButtonWidget(const QStringList &texts, QWidget *parent) + : QWidget(parent) + { + //! [OpenCtor] + { + //![OldNotation] + signalMapper = new QSignalMapper(this); - QGridLayout *gridLayout = new QGridLayout(this); - for (int i = 0; i < texts.size(); ++i) { - QPushButton *button = new QPushButton(texts[i]); - connect(button, &QPushButton::clicked, signalMapper, qOverload<>(&QSignalMapper::map)); -//! [0] //! [1] - signalMapper->setMapping(button, texts[i]); - gridLayout->addWidget(button, i / 3, i % 3); + QGridLayout *gridLayout = new QGridLayout(this); + for (int i = 0; i < texts.size(); ++i) { + QPushButton *button = new QPushButton(texts[i]); + connect(button, &QPushButton::clicked, signalMapper, qOverload<>(&QSignalMapper::map)); + signalMapper->setMapping(button, texts[i]); + gridLayout->addWidget(button, i / 3, i % 3); + } + + connect(signalMapper, &QSignalMapper::mappedString, + this, &ButtonWidget::clicked); + //![OldNotation] + } + + { + //![ModernNotation] + QGridLayout *gridLayout = new QGridLayout(this); + for (int i = 0; i < texts.size(); ++i) { + QString text = texts[i]; + QPushButton *button = new QPushButton(text); + connect(button, &QPushButton::clicked, [this, text] { clicked(text); }); + gridLayout->addWidget(button, i / 3, i % 3); + } + //![ModernNotation] } - connect(signalMapper, &QSignalMapper::mappedString, -//! [1] //! [2] - this, &ButtonWidget::clicked); -} -//! [2] -#else -//! [3] -ButtonWidget::ButtonWidget(const QStringList &texts, QWidget *parent) - : QWidget(parent) -{ - QGridLayout *gridLayout = new QGridLayout(this); - for (int i = 0; i < texts.size(); ++i) { - QString text = texts[i]; - QPushButton *button = new QPushButton(text); - connect(button, &QPushButton::clicked, [this, text] { clicked(text); }); - gridLayout->addWidget(button, i / 3, i % 3); + //! [CloseBrackets] } -} -//! [3] -#endif + //! [CloseBrackets] diff --git a/src/corelib/doc/snippets/qsignalmapper/buttonwidget.h b/src/corelib/doc/snippets/qsignalmapper/buttonwidget.h index 537c21c9141..cd75628dc2b 100644 --- a/src/corelib/doc/snippets/qsignalmapper/buttonwidget.h +++ b/src/corelib/doc/snippets/qsignalmapper/buttonwidget.h @@ -4,10 +4,8 @@ #ifndef BUTTONWIDGET_H #define BUTTONWIDGET_H -#include <QWidget> +#include <QtWidgets> -class QSignalMapper; -class QString; //! [0] class ButtonWidget : public QWidget diff --git a/src/corelib/doc/src/animation.qdoc b/src/corelib/doc/src/animation.qdoc index d2e182f4806..ba5976f11eb 100644 --- a/src/corelib/doc/src/animation.qdoc +++ b/src/corelib/doc/src/animation.qdoc @@ -91,7 +91,12 @@ The following example demonstrates how you can animate a QPushButton widget: - \snippet code/src_corelib_animation_qpropertyanimation.cpp 0 + \snippet code/src_corelib_animation_qpropertyanimation.cpp includes + \snippet code/src_corelib_animation_qpropertyanimation.cpp class_decl + \snippet code/src_corelib_animation_qpropertyanimation.cpp ctor_impl + \snippet code/src_corelib_animation_qpropertyanimation.cpp first_example + \snippet code/src_corelib_animation_qpropertyanimation.cpp ctor_close + \snippet code/src_corelib_animation_qpropertyanimation.cpp main The example animates the \c pos Qt property of a QPushButton, to move it from the top--left corner of the screen to the end position (250, 250), @@ -174,7 +179,9 @@ path. + \snippet code/src_corelib_animation_qpropertyanimation.cpp ctor_impl \snippet code/src_corelib_animation_qpropertyanimation.cpp easing-curve + \snippet code/src_corelib_animation_qpropertyanimation.cpp ctor_close In this example, the animation follows a curve that makes the \c button bounce like a ball. QEasingCurve offers a large collection of curves @@ -199,13 +206,17 @@ The two following examples demonstrate the use of both QSequentialAnimationGroup and QParallelAnimationGroup: + \snippet code/src_corelib_animation_qpropertyanimation.cpp ctor_impl \snippet code/src_corelib_animation_qpropertyanimation.cpp animation-group1 + \snippet code/src_corelib_animation_qpropertyanimation.cpp ctor_close A parallel group plays more than one animation at the same time. Its \l{QAbstractAnimation::}{start()} function starts all animations that are part of the group. + \snippet code/src_corelib_animation_qpropertyanimation.cpp ctor_impl \snippet code/src_corelib_animation_qpropertyanimation.cpp animation-group2 + \snippet code/src_corelib_animation_qpropertyanimation.cpp ctor_close As the name suggests, a QSequentialAnimationGroup plays its animations in sequence. It starts the next animation in @@ -225,7 +236,12 @@ independent QPropertyAnimation has the QApplication instance as its parent: - \snippet code/src_corelib_animation_qpropertyanimation.cpp 0 + \snippet code/src_corelib_animation_qpropertyanimation.cpp includes + \snippet code/src_corelib_animation_qpropertyanimation.cpp class_decl + \snippet code/src_corelib_animation_qpropertyanimation.cpp ctor_impl + \snippet code/src_corelib_animation_qpropertyanimation.cpp first_example + \snippet code/src_corelib_animation_qpropertyanimation.cpp ctor_close + \snippet code/src_corelib_animation_qpropertyanimation.cpp main \note You can also control the animation's lifespan by choosing a \l{QAbstractAnimation::DeletionPolicy}{delete policy} while starting it. diff --git a/src/corelib/doc/src/objectmodel/objecttrees.qdoc b/src/corelib/doc/src/objectmodel/objecttrees.qdoc index 8939a534d40..25245fd5de8 100644 --- a/src/corelib/doc/src/objectmodel/objecttrees.qdoc +++ b/src/corelib/doc/src/objectmodel/objecttrees.qdoc @@ -59,7 +59,9 @@ behavior applies. Normally, the order of destruction still doesn't present a problem. Consider the following snippet: - \snippet code/doc_src_objecttrees.cpp 0 + \snippet code/doc_src_objecttrees.cpp open + \snippet code/doc_src_objecttrees.cpp example1 + \snippet code/doc_src_objecttrees.cpp close The parent, \c window, and the child, \c quit, are both \l {QObject} {QObjects} because QPushButton inherits QWidget, and QWidget inherits @@ -73,7 +75,9 @@ But now consider what happens if we swap the order of construction, as shown in this second snippet: - \snippet code/doc_src_objecttrees.cpp 1 + \snippet code/doc_src_objecttrees.cpp open + \snippet code/doc_src_objecttrees.cpp example2 + \snippet code/doc_src_objecttrees.cpp close In this case, the order of destruction causes a problem. The parent's destructor is called first because it was created last. It then calls diff --git a/src/corelib/io/qdirlisting.cpp b/src/corelib/io/qdirlisting.cpp index c626033dcdb..1fec92a01e2 100644 --- a/src/corelib/io/qdirlisting.cpp +++ b/src/corelib/io/qdirlisting.cpp @@ -510,16 +510,14 @@ bool QDirListingPrivate::matchesFilters(QDirEntryInfo &entryInfo) const if (!iteratorFlags.testAnyFlag(F::IncludeHidden) && entryInfo.isHidden()) return false; - if (entryInfo.isSymLink()) { - // With ResolveSymlinks, we look at the type of the link's target, - // and exclude broken symlinks (where the target doesn't exist). - if (iteratorFlags.testAnyFlag(F::ResolveSymlinks)) { - if (!entryInfo.exists()) - return false; - } else if (iteratorFlags.testAnyFlags(F::FilesOnly) - || iteratorFlags.testAnyFlags(F::DirsOnly)) { - return false; // symlink is not a file or dir - } + // With ResolveSymlinks, we look at the type of the link's target, + // and exclude broken symlinks (where the target doesn't exist). + if (iteratorFlags.testAnyFlag(F::ResolveSymlinks)) { + if (entryInfo.isSymLink() && !entryInfo.exists()) + return false; + } else if ((iteratorFlags.testAnyFlags(F::FilesOnly) + || iteratorFlags.testAnyFlags(F::DirsOnly)) && entryInfo.isSymLink()) { + return false; // symlink is not a file or dir } if (iteratorFlags.testAnyFlag(F::ExcludeOther) diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 1b711ba16bd..55d9cea7c6c 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -1618,6 +1618,17 @@ QCoreApplicationPrivate::QPostEventListLocker QCoreApplicationPrivate::lockThrea details. Events with equal \a priority will be processed in the order posted. + \note QObject::deleteLater() schedules the object for deferred + deletion, which is typically handled by the receiver's event + loop. If no event loop is running in the thread, the deletion + will be performed when the thread finishes. A common and safe + pattern is to connect the thread's finished() signal to the + object's deleteLater() slot: + + \code + QObject::connect(thread, &QThread::finished, worker, &QObject::deleteLater); + \endcode + \threadsafe \sa sendEvent(), notify(), sendPostedEvents(), Qt::EventPriority diff --git a/src/corelib/kernel/qmetaobject.cpp b/src/corelib/kernel/qmetaobject.cpp index de5f27f9619..7f000830e6a 100644 --- a/src/corelib/kernel/qmetaobject.cpp +++ b/src/corelib/kernel/qmetaobject.cpp @@ -808,8 +808,7 @@ static int indexOfMethod_helper(const QMetaObject *m, const char *method) int QMetaObject::indexOfMethod(const char *method) const { - const QMetaObject *m = this; - int i = indexOfMethod_helper(m, method); + int i = indexOfMethod_helper(this, method); INDEXOF_COMPAT(Method, method); return i; } @@ -881,8 +880,7 @@ static int indexOfSignal_helper(const QMetaObject *m, const char *signal) int QMetaObject::indexOfSignal(const char *signal) const { - const QMetaObject *m = this; - int i = indexOfSignal_helper(m, signal); + int i = indexOfSignal_helper(this, signal); INDEXOF_COMPAT(Signal, signal); return i; } @@ -938,8 +936,7 @@ static int indexOfSlot_helper(const QMetaObject *m, const char *slot) int QMetaObject::indexOfSlot(const char *slot) const { - const QMetaObject *m = this; - int i = indexOfSlot_helper(m, slot); + int i = indexOfSlot_helper(this, slot); INDEXOF_COMPAT(Slot, slot); return i; } @@ -1733,16 +1730,8 @@ bool QMetaObject::invokeMethodImpl(QObject *object, QtPrivate::QSlotObjectBase * "queued connections"); return false; } - auto event = std::make_unique<QMetaCallEvent>(std::move(slot), nullptr, -1, parameterCount); - void **args = event->args(); - QMetaType *types = event->types(); - - for (int i = 1; i < parameterCount; ++i) { - types[i] = QMetaType(metaTypes[i]); - args[i] = types[i].create(argv[i]); - } - - QCoreApplication::postEvent(object, event.release()); + QCoreApplication::postEvent(object, new QQueuedMetaCallEvent(std::move(slot), nullptr, -1, + parameterCount, metaTypes, params)); } else if (type == Qt::BlockingQueuedConnection) { #if QT_CONFIG(thread) if (receiverInSameThread) @@ -2889,31 +2878,27 @@ auto QMetaMethodInvoker::invokeImpl(QMetaMethod self, void *target, return InvokeFailReason::CouldNotQueueParameter; } - auto event = std::make_unique<QMetaCallEvent>(idx_offset, idx_relative, callFunction, nullptr, -1, paramCount); - QMetaType *types = event->types(); - void **args = event->args(); - + QVarLengthArray<const QtPrivate::QMetaTypeInterface *> argTypes; + argTypes.emplace_back(nullptr); // return type // fill in the meta types first for (int i = 1; i < paramCount; ++i) { - types[i] = QMetaType(methodMetaTypes[i - 1]); - if (!types[i].iface() && (!MetaTypesAreOptional || metaTypes)) - types[i] = QMetaType(metaTypes[i]); - if (!types[i].iface()) - types[i] = priv->parameterMetaType(i - 1); - if (!types[i].iface() && typeNames[i]) - types[i] = QMetaType::fromName(typeNames[i]); - if (!types[i].iface()) { + QMetaType type = QMetaType(methodMetaTypes[i - 1]); + if (!type.iface() && (!MetaTypesAreOptional || metaTypes)) + type = QMetaType(metaTypes[i]); + if (!type.iface()) + type = priv->parameterMetaType(i - 1); + if (!type.iface() && typeNames[i]) + type = QMetaType::fromName(typeNames[i]); + if (!type.iface()) { qWarning("QMetaMethod::invoke: Unable to handle unregistered datatype '%s'", typeNames[i]); return InvokeFailReason(int(InvokeFailReason::CouldNotQueueParameter) - i); } + argTypes.emplace_back(type.iface()); } - // now create copies of our parameters using those meta types - for (int i = 1; i < paramCount; ++i) - args[i] = types[i].create(parameters[i]); - - QCoreApplication::postEvent(object, event.release()); + QCoreApplication::postEvent(object, new QQueuedMetaCallEvent(idx_offset, idx_relative, callFunction, nullptr, + -1, paramCount, argTypes.data(), parameters)); } else { // blocking queued connection #if QT_CONFIG(thread) if (receiverInSameThread()) { diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index 888c71f5a49..790ccc29339 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -599,14 +599,19 @@ QMetaCallEvent::QMetaCallEvent(QtPrivate::SlotObjUniquePtr slotO, /*! \internal */ +QMetaCallEvent::QMetaCallEvent(const QObject *sender, int signalId, Data &&data) + : QAbstractMetaCallEvent(sender, signalId), + d(std::move(data)), + prealloc_() +{ +} + +/*! + \internal + */ QMetaCallEvent::~QMetaCallEvent() { if (d.nargs_) { - QMetaType *t = types(); - for (int i = 0; i < d.nargs_; ++i) { - if (t[i].isValid() && d.args_[i]) - t[i].destroy(d.args_[i]); - } if (reinterpret_cast<void *>(d.args_) != reinterpret_cast<void *>(prealloc_)) free(d.args_); } @@ -628,6 +633,115 @@ void QMetaCallEvent::placeMetaCall(QObject *object) } /*! + \internal + + Constructs a QQueuedMetaCallEvent by copying the argument values using their meta-types. + */ +QQueuedMetaCallEvent::QQueuedMetaCallEvent(ushort method_offset, ushort method_relative, + QObjectPrivate::StaticMetaCallFunction callFunction, + const QObject *sender, int signalId, int argCount, + const QtPrivate::QMetaTypeInterface * const *argTypes, + const void * const *argValues) + : QMetaCallEvent(sender, signalId, {nullptr, nullptr, callFunction, argCount, + method_offset, method_relative}) +{ + allocArgs(); + copyArgValues(argCount, argTypes, argValues); +} + +/*! + \internal + + Constructs a QQueuedMetaCallEvent by copying the argument values using their meta-types. + */ +QQueuedMetaCallEvent::QQueuedMetaCallEvent(QtPrivate::QSlotObjectBase *slotObj, + const QObject *sender, int signalId, int argCount, + const QtPrivate::QMetaTypeInterface * const *argTypes, + const void * const *argValues) + : QMetaCallEvent(sender, signalId, {QtPrivate::SlotObjUniquePtr(slotObj), nullptr, nullptr, argCount, + 0, ushort(-1)}) +{ + if (d.slotObj_) + d.slotObj_->ref(); + allocArgs(); + copyArgValues(argCount, argTypes, argValues); +} + +/*! + \internal + + Constructs a QQueuedMetaCallEvent by copying the argument values using their meta-types. + */ +QQueuedMetaCallEvent::QQueuedMetaCallEvent(QtPrivate::SlotObjUniquePtr slotObj, + const QObject *sender, int signalId, int argCount, + const QtPrivate::QMetaTypeInterface * const *argTypes, + const void * const *argValues) + : QMetaCallEvent(sender, signalId, {std::move(slotObj), nullptr, nullptr, argCount, + 0, ushort(-1)}) +{ + allocArgs(); + copyArgValues(argCount, argTypes, argValues); +} + +/*! + \internal + */ +QQueuedMetaCallEvent::~QQueuedMetaCallEvent() +{ + const QMetaType *t = types(); + int inplaceIndex = 0; + for (int i = 0; i < d.nargs_; ++i) { + if (t[i].isValid() && d.args_[i]) { + if (typeFitsInPlace(t[i]) && inplaceIndex < InplaceValuesCapacity) { + // Only destruct + void *where = &valuesPrealloc_[inplaceIndex++].storage; + t[i].destruct(where); + } else { + // Destruct and deallocate + t[i].destroy(d.args_[i]); + } + } + } +} + +/*! + \internal + */ +inline void QQueuedMetaCallEvent::copyArgValues(int argCount, const QtPrivate::QMetaTypeInterface * const *argTypes, + const void * const *argValues) +{ + void **args = d.args_; + QMetaType *types = this->types(); + int inplaceIndex = 0; + + types[0] = QMetaType(); // return type + args[0] = nullptr; // return value pointer + // no return value + + for (int n = 1; n < argCount; ++n) { + types[n] = QMetaType(argTypes[n]); + if (typeFitsInPlace(types[n]) && inplaceIndex < InplaceValuesCapacity) { + // Copy-construct in place + void *where = &valuesPrealloc_[inplaceIndex++].storage; + types[n].construct(where, argValues[n]); + args[n] = where; + } else { + // Allocate and copy-construct + args[n] = types[n].create(argValues[n]); + } + } +} + +/*! + \internal + */ +inline bool QQueuedMetaCallEvent::typeFitsInPlace(const QMetaType type) +{ + return (q20::cmp_less_equal(type.sizeOf(), sizeof(ArgValueStorage)) && + q20::cmp_less_equal(type.alignOf(), alignof(ArgValueStorage))); +} + +/*! \class QSignalBlocker \brief Exception-safe wrapper around QObject::blockSignals(). \since 5.3 @@ -2406,6 +2520,14 @@ void QObject::removeEventFilter(QObject *obj) thread with no running event loop, the object will be destroyed when the thread finishes. + A common pattern when using a worker \c QObject in a \c QThread + is to connect the thread's \c finished() signal to the worker's + \c deleteLater() slot to ensure it is safely deleted: + + \code + connect(thread, &QThread::finished, worker, &QObject::deleteLater); + \endcode + Note that entering and leaving a new event loop (e.g., by opening a modal dialog) will \e not perform the deferred deletion; for the object to be deleted, the control must return to the event loop from which deleteLater() @@ -4069,26 +4191,19 @@ static void queued_activate(QObject *sender, int signal, QObjectPrivate::Connect SlotObjectGuard slotObjectGuard { c->isSlotObject ? c->slotObj : nullptr }; locker.unlock(); - QMetaCallEvent *ev = c->isSlotObject ? - new QMetaCallEvent(c->slotObj, sender, signal, nargs) : - new QMetaCallEvent(c->method_offset, c->method_relative, c->callFunction, sender, signal, nargs); - - void **args = ev->args(); - QMetaType *types = ev->types(); - - types[0] = QMetaType(); // return type - args[0] = nullptr; // return value - - if (nargs > 1) { - for (int n = 1; n < nargs; ++n) - types[n] = QMetaType(argumentTypes[n - 1]); - - for (int n = 1; n < nargs; ++n) - args[n] = types[n].create(argv[n]); + QVarLengthArray<const QtPrivate::QMetaTypeInterface *> argTypes; + argTypes.emplace_back(nullptr); // return type + for (int n = 1; n < nargs; ++n) { + argTypes.emplace_back(QMetaType(argumentTypes[n - 1]).iface()); // convert type ids to QMetaTypeInterfaces } + auto ev = c->isSlotObject ? + std::make_unique<QQueuedMetaCallEvent>(c->slotObj, + sender, signal, nargs, argTypes.data(), argv) : + std::make_unique<QQueuedMetaCallEvent>(c->method_offset, c->method_relative, c->callFunction, + sender, signal, nargs, argTypes.data(), argv); + if (c->isSingleShot && !QObjectPrivate::removeConnection(c)) { - delete ev; return; } @@ -4096,11 +4211,10 @@ static void queued_activate(QObject *sender, int signal, QObjectPrivate::Connect if (!c->isSingleShot && !c->receiver.loadRelaxed()) { // the connection has been disconnected while we were unlocked locker.unlock(); - delete ev; return; } - QCoreApplication::postEvent(receiver, ev); + QCoreApplication::postEvent(receiver, ev.release()); } template <bool callbacks_enabled> diff --git a/src/corelib/kernel/qobject_p.h b/src/corelib/kernel/qobject_p.h index dca9c1af932..75aeba73583 100644 --- a/src/corelib/kernel/qobject_p.h +++ b/src/corelib/kernel/qobject_p.h @@ -351,7 +351,7 @@ private: class Q_CORE_EXPORT QMetaCallEvent : public QAbstractMetaCallEvent { public: - // blocking queued with latch - args always owned by caller + // blocking queued with latch - arguments always remain owned by the caller QMetaCallEvent(ushort method_offset, ushort method_relative, QObjectPrivate::StaticMetaCallFunction callFunction, const QObject *sender, int signalId, @@ -363,14 +363,17 @@ public: const QObject *sender, int signalId, void **args, QLatch *latch); - // queued - args allocated by event, copied by caller + // OLD - queued - args allocated by event, copied by caller + Q_DECL_DEPRECATED_X("Remove this constructor once the qtdeclarative patch is merged. Arguments are now copied by the QQueuedMetaCallEvent constructor and not the caller.") QMetaCallEvent(ushort method_offset, ushort method_relative, QObjectPrivate::StaticMetaCallFunction callFunction, const QObject *sender, int signalId, int nargs); + Q_DECL_DEPRECATED_X("Remove this constructor once the qtdeclarative patch is merged. Arguments are now copied by the QQueuedMetaCallEvent constructor and not the caller.") QMetaCallEvent(QtPrivate::QSlotObjectBase *slotObj, const QObject *sender, int signalId, int nargs); + Q_DECL_DEPRECATED_X("Remove this constructor once the qtdeclarative patch is merged. Arguments are now copied by the QQueuedMetaCallEvent constructor and not the caller.") QMetaCallEvent(QtPrivate::SlotObjUniquePtr slotObj, const QObject *sender, int signalId, int nargs); @@ -378,14 +381,16 @@ public: ~QMetaCallEvent() override; inline int id() const { return d.method_offset_ + d.method_relative_; } - inline const void * const* args() const { return d.args_; } + Q_DECL_DEPRECATED_X("Remove this function once the qtdeclarative patch is merged. Arguments are now copied by the QQueuedMetaCallEvent constructor and not the caller.") inline void ** args() { return d.args_; } - inline const QMetaType *types() const { return reinterpret_cast<QMetaType *>(d.args_ + d.nargs_); } + Q_DECL_DEPRECATED_X("Remove this function once the qtdeclarative patch is merged. Arguments are now copied by the QQueuedMetaCallEvent constructor and not the caller.") inline QMetaType *types() { return reinterpret_cast<QMetaType *>(d.args_ + d.nargs_); } virtual void placeMetaCall(QObject *object) override; -private: +protected: + // Move to QQueuedMetaCallEvent once the qtdeclarative patch is merged. + // QMetaCallEvent should not alloc anything anymore. inline void allocArgs(); struct Data { @@ -396,9 +401,50 @@ private: ushort method_offset_; ushort method_relative_; } d; - // preallocate enough space for three arguments - alignas(void *) char prealloc_[3 * sizeof(void *) + 3 * sizeof(QMetaType)]; + + inline QMetaCallEvent(const QObject *sender, int signalId, Data &&data); + inline void * const *args() const { return d.args_; } + inline QMetaType const *types() const { return reinterpret_cast<QMetaType *>(d.args_ + d.nargs_); } + + // Space for 5 argument pointers and types (including 1 return arg). + // Contiguous so that we can make one calloc() for both the pointers and the types when necessary. + // Move to QQueuedMetaCallEvent once the qtdeclarative patch is merged. + alignas(void *) char prealloc_[5 * sizeof(void *) + 5 * sizeof(QMetaType)]; +}; + +class Q_CORE_EXPORT QQueuedMetaCallEvent : public QMetaCallEvent +{ +public: + // queued - arguments are allocated and copied from argValues by these constructors + QQueuedMetaCallEvent(ushort method_offset, ushort method_relative, + QObjectPrivate::StaticMetaCallFunction callFunction, + const QObject *sender, int signalId, + int argCount, const QtPrivate::QMetaTypeInterface * const *argTypes, + const void * const *argValues); + QQueuedMetaCallEvent(QtPrivate::QSlotObjectBase *slotObj, + const QObject *sender, int signalId, + int argCount, const QtPrivate::QMetaTypeInterface * const *argTypes, + const void * const *argValues); + QQueuedMetaCallEvent(QtPrivate::SlotObjUniquePtr slotObj, + const QObject *sender, int signalId, + int argCount, const QtPrivate::QMetaTypeInterface * const *argTypes, + const void * const *argValues); + + ~QQueuedMetaCallEvent() override; + +private: + inline void copyArgValues(int argCount, const QtPrivate::QMetaTypeInterface * const *argTypes, + const void * const *argValues); + static inline bool typeFitsInPlace(const QMetaType type); + + struct ArgValueStorage { // size and alignment matching QString, QList, etc + static constexpr size_t MaxSize = 3 * sizeof(void *); + alignas(void *) char storage[MaxSize]; + }; + static constexpr int InplaceValuesCapacity = 3; + ArgValueStorage valuesPrealloc_[InplaceValuesCapacity]; }; +// The total QQueuedMetaCallEvent size is 224 bytes which is a 32-byte multiple, efficient for memory allocators. struct QAbstractDynamicMetaObject; struct Q_CORE_EXPORT QDynamicMetaObjectData diff --git a/src/corelib/kernel/qsignalmapper.cpp b/src/corelib/kernel/qsignalmapper.cpp index 65d766db4a9..96e909d1be7 100644 --- a/src/corelib/kernel/qsignalmapper.cpp +++ b/src/corelib/kernel/qsignalmapper.cpp @@ -74,9 +74,9 @@ public: The only function that we need to implement is the constructor: - \snippet qsignalmapper/buttonwidget.cpp 0 - \snippet qsignalmapper/buttonwidget.cpp 1 - \snippet qsignalmapper/buttonwidget.cpp 2 + \snippet qsignalmapper/buttonwidget.cpp OpenCtor + \snippet qsignalmapper/buttonwidget.cpp OldNotation + \snippet qsignalmapper/buttonwidget.cpp CloseBrackets A list of texts is passed to the constructor. A signal mapper is constructed and for each text in the list a QPushButton is @@ -92,7 +92,9 @@ public: slots. The example above can be rewritten simpler without QSignalMapper by connecting to a lambda function. - \snippet qsignalmapper/buttonwidget.cpp 3 + \snippet qsignalmapper/buttonwidget.cpp OpenCtor + \snippet qsignalmapper/buttonwidget.cpp ModernNotation + \snippet qsignalmapper/buttonwidget.cpp CloseBrackets \sa QObject, QButtonGroup, QActionGroup */ diff --git a/src/corelib/tools/qarraydata.h b/src/corelib/tools/qarraydata.h index 71e183e646e..38d1091ac1f 100644 --- a/src/corelib/tools/qarraydata.h +++ b/src/corelib/tools/qarraydata.h @@ -39,7 +39,7 @@ struct QArrayData }; Q_DECLARE_FLAGS(ArrayOptions, ArrayOption) - QBasicAtomicInt m_ref; + QBasicAtomicInt ref_; ArrayOptions flags; qsizetype alloc; @@ -56,19 +56,19 @@ struct QArrayData /// Returns true if sharing took place bool ref() noexcept { - m_ref.ref(); + ref_.ref(); return true; } /// Returns false if deallocation is necessary bool deref() noexcept { - return m_ref.deref(); + return ref_.deref(); } bool isShared() const noexcept { - return m_ref.loadRelaxed() != 1; + return ref_.loadRelaxed() != 1; } // Returns true if a detach is necessary before modifying the data @@ -76,7 +76,7 @@ struct QArrayData // detaching is necessary, you should be in a non-const function already bool needsDetach() noexcept { - return m_ref.loadRelaxed() > 1; + return ref_.loadRelaxed() > 1; } qsizetype detachCapacity(qsizetype newSize) const noexcept diff --git a/src/corelib/tools/qarraydataops.h b/src/corelib/tools/qarraydataops.h index 419585b0260..c20abd12c23 100644 --- a/src/corelib/tools/qarraydataops.h +++ b/src/corelib/tools/qarraydataops.h @@ -82,7 +82,7 @@ public: void destroyAll() noexcept // Call from destructors, ONLY! { Q_ASSERT(this->d); - Q_ASSERT(this->d->m_ref.loadRelaxed() == 0); + Q_ASSERT(this->d->ref_.loadRelaxed() == 0); // As this is to be called only from destructor, it doesn't need to be // exception safe; size not updated. @@ -345,7 +345,7 @@ public: // As this is to be called only from destructor, it doesn't need to be // exception safe; size not updated. - Q_ASSERT(this->d->m_ref.loadRelaxed() == 0); + Q_ASSERT(this->d->ref_.loadRelaxed() == 0); std::destroy(this->begin(), this->end()); } diff --git a/src/gui/util/qgridlayoutengine.cpp b/src/gui/util/qgridlayoutengine.cpp index e8a6b56ae20..32fee3bcdf7 100644 --- a/src/gui/util/qgridlayoutengine.cpp +++ b/src/gui/util/qgridlayoutengine.cpp @@ -471,6 +471,7 @@ void QGridLayoutRowData::dump(int indent) const qDebug("%*s Multi-cell entry <%d, %d> (stretch %d)", indent, "", it.key().first, it.key().second, it.value().q_stretch); it.value().q_box.dump(indent + 2); + ++it; } } #endif diff --git a/src/plugins/platforms/cocoa/qcocoaclipboard.mm b/src/plugins/platforms/cocoa/qcocoaclipboard.mm index 241faadbec0..0b84cae956b 100644 --- a/src/plugins/platforms/cocoa/qcocoaclipboard.mm +++ b/src/plugins/platforms/cocoa/qcocoaclipboard.mm @@ -3,6 +3,7 @@ #include "qcocoaclipboard.h" +#include <QtGui/qguiapplication.h> #include <QtGui/qutimimeconverter.h> #ifndef QT_NO_CLIPBOARD diff --git a/src/plugins/platforms/cocoa/qcocoacursor.h b/src/plugins/platforms/cocoa/qcocoacursor.h index 82c03573763..5c8aaeb1fde 100644 --- a/src/plugins/platforms/cocoa/qcocoacursor.h +++ b/src/plugins/platforms/cocoa/qcocoacursor.h @@ -4,9 +4,10 @@ #ifndef QWINDOWSCURSOR_H #define QWINDOWSCURSOR_H -#include <QtCore> #include <qpa/qplatformcursor.h> +#include <QtCore/qhash.h> + Q_FORWARD_DECLARE_OBJC_CLASS(NSCursor); QT_BEGIN_NAMESPACE diff --git a/src/plugins/platforms/cocoa/qcocoadrag.h b/src/plugins/platforms/cocoa/qcocoadrag.h index c5c126ecf3e..09ba685078b 100644 --- a/src/plugins/platforms/cocoa/qcocoadrag.h +++ b/src/plugins/platforms/cocoa/qcocoadrag.h @@ -4,16 +4,12 @@ #ifndef QCOCOADRAG_H #define QCOCOADRAG_H -#include <QtGui> #include <qpa/qplatformdrag.h> -#include <private/qsimpledrag_p.h> +#include <QtGui/private/qsimpledrag_p.h> +#include <QtGui/private/qinternalmimedata_p.h> #include <QtCore/private/qcore_mac_p.h> -#include <QtGui/private/qdnd_p.h> -#include <QtGui/private/qinternalmimedata_p.h> - -#include <QtCore/qeventloop.h> Q_FORWARD_DECLARE_OBJC_CLASS(NSView); Q_FORWARD_DECLARE_OBJC_CLASS(NSEvent); @@ -21,6 +17,10 @@ Q_FORWARD_DECLARE_OBJC_CLASS(NSPasteboard); QT_BEGIN_NAMESPACE +class QDrag; +class QEventLoop; +class QMimeData; + class QCocoaDrag : public QPlatformDrag { public: diff --git a/src/plugins/platforms/cocoa/qcocoadrag.mm b/src/plugins/platforms/cocoa/qcocoadrag.mm index 64df903edcb..0f9df3f17ab 100644 --- a/src/plugins/platforms/cocoa/qcocoadrag.mm +++ b/src/plugins/platforms/cocoa/qcocoadrag.mm @@ -7,8 +7,15 @@ #include "qcocoadrag.h" #include "qmacclipboard.h" #include "qcocoahelpers.h" -#include <QtGui/private/qcoregraphics_p.h> + +#include <QtGui/qfont.h> +#include <QtGui/qfontmetrics.h> +#include <QtGui/qpainter.h> #include <QtGui/qutimimeconverter.h> +#include <QtGui/private/qcoregraphics_p.h> +#include <QtGui/private/qdnd_p.h> + +#include <QtCore/qeventloop.h> #include <QtCore/private/qcore_mac_p.h> #include <vector> diff --git a/src/plugins/platforms/cocoa/qcocoahelpers.mm b/src/plugins/platforms/cocoa/qcocoahelpers.mm index 7ef958e5d9b..a569ce2ba4d 100644 --- a/src/plugins/platforms/cocoa/qcocoahelpers.mm +++ b/src/plugins/platforms/cocoa/qcocoahelpers.mm @@ -8,8 +8,6 @@ #include "qcocoahelpers.h" #include "qnsview.h" -#include <QtCore> -#include <QtGui> #include <qpa/qplatformscreen.h> #include <private/qguiapplication_p.h> #include <private/qwindow_p.h> diff --git a/src/plugins/platforms/cocoa/qmacclipboard.h b/src/plugins/platforms/cocoa/qmacclipboard.h index 95267565f2d..dcc300797c9 100644 --- a/src/plugins/platforms/cocoa/qmacclipboard.h +++ b/src/plugins/platforms/cocoa/qmacclipboard.h @@ -4,10 +4,10 @@ #ifndef QMACCLIPBOARD_H #define QMACCLIPBOARD_H -#include <QtGui> #include <QtGui/qutimimeconverter.h> #include <QtCore/qpointer.h> +#include <QtCore/qvariant.h> #include <ApplicationServices/ApplicationServices.h> diff --git a/src/plugins/platforms/cocoa/qmacclipboard.mm b/src/plugins/platforms/cocoa/qmacclipboard.mm index edafa3b6a10..155c4aa826d 100644 --- a/src/plugins/platforms/cocoa/qmacclipboard.mm +++ b/src/plugins/platforms/cocoa/qmacclipboard.mm @@ -11,6 +11,7 @@ #include <QtGui/qbitmap.h> #include <QtCore/qdatetime.h> #include <QtCore/qmetatype.h> +#include <QtCore/qmimedata.h> #include <QtCore/qdebug.h> #include <QtCore/private/qcore_mac_p.h> #include <QtGui/qguiapplication.h> diff --git a/src/plugins/platforms/cocoa/qmultitouch_mac_p.h b/src/plugins/platforms/cocoa/qmultitouch_mac_p.h index d47d37729f5..63647246589 100644 --- a/src/plugins/platforms/cocoa/qmultitouch_mac_p.h +++ b/src/plugins/platforms/cocoa/qmultitouch_mac_p.h @@ -15,13 +15,12 @@ #ifndef QMULTITOUCH_MAC_P_H #define QMULTITOUCH_MAC_P_H -#include <QtCore/qglobal.h> -#include <qpa/qwindowsysteminterface.h> -#include <qhash.h> -#include <QtCore> +#include <QtCore/qhash.h> +#include <QtCore/private/qcore_mac_p.h> + #include <QtGui/qpointingdevice.h> -#include <QtCore/private/qcore_mac_p.h> +#include <qpa/qwindowsysteminterface.h> Q_FORWARD_DECLARE_OBJC_CLASS(NSTouch); QT_FORWARD_DECLARE_OBJC_ENUM(NSTouchPhase, unsigned long); diff --git a/src/plugins/platforms/cocoa/qnsview_dragging.mm b/src/plugins/platforms/cocoa/qnsview_dragging.mm index b4c82ddc0d8..805cc7d59ea 100644 --- a/src/plugins/platforms/cocoa/qnsview_dragging.mm +++ b/src/plugins/platforms/cocoa/qnsview_dragging.mm @@ -3,6 +3,8 @@ // This file is included from qnsview.mm, and only used to organize the code +#include <QtGui/qdrag.h> + @implementation QNSView (Dragging) -(void)registerDragTypes diff --git a/src/plugins/platforms/ios/qiosapplicationdelegate.mm b/src/plugins/platforms/ios/qiosapplicationdelegate.mm index a01c590bba8..990409f2d17 100644 --- a/src/plugins/platforms/ios/qiosapplicationdelegate.mm +++ b/src/plugins/platforms/ios/qiosapplicationdelegate.mm @@ -73,22 +73,23 @@ UIWindowScene *windowScene = static_cast<UIWindowScene*>(scene); QUIWindow *window = [[QUIWindow alloc] initWithWindowScene:windowScene]; + window.rootViewController = [[[QIOSViewController alloc] initWithWindow:window] autorelease]; - QIOSScreen *screen = [&]{ - for (auto *screen : qGuiApp->screens()) { - auto *platformScreen = static_cast<QIOSScreen*>(screen->handle()); -#if !defined(Q_OS_VISIONOS) - if (platformScreen->uiScreen() == windowScene.screen) -#endif - return platformScreen; - } - Q_UNREACHABLE(); - }(); + self.window = [window autorelease]; +} - window.rootViewController = [[[QIOSViewController alloc] - initWithWindow:window andScreen:screen] autorelease]; +- (void)windowScene:(UIWindowScene *)windowScene + didUpdateCoordinateSpace:(id<UICoordinateSpace>)previousCoordinateSpace + interfaceOrientation:(UIInterfaceOrientation)previousInterfaceOrientation + traitCollection:(UITraitCollection *)previousTraitCollection +{ + qCDebug(lcQpaWindowScene) << "Scene" << windowScene << "did update properties"; + if (!self.window) + return; - self.window = [window autorelease]; + Q_ASSERT([self.window isKindOfClass:QUIWindow.class]); + auto *viewController = static_cast<QIOSViewController*>(self.window.rootViewController); + [viewController updatePlatformScreen]; } - (void)sceneDidDisconnect:(UIScene *)scene diff --git a/src/plugins/platforms/ios/qiosglobal.mm b/src/plugins/platforms/ios/qiosglobal.mm index 9ab490b2970..89a343061ee 100644 --- a/src/plugins/platforms/ios/qiosglobal.mm +++ b/src/plugins/platforms/ios/qiosglobal.mm @@ -13,7 +13,7 @@ QT_BEGIN_NAMESPACE Q_LOGGING_CATEGORY(lcQpaApplication, "qt.qpa.application"); -Q_LOGGING_CATEGORY(lcQpaInputMethods, "qt.qpa.input.methods"); +Q_LOGGING_CATEGORY(lcQpaInputMethods, "qt.qpa.input.methods", QtCriticalMsg); Q_LOGGING_CATEGORY(lcQpaWindow, "qt.qpa.window"); Q_LOGGING_CATEGORY(lcQpaWindowScene, "qt.qpa.window.scene"); diff --git a/src/plugins/platforms/ios/qiosinputcontext.mm b/src/plugins/platforms/ios/qiosinputcontext.mm index 2926e6788ba..8544c497d43 100644 --- a/src/plugins/platforms/ios/qiosinputcontext.mm +++ b/src/plugins/platforms/ios/qiosinputcontext.mm @@ -640,8 +640,10 @@ void QIOSInputContext::update(Qt::InputMethodQueries updatedProperties) // focus object. We try to detect code paths that fail this assertion and smooth // over the situation by doing a manual update of the focus object. if (qApp->focusObject() != m_imeState.focusObject && updatedProperties != Qt::ImQueryAll) { - qWarning() << "stale focus object" << static_cast<void *>(m_imeState.focusObject) - << ", doing manual update"; + qCWarning(lcQpaInputMethods).verbosity(0) << "Updating input context" << updatedProperties + << "with last reported focus object" << m_imeState.focusObject + << "but qGuiApp reports" << qApp->focusObject() + << "which means someone failed to call QPlatformInputContext::setFocusObject()"; setFocusObject(qApp->focusObject()); return; } diff --git a/src/plugins/platforms/ios/qiosviewcontroller.h b/src/plugins/platforms/ios/qiosviewcontroller.h index 340cc6b6acb..b5ce88d6a33 100644 --- a/src/plugins/platforms/ios/qiosviewcontroller.h +++ b/src/plugins/platforms/ios/qiosviewcontroller.h @@ -12,10 +12,12 @@ QT_END_NAMESPACE @interface QIOSViewController : UIViewController -- (instancetype)initWithWindow:(UIWindow*)window andScreen:(QT_PREPEND_NAMESPACE(QIOSScreen) *)screen; +- (instancetype)initWithWindow:(UIWindow*)window; - (void)updateStatusBarProperties; - (NSArray*)keyCommands; - (void)handleShortcut:(UIKeyCommand*)keyCommand; +- (void)updatePlatformScreen; + #ifndef Q_OS_TVOS // UIViewController diff --git a/src/plugins/platforms/ios/qiosviewcontroller.mm b/src/plugins/platforms/ios/qiosviewcontroller.mm index b6b42c59b0b..169f8ae1754 100644 --- a/src/plugins/platforms/ios/qiosviewcontroller.mm +++ b/src/plugins/platforms/ios/qiosviewcontroller.mm @@ -231,11 +231,12 @@ @synthesize preferredStatusBarStyle; #endif -- (instancetype)initWithWindow:(UIWindow*)window andScreen:(QT_PREPEND_NAMESPACE(QIOSScreen) *)screen +- (instancetype)initWithWindow:(UIWindow*)window { if (self = [self init]) { self.window = window; - self.platformScreen = screen; + self.platformScreen = nil; + [self updatePlatformScreen]; self.changingOrientation = NO; #ifndef Q_OS_TVOS @@ -314,6 +315,54 @@ // ------------------------------------------------------------------------- +- (void)updatePlatformScreen +{ + auto *windowScene = self.window.windowScene; + + QIOSScreen *newPlatformScreen = [&]{ + for (auto *screen : qGuiApp->screens()) { + auto *platformScreen = static_cast<QIOSScreen*>(screen->handle()); +#if !defined(Q_OS_VISIONOS) + if (platformScreen->uiScreen() == windowScene.screen) +#endif + return platformScreen; + } + Q_UNREACHABLE(); + }(); + + if (newPlatformScreen != self.platformScreen) { + QIOSScreen *oldPlatformScreen = self.platformScreen; + self.platformScreen = newPlatformScreen; + + qCDebug(lcQpaWindow) << "View controller" << self << "moved from" + << oldPlatformScreen << "to" << newPlatformScreen; + + QScreen *newScreen = newPlatformScreen ? newPlatformScreen->screen() : nullptr; + + const bool isPrimaryScene = !qt_apple_sharedApplication().supportsMultipleScenes + && windowScene.session.role == UIWindowSceneSessionRoleApplication; + + if (isPrimaryScene) { + // When we only have a single application-role scene we treat the + // active screen as the primary one, so that windows shown on the + // primary screen end up in our view controller. + QWindowSystemInterface::handlePrimaryScreenChanged(newPlatformScreen); + } + + for (auto *window : qGuiApp->topLevelWindows()) { + // Move window to new screen if it was on the old screen, + // or if we're setting up the primary scene, in which case + // we want to adopt all existing windows to this screen. + if ((window->screen()->handle() == oldPlatformScreen) + || (isPrimaryScene && !oldPlatformScreen)) { + QWindowSystemInterface::handleWindowScreenChanged(window, newScreen); + } + } + } +} + +// ------------------------------------------------------------------------- + - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration { self.changingOrientation = YES; diff --git a/src/plugins/styles/modernwindows/qwindows11style.cpp b/src/plugins/styles/modernwindows/qwindows11style.cpp index 7ae48a302f4..62269a21de1 100644 --- a/src/plugins/styles/modernwindows/qwindows11style.cpp +++ b/src/plugins/styles/modernwindows/qwindows11style.cpp @@ -183,30 +183,24 @@ void QWindows11Style::drawComplexControl(ComplexControl control, const QStyleOpt QObject *styleObject = option->styleObject; // Can be widget or qquickitem QRectF thumbRect = proxy()->subControlRect(CC_Slider, option, SC_SliderHandle, widget); - auto center = thumbRect.center(); const qreal outerRadius = qMin(8.0, (slider->orientation == Qt::Horizontal ? thumbRect.height() / 2.0 : thumbRect.width() / 2.0) - 1); - - thumbRect.setWidth(outerRadius); - thumbRect.setHeight(outerRadius); - thumbRect.moveCenter(center); - QPointF cursorPos = widget ? widget->mapFromGlobal(QCursor::pos()) : QPointF(); - bool isInsideHandle = thumbRect.contains(cursorPos); + bool isInsideHandle = option->activeSubControls == SC_SliderHandle; bool oldIsInsideHandle = styleObject->property("_q_insidehandle").toBool(); - int oldState = styleObject->property("_q_stylestate").toInt(); - int oldActiveControls = styleObject->property("_q_stylecontrols").toInt(); + State oldState = State(styleObject->property("_q_stylestate").toInt()); + SubControls oldActiveControls = SubControls(styleObject->property("_q_stylecontrols").toInt()); QRectF oldRect = styleObject->property("_q_stylerect").toRect(); styleObject->setProperty("_q_insidehandle", isInsideHandle); - styleObject->setProperty("_q_stylestate", int(option->state)); + styleObject->setProperty("_q_stylestate", int(state)); styleObject->setProperty("_q_stylecontrols", int(option->activeSubControls)); styleObject->setProperty("_q_stylerect", option->rect); if (option->styleObject->property("_q_end_radius").isNull()) option->styleObject->setProperty("_q_end_radius", outerRadius * 0.43); bool doTransition = (((state & State_Sunken) != (oldState & State_Sunken) - || ((oldIsInsideHandle) != (isInsideHandle)) - || oldActiveControls != int(option->activeSubControls)) + || (oldIsInsideHandle != isInsideHandle) + || (oldActiveControls != option->activeSubControls)) && state & State_Enabled); if (oldRect != option->rect) { @@ -374,31 +368,29 @@ void QWindows11Style::drawComplexControl(ComplexControl control, const QStyleOpt } } if (sub & SC_SliderHandle) { - if (const auto *slider = qstyleoption_cast<const QStyleOptionSlider *>(option)) { - const QRectF rect = proxy()->subControlRect(CC_Slider, option, SC_SliderHandle, widget); - const QPointF center = rect.center(); + const QRectF rect = proxy()->subControlRect(CC_Slider, option, SC_SliderHandle, widget); + const QPointF center = rect.center(); - const QNumberStyleAnimation* animation = qobject_cast<QNumberStyleAnimation*>(d->animation(option->styleObject)); + const QNumberStyleAnimation* animation = qobject_cast<QNumberStyleAnimation*>(d->animation(option->styleObject)); - if (animation != nullptr) - option->styleObject->setProperty("_q_inner_radius", animation->currentValue()); - else - option->styleObject->setProperty("_q_inner_radius", option->styleObject->property("_q_end_radius")); - - const qreal outerRadius = qMin(8.0,(slider->orientation == Qt::Horizontal ? rect.height() / 2.0 : rect.width() / 2.0) - 1); - const float innerRadius = option->styleObject->property("_q_inner_radius").toFloat(); - painter->setRenderHint(QPainter::Antialiasing, true); - painter->setPen(Qt::NoPen); - painter->setBrush(WINUI3Colors[colorSchemeIndex][controlFillSolid]); - painter->drawEllipse(center, outerRadius, outerRadius); - painter->setBrush(option->palette.accent()); - painter->drawEllipse(center, innerRadius, innerRadius); - - painter->setPen(WINUI3Colors[colorSchemeIndex][controlStrokeSecondary]); - painter->setBrush(Qt::NoBrush); - painter->drawEllipse(center, outerRadius + 0.5, outerRadius + 0.5); - painter->drawEllipse(center, innerRadius + 0.5, innerRadius + 0.5); - } + float innerRadius = 0; + if (animation != nullptr) + innerRadius = animation->currentValue(); + else + innerRadius = option->styleObject->property("_q_end_radius").toFloat(); + option->styleObject->setProperty("_q_inner_radius", innerRadius); + const qreal outerRadius = qMin(8.0,(slider->orientation == Qt::Horizontal ? rect.height() / 2.0 : rect.width() / 2.0) - 1); + + painter->setPen(Qt::NoPen); + painter->setBrush(winUI3Color(controlFillSolid)); + painter->drawEllipse(center, outerRadius, outerRadius); + painter->setBrush(option->palette.accent()); + painter->drawEllipse(center, innerRadius, innerRadius); + + painter->setPen(winUI3Color(controlStrokeSecondary)); + painter->setBrush(Qt::NoBrush); + painter->drawEllipse(center, outerRadius + 0.5, outerRadius + 0.5); + painter->drawEllipse(center, innerRadius + 0.5, innerRadius + 0.5); } if (slider->state & State_HasFocus) { QStyleOptionFocusRect fropt; @@ -1684,18 +1676,13 @@ void QWindows11Style::drawControl(ControlElement element, const QStyleOption *op } const bool highlightCurrent = vopt->state.testAnyFlags(State_Selected | State_MouseOver); if (highlightCurrent) { - const QAbstractItemView *view = qobject_cast<const QAbstractItemView *>(widget); - if (highContrastTheme) + if (highContrastTheme) { painter->setBrush(vopt->palette.highlight()); - else - painter->setBrush(view->alternatingRowColors() ? vopt->palette.highlight() : WINUI3Colors[colorSchemeIndex][subtleHighlightColor]); - QWidget *editorWidget = view ? view->indexWidget(view->currentIndex()) : nullptr; - if (editorWidget) { - QPalette pal = editorWidget->palette(); - QColor editorBgColor = vopt->backgroundBrush == Qt::NoBrush ? vopt->palette.color(widget->backgroundRole()) : vopt->backgroundBrush.color(); - editorBgColor.setAlpha(255); - pal.setColor(editorWidget->backgroundRole(), editorBgColor); - editorWidget->setPalette(pal); + } else { + const QAbstractItemView *view = qobject_cast<const QAbstractItemView *>(widget); + painter->setBrush(view && view->alternatingRowColors() + ? vopt->palette.highlight() + : winUI3Color(subtleHighlightColor)); } } else { painter->setBrush(vopt->backgroundBrush); @@ -2237,10 +2224,11 @@ static void populateLightSystemBasePalette(QPalette &result) static QString oldStyleSheet; const bool styleSheetChanged = oldStyleSheet != qApp->styleSheet(); - const QColor textColor = QColor(0x00,0x00,0x00,0xE4); - const QColor textDisabled = QColor(0x00,0x00,0x00,0x5C); - const QColor btnFace = QColor(0xFF,0xFF,0xFF,0xB3); - const QColor alternateBase = QColor(0x00,0x00,0x00,0x09); + constexpr QColor textColor = QColor(0x00,0x00,0x00,0xE4); + constexpr QColor textDisabled = QColor(0x00, 0x00, 0x00, 0x5C); + constexpr QColor btnFace = QColor(0xFF, 0xFF, 0xFF, 0xB3); + constexpr QColor base = QColor(0xFF, 0xFF, 0xFF, 0xFF); + constexpr QColor alternateBase = QColor(0x00, 0x00, 0x00, 0x09); const QColor btnHighlight = result.accent().color(); const QColor btnColor = result.button().color(); @@ -2252,7 +2240,7 @@ static void populateLightSystemBasePalette(QPalette &result) SET_IF_UNRESOLVED(QPalette::Active, QPalette::Mid, btnColor.darker(150)); SET_IF_UNRESOLVED(QPalette::Active, QPalette::Text, textColor); SET_IF_UNRESOLVED(QPalette::Active, QPalette::BrightText, btnHighlight); - SET_IF_UNRESOLVED(QPalette::Active, QPalette::Base, btnFace); + SET_IF_UNRESOLVED(QPalette::Active, QPalette::Base, base); SET_IF_UNRESOLVED(QPalette::Active, QPalette::Window, QColor(0xF3,0xF3,0xF3,0xFF)); SET_IF_UNRESOLVED(QPalette::Active, QPalette::ButtonText, textColor); SET_IF_UNRESOLVED(QPalette::Active, QPalette::Midlight, btnColor.lighter(125)); @@ -2269,7 +2257,7 @@ static void populateLightSystemBasePalette(QPalette &result) SET_IF_UNRESOLVED(QPalette::Inactive, QPalette::Mid, btnColor.darker(150)); SET_IF_UNRESOLVED(QPalette::Inactive, QPalette::Text, textColor); SET_IF_UNRESOLVED(QPalette::Inactive, QPalette::BrightText, btnHighlight); - SET_IF_UNRESOLVED(QPalette::Inactive, QPalette::Base, btnFace); + SET_IF_UNRESOLVED(QPalette::Inactive, QPalette::Base, base); SET_IF_UNRESOLVED(QPalette::Inactive, QPalette::Window, QColor(0xF3,0xF3,0xF3,0xFF)); SET_IF_UNRESOLVED(QPalette::Inactive, QPalette::ButtonText, textColor); SET_IF_UNRESOLVED(QPalette::Inactive, QPalette::Midlight, btnColor.lighter(125)); @@ -2290,7 +2278,7 @@ static void populateDarkSystemBasePalette(QPalette &result) static QString oldStyleSheet; const bool styleSheetChanged = oldStyleSheet != qApp->styleSheet(); - const QColor alternateBase = QColor(0xFF,0xFF,0xFF,0x0F); + constexpr QColor alternateBase = QColor(0xFF, 0xFF, 0xFF, 0x0F); SET_IF_UNRESOLVED(QPalette::Active, QPalette::AlternateBase, alternateBase); diff --git a/src/widgets/dialogs/qmessagebox.cpp b/src/widgets/dialogs/qmessagebox.cpp index f9d58e4d906..acb63c4aa9b 100644 --- a/src/widgets/dialogs/qmessagebox.cpp +++ b/src/widgets/dialogs/qmessagebox.cpp @@ -562,6 +562,10 @@ void QMessageBoxPrivate::helperClicked(QPlatformDialogHelper::StandardButton hel and \l{QMessageBox::standardButtons} {standard buttons} for accepting a user response. + While the parent parameter is optional, specifying it gives a hint + to the window manager, which can then take care of positioning, and + maintain a proper stacking order of the dialog window. + Two APIs for using QMessageBox are provided, the property-based API, and the static functions. Calling one of the static functions is the simpler approach, but it is less flexible than using the diff --git a/src/widgets/doc/snippets/code/src_gui_dialogs_qmessagebox.cpp b/src/widgets/doc/snippets/code/src_gui_dialogs_qmessagebox.cpp index ebc07025ec0..062e2e5142a 100644 --- a/src/widgets/doc/snippets/code/src_gui_dialogs_qmessagebox.cpp +++ b/src/widgets/doc/snippets/code/src_gui_dialogs_qmessagebox.cpp @@ -12,7 +12,7 @@ int ret = QMessageBox::warning(this, tr("My Application"), //! [2] -QMessageBox msgBox; +QMessageBox msgBox(this); QPushButton *connectButton = msgBox.addButton(tr("Connect"), QMessageBox::ActionRole); QPushButton *abortButton = msgBox.addButton(QMessageBox::Abort); @@ -53,13 +53,13 @@ int main(int argc, char *argv[]) //! [4] //! [5] -QMessageBox msgBox; +QMessageBox msgBox(this); msgBox.setText("The document has been modified."); msgBox.exec(); //! [5] //! [6] -QMessageBox msgBox; +QMessageBox msgBox(this); msgBox.setText("The document has been modified."); msgBox.setInformativeText("Do you want to save your changes?"); msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); diff --git a/src/widgets/styles/qstylesheetstyle.cpp b/src/widgets/styles/qstylesheetstyle.cpp index 08178e7b685..f4b21d3b70d 100644 --- a/src/widgets/styles/qstylesheetstyle.cpp +++ b/src/widgets/styles/qstylesheetstyle.cpp @@ -5269,7 +5269,11 @@ QSize QStyleSheetStyle::sizeFromContents(ContentsType ct, const QStyleOption *op if (const QStyleOptionSpinBox *spinbox = qstyleoption_cast<const QStyleOptionSpinBox *>(opt)) { if (rule.baseStyleCanDraw()) { sz = baseStyle()->sizeFromContents(ct, opt, sz, w); - } else if (spinbox->buttonSymbols != QAbstractSpinBox::NoButtons) { + if (rule.hasBox() || !rule.hasNativeBorder()) + sz = rule.boxSize(sz); + return sz; + } + if (spinbox->buttonSymbols != QAbstractSpinBox::NoButtons) { // Add some space for the up/down buttons QRenderRule subRule = renderRule(w, opt, PseudoElement_SpinBoxUpButton); if (subRule.hasDrawable()) { diff --git a/src/widgets/widgets/qtoolbararealayout.cpp b/src/widgets/widgets/qtoolbararealayout.cpp index 54c21b60387..0d7227eb038 100644 --- a/src/widgets/widgets/qtoolbararealayout.cpp +++ b/src/widgets/widgets/qtoolbararealayout.cpp @@ -564,19 +564,19 @@ int QToolBarAreaLayoutInfo::distance(const QPoint &pos) const { switch (dockPos) { case QInternal::LeftDock: - if (pos.y() < rect.bottom()) + if (pos.y() > 0 && pos.y() < rect.bottom()) return pos.x() - rect.right(); break; case QInternal::RightDock: - if (pos.y() < rect.bottom()) + if (pos.y() > 0 && pos.y() < rect.bottom()) return rect.left() - pos.x(); break; case QInternal::TopDock: - if (pos.x() < rect.right()) + if (pos.x() > 0 && pos.x() < rect.right()) return pos.y() - rect.bottom(); break; case QInternal::BottomDock: - if (pos.x() < rect.right()) + if (pos.x() > 0 && pos.x() < rect.right()) return rect.top() - pos.y(); break; |