diff options
33 files changed, 1086 insertions, 784 deletions
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/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/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/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/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/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp b/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp index 6dd8a6d07f9..103530ec3ed 100644 --- a/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp +++ b/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp @@ -1379,17 +1379,14 @@ void tst_QTextStream::pos2() // ------------------------------------------------------------------------------ void tst_QTextStream::pos3LargeFile() { - if (QTestPrivate::isRunningArmOnX86()) - QSKIP("Running QTextStream::pos() in tight loop is too slow on emulator"); - { QFile file(testFileName); QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text)); QTextStream out( &file ); // NOTE: The unusual spacing is to ensure non-1-character whitespace. QString lineString = " 0 1 2\t3 4\t \t5 6 7 8 9 \n"; - // Approximate 50kb text file - const int NbLines = (50*1024) / lineString.size() + 1; + // Approximately 5kb text file (more is too slow (QTBUG-138435)) + const int NbLines = (5 * 1024) / lineString.size() + 1; for (int line = 0; line < NbLines; ++line) QVERIFY(out << lineString); // File is automatically flushed and closed on destruction. |