Cleanup SimpleTreeModel example

Cleanup the SimpleTreeModel example:
 - include own headers first
 - use nullptr
 - add sanity checks

Change-Id: If57d608e3919368b2022ff86aede8de9c2ba7369
Reviewed-by: Luca Beldi <v.ronin@yahoo.it>
Reviewed-by: Sze Howe Koh <szehowe.koh@gmail.com>
This commit is contained in:
Christian Ehrlicher 2018-11-23 20:05:42 +01:00
parent 7cc6f78dd4
commit c590aa678d
5 changed files with 31 additions and 37 deletions

View File

@ -170,9 +170,8 @@
\snippet itemviews/simpletreemodel/treeitem.cpp 5
Column data is returned by the \c data() function, taking advantage of
QList's ability to provide sensible default values if the column number
is out of range:
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

View File

@ -54,16 +54,12 @@
A container for items of data supplied by the simple tree model.
*/
#include <QStringList>
#include "treeitem.h"
//! [0]
TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent)
{
m_parentItem = parent;
m_itemData = data;
}
TreeItem::TreeItem(const QVector<QVariant> &data, TreeItem *parent)
: m_itemData(data), m_parentItem(parent)
{}
//! [0]
//! [1]
@ -83,7 +79,9 @@ void TreeItem::appendChild(TreeItem *item)
//! [3]
TreeItem *TreeItem::child(int row)
{
return m_childItems.value(row);
if (row < 0 || row >= m_childItems.size())
return nullptr;
return m_childItems.at(row);
}
//! [3]
@ -104,7 +102,9 @@ int TreeItem::columnCount() const
//! [6]
QVariant TreeItem::data(int column) const
{
return m_itemData.value(column);
if (column < 0 || column >= m_itemData.size())
return QVariant();
return m_itemData.at(column);
}
//! [6]

View File

@ -51,14 +51,14 @@
#ifndef TREEITEM_H
#define TREEITEM_H
#include <QList>
#include <QVariant>
#include <QVector>
//! [0]
class TreeItem
{
public:
explicit TreeItem(const QList<QVariant> &data, TreeItem *parentItem = 0);
explicit TreeItem(const QVector<QVariant> &data, TreeItem *parentItem = nullptr);
~TreeItem();
void appendChild(TreeItem *child);
@ -71,8 +71,8 @@ public:
TreeItem *parentItem();
private:
QList<TreeItem*> m_childItems;
QList<QVariant> m_itemData;
QVector<TreeItem*> m_childItems;
QVector<QVariant> m_itemData;
TreeItem *m_parentItem;
};
//! [0]

View File

@ -55,8 +55,8 @@
models.
*/
#include "treeitem.h"
#include "treemodel.h"
#include "treeitem.h"
#include <QStringList>
@ -64,10 +64,8 @@
TreeModel::TreeModel(const QString &data, QObject *parent)
: QAbstractItemModel(parent)
{
QList<QVariant> rootData;
rootData << "Title" << "Summary";
rootItem = new TreeItem(rootData);
setupModelData(data.split(QString("\n")), rootItem);
rootItem = new TreeItem({tr("Title"), tr("Summary")});
setupModelData(data.split('\n'), rootItem);
}
//! [0]
@ -83,8 +81,7 @@ int TreeModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
else
return rootItem->columnCount();
return rootItem->columnCount();
}
//! [2]
@ -107,7 +104,7 @@ QVariant TreeModel::data(const QModelIndex &index, int role) const
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
return Qt::NoItemFlags;
return QAbstractItemModel::flags(index);
}
@ -125,8 +122,7 @@ QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
//! [5]
//! [6]
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
const
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
@ -141,8 +137,7 @@ QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
TreeItem *childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
return QModelIndex();
}
//! [6]
@ -180,8 +175,8 @@ int TreeModel::rowCount(const QModelIndex &parent) const
void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
{
QList<TreeItem*> parents;
QList<int> indentations;
QVector<TreeItem*> parents;
QVector<int> indentations;
parents << parent;
indentations << 0;
@ -195,14 +190,15 @@ void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
position++;
}
QString lineData = lines[number].mid(position).trimmed();
const QString lineData = lines[number].mid(position).trimmed();
if (!lineData.isEmpty()) {
// Read the column data from the rest of the line.
QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
QList<QVariant> columnData;
for (int column = 0; column < columnStrings.count(); ++column)
columnData << columnStrings[column];
const QStringList columnStrings = lineData.split('\t', QString::SkipEmptyParts);
QVector<QVariant> columnData;
columnData.reserve(columnStrings.count());
for (const QString &columnString : columnStrings)
columnData << columnString;
if (position > indentations.last()) {
// The last child of the current parent is now the new parent
@ -222,7 +218,6 @@ void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
// Append a new item to the current parent's list of children.
parents.last()->appendChild(new TreeItem(columnData, parents.last()));
}
++number;
}
}

View File

@ -63,7 +63,7 @@ class TreeModel : public QAbstractItemModel
Q_OBJECT
public:
explicit TreeModel(const QString &data, QObject *parent = 0);
explicit TreeModel(const QString &data, QObject *parent = nullptr);
~TreeModel();
QVariant data(const QModelIndex &index, int role) const override;