diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2019-09-17 18:10:59 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2020-02-11 19:26:05 +0100 |
commit | 90b4528b846542bfa6f0723487315140b9de17b4 (patch) | |
tree | 9356c0e6b5a736b3228ca6793416d927432c101e /examples/quick/models | |
parent | 38c03709236f6a2114ace7adf1b6bdcdfe8e4e18 (diff) |
Avoid discouraged patterns in examples
In particular, use required properties where applicable, explicitly
import QtQml where we use it, avoid unqualified access into the root
scope of a component, use JavaScript functions with explicit parameters
as signal handlers.
Change-Id: I3eaaba47cc3c7a2a12d488e36f9eec145cedbb0e
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'examples/quick/models')
-rw-r--r-- | examples/quick/models/abstractitemmodel/main.cpp | 4 | ||||
-rw-r--r-- | examples/quick/models/abstractitemmodel/view.qml | 10 | ||||
-rw-r--r-- | examples/quick/models/objectlistmodel/main.cpp | 14 | ||||
-rw-r--r-- | examples/quick/models/objectlistmodel/view.qml | 8 | ||||
-rw-r--r-- | examples/quick/models/stringlistmodel/main.cpp | 14 | ||||
-rw-r--r-- | examples/quick/models/stringlistmodel/view.qml | 8 |
6 files changed, 33 insertions, 25 deletions
diff --git a/examples/quick/models/abstractitemmodel/main.cpp b/examples/quick/models/abstractitemmodel/main.cpp index 515f47ec30..dd5b368c6b 100644 --- a/examples/quick/models/abstractitemmodel/main.cpp +++ b/examples/quick/models/abstractitemmodel/main.cpp @@ -68,10 +68,8 @@ int main(int argc, char ** argv) QQuickView view; view.setResizeMode(QQuickView::SizeRootObjectToView); - QQmlContext *ctxt = view.rootContext(); - ctxt->setContextProperty("myModel", &model); + view.setInitialProperties({{"model", QVariant::fromValue(&model)}}); //![0] - view.setSource(QUrl("qrc:view.qml")); view.show(); diff --git a/examples/quick/models/abstractitemmodel/view.qml b/examples/quick/models/abstractitemmodel/view.qml index f699aa40c8..2b9f87df92 100644 --- a/examples/quick/models/abstractitemmodel/view.qml +++ b/examples/quick/models/abstractitemmodel/view.qml @@ -53,8 +53,14 @@ import QtQuick 2.0 ListView { width: 200; height: 250 - model: myModel - delegate: Text { text: "Animal: " + type + ", " + size } + required model + + delegate: Text { + required property string type + required property string size + + text: "Animal: " + type + ", " + size + } } //![0] diff --git a/examples/quick/models/objectlistmodel/main.cpp b/examples/quick/models/objectlistmodel/main.cpp index 977bbfb93b..8fbe7c183c 100644 --- a/examples/quick/models/objectlistmodel/main.cpp +++ b/examples/quick/models/objectlistmodel/main.cpp @@ -68,16 +68,16 @@ int main(int argc, char ** argv) { QGuiApplication app(argc, argv); - QList<QObject*> dataList; - dataList.append(new DataObject("Item 1", "red")); - dataList.append(new DataObject("Item 2", "green")); - dataList.append(new DataObject("Item 3", "blue")); - dataList.append(new DataObject("Item 4", "yellow")); + QList<QObject *> dataList = { + new DataObject("Item 1", "red"), + new DataObject("Item 2", "green"), + new DataObject("Item 3", "blue"), + new DataObject("Item 4", "yellow") + }; QQuickView view; view.setResizeMode(QQuickView::SizeRootObjectToView); - QQmlContext *ctxt = view.rootContext(); - ctxt->setContextProperty("myModel", QVariant::fromValue(dataList)); + view.setInitialProperties({{ "model", QVariant::fromValue(dataList) }}); //![0] view.setSource(QUrl("qrc:view.qml")); diff --git a/examples/quick/models/objectlistmodel/view.qml b/examples/quick/models/objectlistmodel/view.qml index d9a32aff14..ba0c905e1e 100644 --- a/examples/quick/models/objectlistmodel/view.qml +++ b/examples/quick/models/objectlistmodel/view.qml @@ -53,13 +53,15 @@ import QtQuick 2.0 //![0] ListView { width: 100; height: 100 + required model - model: myModel delegate: Rectangle { + required color + required property string name + height: 25 width: 100 - color: model.modelData.color - Text { text: name } + Text { text: parent.name } } } //![0] diff --git a/examples/quick/models/stringlistmodel/main.cpp b/examples/quick/models/stringlistmodel/main.cpp index 6b4db422a6..d9de69b3aa 100644 --- a/examples/quick/models/stringlistmodel/main.cpp +++ b/examples/quick/models/stringlistmodel/main.cpp @@ -68,15 +68,15 @@ int main(int argc, char ** argv) QGuiApplication app(argc, argv); //![0] - QStringList dataList; - dataList.append("Item 1"); - dataList.append("Item 2"); - dataList.append("Item 3"); - dataList.append("Item 4"); + QStringList dataList = { + "Item 1", + "Item 2", + "Item 3", + "Item 4" + }; QQuickView view; - QQmlContext *ctxt = view.rootContext(); - ctxt->setContextProperty("myModel", QVariant::fromValue(dataList)); + view.setInitialProperties({{ "model", QVariant::fromValue(dataList) }}); //![0] view.setSource(QUrl("qrc:view.qml")); diff --git a/examples/quick/models/stringlistmodel/view.qml b/examples/quick/models/stringlistmodel/view.qml index f74b7db1c1..e392284d0f 100644 --- a/examples/quick/models/stringlistmodel/view.qml +++ b/examples/quick/models/stringlistmodel/view.qml @@ -52,13 +52,15 @@ import QtQuick 2.0 //![0] ListView { - width: 100; height: 100 + width: 100 + height: 100 + required model - model: myModel delegate: Rectangle { + required property string modelData height: 25 width: 100 - Text { text: modelData } + Text { text: parent.modelData } } } //![0] |