diff options
Diffstat (limited to 'src/qmlmodels/doc/snippets')
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) } } } |
