From 54d5ca0c2766e915c960fa437cee6c20a324c1a7 Mon Sep 17 00:00:00 2001 From: Samuel Gaist Date: Sat, 16 Nov 2019 16:53:08 +0100 Subject: Doc: improve Using Model Indexes in Model View Programming guide The current example using QFileSystemModel doesn't take into account the asynchronous nature of that model. This puts people on the wrong path on how to use it. This patch improves the snippet as well as the explanation steps. Change-Id: I5c7a3c19aad48847f0b965b5eb69b492d6263f51 Reviewed-by: Paul Wicking --- src/widgets/doc/snippets/simplemodel-use/main.cpp | 7 +++++-- src/widgets/doc/src/model-view-programming.qdoc | 13 +++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) (limited to 'src/widgets/doc') diff --git a/src/widgets/doc/snippets/simplemodel-use/main.cpp b/src/widgets/doc/snippets/simplemodel-use/main.cpp index 3e106c8eea..940669e101 100644 --- a/src/widgets/doc/snippets/simplemodel-use/main.cpp +++ b/src/widgets/doc/snippets/simplemodel-use/main.cpp @@ -79,8 +79,11 @@ int main(int argc, char *argv[]) //! [0] QFileSystemModel *model = new QFileSystemModel; - QModelIndex parentIndex = model->index(QDir::currentPath()); - int numRows = model->rowCount(parentIndex); + connect(model, &QFileSystemModel::directoryLoaded, [model](const QString &directory) { + QModelIndex parentIndex = model->index(directory); + int numRows = model->rowCount(parentIndex); + }); + model->setRootPath(QDir::currentPath); //! [0] //! [1] diff --git a/src/widgets/doc/src/model-view-programming.qdoc b/src/widgets/doc/src/model-view-programming.qdoc index 236582ef3f..ede1ebf932 100644 --- a/src/widgets/doc/src/model-view-programming.qdoc +++ b/src/widgets/doc/src/model-view-programming.qdoc @@ -465,14 +465,19 @@ Although this does not show a normal way of using a model, it demonstrates the conventions used by models when dealing with model indexes. + QFileSystemModel loading is asynchronous to minimize system resource use. + We have to take that into account when dealing with this model. + We construct a file system model in the following way: \snippet simplemodel-use/main.cpp 0 - In this case, we set up a default QFileSystemModel, obtain a parent index - using a specific implementation of \l{QFileSystemModel::}{index()} - provided by that model, and we count the number of rows in the model using - the \l{QFileSystemModel::}{rowCount()} function. + In this case, we start by setting up a default QFileSystemModel. We connect + it to a lambda, in which we will obtain a parent index using a specific + implementation of \l{QFileSystemModel::}{index()} provided by that model. + In the lambda, we count the number of rows in the model using the + \l{QFileSystemModel::}{rowCount()} function. Finally, we set the root path + of the QFileSystemModel so it starts loading data and triggers the lambda. For simplicity, we are only interested in the items in the first column of the model. We examine each row in turn, obtaining a model index for -- cgit v1.2.3