aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmlmodels/doc/snippets
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@qt.io>2024-11-27 19:06:41 +0100
committerShawn Rutledge <shawn.rutledge@qt.io>2024-11-27 22:51:58 +0100
commit7dda22389674bd4ba705fbef438ff2ad97ed8ef5 (patch)
tree027876fa32b78883fb53ac192c6f6ccf5840fc04 /src/qmlmodels/doc/snippets
parenta2feeada7db56c7249640974b87111667e3e3963 (diff)
doc: Modernize QFileSystemModel snippet and get it working
- call QFileSystemModel::setRootPath() to start populating it - singleton rather than context property - alternate-colored rows - TapHandler rather than MouseArea - required properties, qualified access etc. to satisfy qmllint - no need for QApplication in Qt 6 (QGuiApplication is fine) - make it buildable (how else would we know if it works?) Pick-to: 6.8 Task-number: QTBUG-131487 Change-Id: I8b42971a3ec4f3a2fd8cfadbb2de699c852783a0 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qmlmodels/doc/snippets')
-rw-r--r--src/qmlmodels/doc/snippets/delegatemodel/delegatemodel_rootindex/CMakeLists.txt25
-rw-r--r--src/qmlmodels/doc/snippets/delegatemodel/delegatemodel_rootindex/delegatemodel_rootindex.pro5
-rw-r--r--src/qmlmodels/doc/snippets/delegatemodel/delegatemodel_rootindex/main.cpp14
-rw-r--r--src/qmlmodels/doc/snippets/delegatemodel/delegatemodel_rootindex/view.qml30
4 files changed, 58 insertions, 16 deletions
diff --git a/src/qmlmodels/doc/snippets/delegatemodel/delegatemodel_rootindex/CMakeLists.txt b/src/qmlmodels/doc/snippets/delegatemodel/delegatemodel_rootindex/CMakeLists.txt
new file mode 100644
index 0000000000..bae65ec6de
--- /dev/null
+++ b/src/qmlmodels/doc/snippets/delegatemodel/delegatemodel_rootindex/CMakeLists.txt
@@ -0,0 +1,25 @@
+# Copyright (C) 2024 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+cmake_minimum_required(VERSION 3.16)
+project(delegatemodel_rootindex LANGUAGES CXX)
+
+find_package(Qt6 REQUIRED COMPONENTS Core Gui Quick)
+qt_standard_project_setup(REQUIRES 6.8)
+
+qt_add_executable(delegatemodel_rootindex
+ main.cpp
+)
+
+qt_add_qml_module(delegatemodel_rootindex
+ URI FileSystemModule
+ VERSION 1.0
+)
+
+target_link_libraries(delegatemodel_rootindex
+ PRIVATE
+ Qt6::Core
+ Qt6::Gui
+ Qt6::Quick
+)
+
diff --git a/src/qmlmodels/doc/snippets/delegatemodel/delegatemodel_rootindex/delegatemodel_rootindex.pro b/src/qmlmodels/doc/snippets/delegatemodel/delegatemodel_rootindex/delegatemodel_rootindex.pro
new file mode 100644
index 0000000000..717e28a690
--- /dev/null
+++ b/src/qmlmodels/doc/snippets/delegatemodel/delegatemodel_rootindex/delegatemodel_rootindex.pro
@@ -0,0 +1,5 @@
+TEMPLATE = app
+TARGET = delegatemodel_rootindex
+INCLUDEPATH += .
+QT += quick
+SOURCES += main.cpp
diff --git a/src/qmlmodels/doc/snippets/delegatemodel/delegatemodel_rootindex/main.cpp b/src/qmlmodels/doc/snippets/delegatemodel/delegatemodel_rootindex/main.cpp
index 710d86eadd..f7dff07bd7 100644
--- a/src/qmlmodels/doc/snippets/delegatemodel/delegatemodel_rootindex/main.cpp
+++ b/src/qmlmodels/doc/snippets/delegatemodel/delegatemodel_rootindex/main.cpp
@@ -1,25 +1,25 @@
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-#include <QQuickView>
-#include <QQmlContext>
-
-#include <QApplication>
+#include <QGuiApplication>
#include <QFileSystemModel>
+#include <QQmlEngine>
+#include <QQuickView>
//![0]
int main(int argc, char ** argv)
{
- QApplication app(argc, argv);
+ QGuiApplication app(argc, argv);
QQuickView view;
QFileSystemModel model;
- view.rootContext()->setContextProperty("fileSystemModel", &model);
+ // start populating the model (doesn't change the model's root)
+ model.setRootPath(QDir::currentPath());
+ qmlRegisterSingletonInstance("FileSystemModule", 1, 0, "FileSystemModel", &model);
view.setSource(QUrl::fromLocalFile("view.qml"));
view.show();
return app.exec();
}
//![0]
-
diff --git a/src/qmlmodels/doc/snippets/delegatemodel/delegatemodel_rootindex/view.qml b/src/qmlmodels/doc/snippets/delegatemodel/delegatemodel_rootindex/view.qml
index 01abaf1909..d774422cac 100644
--- a/src/qmlmodels/doc/snippets/delegatemodel/delegatemodel_rootindex/view.qml
+++ b/src/qmlmodels/doc/snippets/delegatemodel/delegatemodel_rootindex/view.qml
@@ -3,6 +3,9 @@
//![0]
import QtQuick
import QtQml.Models
+import FileSystemModule
+
+pragma ComponentBehavior: Bound
ListView {
id: view
@@ -10,18 +13,27 @@ ListView {
height: 400
model: DelegateModel {
- model: fileSystemModel
+ id: delegateModel
+ model: FileSystemModel // singleton
delegate: Rectangle {
- width: 200; height: 25
- Text { text: filePath }
+ id: delegate
+ required property int index
+ required property string filePath
+ required property bool hasModelChildren
+
+ width: 300; height: 25
+ color: index % 2 ? palette.alternateBase : palette.base
+
+ Text {
+ anchors.verticalCenter: parent.verticalCenter
+ color: palette.text
+ text: delegate.filePath
+ }
- MouseArea {
- anchors.fill: parent
- onClicked: {
- if (model.hasModelChildren)
- view.model.rootIndex = view.model.modelIndex(index)
- }
+ TapHandler {
+ onTapped: if (delegate.hasModelChildren)
+ delegateModel.rootIndex = delegateModel.modelIndex(delegate.index)
}
}
}