summaryrefslogtreecommitdiffstats
path: root/examples/widgets/doc/src
diff options
context:
space:
mode:
authorCarl Schwan <[email protected]>2023-05-26 16:51:49 +0200
committerVolker Hilsheimer <[email protected]>2023-06-28 19:26:45 +0000
commit25027444a9b53d61a6257dc5f5ce0ffdb3b06f98 (patch)
tree9a08a47b66e6c2dbd3a7b3e53deeb8a269a490ae /examples/widgets/doc/src
parentbe1b589cb9f147762a5a73d2b3a44778b7ae7502 (diff)
Modernize SimpleTreeModel example
Use std::unique_ptr to manage items tree memory allocations. This also use the new string literals operator. Change-Id: Iab002b5dc612b75cef0be10862e263c6c6c013c1 Reviewed-by: Volker Hilsheimer <[email protected]>
Diffstat (limited to 'examples/widgets/doc/src')
-rw-r--r--examples/widgets/doc/src/simpletreemodel.qdoc28
1 files changed, 14 insertions, 14 deletions
diff --git a/examples/widgets/doc/src/simpletreemodel.qdoc b/examples/widgets/doc/src/simpletreemodel.qdoc
index 5892d052446..3ab5a701518 100644
--- a/examples/widgets/doc/src/simpletreemodel.qdoc
+++ b/examples/widgets/doc/src/simpletreemodel.qdoc
@@ -104,16 +104,14 @@
\snippet itemviews/simpletreemodel/treeitem.cpp 0
A pointer to each of the child items belonging to this item will be
- stored in the \c childItems private member variable. When the class's
- destructor is called, it must delete each of these to ensure that
- their memory is reused:
-
- \snippet itemviews/simpletreemodel/treeitem.cpp 1
+ stored in the \c childItems private member variable as an std::unique_ptr.
+ When the class's destructor is called, the child items will be automatically
+ deleted to ensure that their memory is reused:
Since each of the child items are constructed when the model is initially
populated with data, the function to add child items is straightforward:
- \snippet itemviews/simpletreemodel/treeitem.cpp 2
+ \snippet itemviews/simpletreemodel/treeitem.cpp 1
Each item is able to return any of its child items when given a suitable
row number. For example, in the \l{#SimpleTreeModelStructure}{above diagram},
@@ -124,11 +122,11 @@
The \c child() function returns the child that corresponds to
the specified row number in the item's list of child items:
- \snippet itemviews/simpletreemodel/treeitem.cpp 3
+ \snippet itemviews/simpletreemodel/treeitem.cpp 2
The number of child items held can be found with \c childCount():
- \snippet itemviews/simpletreemodel/treeitem.cpp 4
+ \snippet itemviews/simpletreemodel/treeitem.cpp 3
The \c TreeModel uses this function to determine the number of rows that
exist for a given parent item.
@@ -136,7 +134,7 @@
The \c row() function reports the item's location within its parent's
list of items:
- \snippet itemviews/simpletreemodel/treeitem.cpp 8
+ \snippet itemviews/simpletreemodel/treeitem.cpp 7
Note that, although the root item (with no parent item) is automatically
assigned a row number of 0, this information is never used by the model.
@@ -144,16 +142,16 @@
The number of columns of data in the item is trivially returned by the
\c columnCount() function.
- \snippet itemviews/simpletreemodel/treeitem.cpp 5
+ \snippet itemviews/simpletreemodel/treeitem.cpp 4
Column data is returned by the \c data() function. The bounds are checked
before accessing the container with the data:
- \snippet itemviews/simpletreemodel/treeitem.cpp 6
+ \snippet itemviews/simpletreemodel/treeitem.cpp 5
The item's parent is found with \c parent():
- \snippet itemviews/simpletreemodel/treeitem.cpp 7
+ \snippet itemviews/simpletreemodel/treeitem.cpp 6
Note that, since the root item in the model will not have a parent, this
function will return zero in that case. We need to ensure that the model
@@ -183,14 +181,16 @@
item only contains vertical header data for convenience. We also use it
to reference the internal data structure that contains the model data,
and it is used to represent an imaginary parent of top-level items in
- the model.
+ the model. The root item is managed with a std::unique_ptr to ensure the
+ entire tree of item is deleted when the model is deleted.
The model's internal data structure is populated with items by the
\c setupModelData() function. We will examine this function separately
at the end of this document.
The destructor ensures that the root item and all of its descendants
- are deleted when the model is destroyed:
+ are deleted when the model is destroyed. This is done automatically
+ since the root item is stored in a unique_ptr.
\snippet itemviews/simpletreemodel/treemodel.cpp 1