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:
parent
7cc6f78dd4
commit
c590aa678d
@ -170,9 +170,8 @@
|
|||||||
|
|
||||||
\snippet itemviews/simpletreemodel/treeitem.cpp 5
|
\snippet itemviews/simpletreemodel/treeitem.cpp 5
|
||||||
|
|
||||||
Column data is returned by the \c data() function, taking advantage of
|
Column data is returned by the \c data() function. The bounds are checked
|
||||||
QList's ability to provide sensible default values if the column number
|
before accessing the container with the data:
|
||||||
is out of range:
|
|
||||||
|
|
||||||
\snippet itemviews/simpletreemodel/treeitem.cpp 6
|
\snippet itemviews/simpletreemodel/treeitem.cpp 6
|
||||||
|
|
||||||
|
@ -54,16 +54,12 @@
|
|||||||
A container for items of data supplied by the simple tree model.
|
A container for items of data supplied by the simple tree model.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <QStringList>
|
|
||||||
|
|
||||||
#include "treeitem.h"
|
#include "treeitem.h"
|
||||||
|
|
||||||
//! [0]
|
//! [0]
|
||||||
TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent)
|
TreeItem::TreeItem(const QVector<QVariant> &data, TreeItem *parent)
|
||||||
{
|
: m_itemData(data), m_parentItem(parent)
|
||||||
m_parentItem = parent;
|
{}
|
||||||
m_itemData = data;
|
|
||||||
}
|
|
||||||
//! [0]
|
//! [0]
|
||||||
|
|
||||||
//! [1]
|
//! [1]
|
||||||
@ -83,7 +79,9 @@ void TreeItem::appendChild(TreeItem *item)
|
|||||||
//! [3]
|
//! [3]
|
||||||
TreeItem *TreeItem::child(int row)
|
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]
|
//! [3]
|
||||||
|
|
||||||
@ -104,7 +102,9 @@ int TreeItem::columnCount() const
|
|||||||
//! [6]
|
//! [6]
|
||||||
QVariant TreeItem::data(int column) const
|
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]
|
//! [6]
|
||||||
|
|
||||||
|
@ -51,14 +51,14 @@
|
|||||||
#ifndef TREEITEM_H
|
#ifndef TREEITEM_H
|
||||||
#define TREEITEM_H
|
#define TREEITEM_H
|
||||||
|
|
||||||
#include <QList>
|
|
||||||
#include <QVariant>
|
#include <QVariant>
|
||||||
|
#include <QVector>
|
||||||
|
|
||||||
//! [0]
|
//! [0]
|
||||||
class TreeItem
|
class TreeItem
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit TreeItem(const QList<QVariant> &data, TreeItem *parentItem = 0);
|
explicit TreeItem(const QVector<QVariant> &data, TreeItem *parentItem = nullptr);
|
||||||
~TreeItem();
|
~TreeItem();
|
||||||
|
|
||||||
void appendChild(TreeItem *child);
|
void appendChild(TreeItem *child);
|
||||||
@ -71,8 +71,8 @@ public:
|
|||||||
TreeItem *parentItem();
|
TreeItem *parentItem();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QList<TreeItem*> m_childItems;
|
QVector<TreeItem*> m_childItems;
|
||||||
QList<QVariant> m_itemData;
|
QVector<QVariant> m_itemData;
|
||||||
TreeItem *m_parentItem;
|
TreeItem *m_parentItem;
|
||||||
};
|
};
|
||||||
//! [0]
|
//! [0]
|
||||||
|
@ -55,8 +55,8 @@
|
|||||||
models.
|
models.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "treeitem.h"
|
|
||||||
#include "treemodel.h"
|
#include "treemodel.h"
|
||||||
|
#include "treeitem.h"
|
||||||
|
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
|
|
||||||
@ -64,10 +64,8 @@
|
|||||||
TreeModel::TreeModel(const QString &data, QObject *parent)
|
TreeModel::TreeModel(const QString &data, QObject *parent)
|
||||||
: QAbstractItemModel(parent)
|
: QAbstractItemModel(parent)
|
||||||
{
|
{
|
||||||
QList<QVariant> rootData;
|
rootItem = new TreeItem({tr("Title"), tr("Summary")});
|
||||||
rootData << "Title" << "Summary";
|
setupModelData(data.split('\n'), rootItem);
|
||||||
rootItem = new TreeItem(rootData);
|
|
||||||
setupModelData(data.split(QString("\n")), rootItem);
|
|
||||||
}
|
}
|
||||||
//! [0]
|
//! [0]
|
||||||
|
|
||||||
@ -83,8 +81,7 @@ int TreeModel::columnCount(const QModelIndex &parent) const
|
|||||||
{
|
{
|
||||||
if (parent.isValid())
|
if (parent.isValid())
|
||||||
return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
|
return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
|
||||||
else
|
return rootItem->columnCount();
|
||||||
return rootItem->columnCount();
|
|
||||||
}
|
}
|
||||||
//! [2]
|
//! [2]
|
||||||
|
|
||||||
@ -107,7 +104,7 @@ QVariant TreeModel::data(const QModelIndex &index, int role) const
|
|||||||
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
|
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
|
||||||
{
|
{
|
||||||
if (!index.isValid())
|
if (!index.isValid())
|
||||||
return 0;
|
return Qt::NoItemFlags;
|
||||||
|
|
||||||
return QAbstractItemModel::flags(index);
|
return QAbstractItemModel::flags(index);
|
||||||
}
|
}
|
||||||
@ -125,8 +122,7 @@ QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
|
|||||||
//! [5]
|
//! [5]
|
||||||
|
|
||||||
//! [6]
|
//! [6]
|
||||||
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
|
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
|
||||||
const
|
|
||||||
{
|
{
|
||||||
if (!hasIndex(row, column, parent))
|
if (!hasIndex(row, column, parent))
|
||||||
return QModelIndex();
|
return QModelIndex();
|
||||||
@ -141,8 +137,7 @@ QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
|
|||||||
TreeItem *childItem = parentItem->child(row);
|
TreeItem *childItem = parentItem->child(row);
|
||||||
if (childItem)
|
if (childItem)
|
||||||
return createIndex(row, column, childItem);
|
return createIndex(row, column, childItem);
|
||||||
else
|
return QModelIndex();
|
||||||
return QModelIndex();
|
|
||||||
}
|
}
|
||||||
//! [6]
|
//! [6]
|
||||||
|
|
||||||
@ -180,8 +175,8 @@ int TreeModel::rowCount(const QModelIndex &parent) const
|
|||||||
|
|
||||||
void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
|
void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
|
||||||
{
|
{
|
||||||
QList<TreeItem*> parents;
|
QVector<TreeItem*> parents;
|
||||||
QList<int> indentations;
|
QVector<int> indentations;
|
||||||
parents << parent;
|
parents << parent;
|
||||||
indentations << 0;
|
indentations << 0;
|
||||||
|
|
||||||
@ -195,14 +190,15 @@ void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
|
|||||||
position++;
|
position++;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString lineData = lines[number].mid(position).trimmed();
|
const QString lineData = lines[number].mid(position).trimmed();
|
||||||
|
|
||||||
if (!lineData.isEmpty()) {
|
if (!lineData.isEmpty()) {
|
||||||
// Read the column data from the rest of the line.
|
// Read the column data from the rest of the line.
|
||||||
QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
|
const QStringList columnStrings = lineData.split('\t', QString::SkipEmptyParts);
|
||||||
QList<QVariant> columnData;
|
QVector<QVariant> columnData;
|
||||||
for (int column = 0; column < columnStrings.count(); ++column)
|
columnData.reserve(columnStrings.count());
|
||||||
columnData << columnStrings[column];
|
for (const QString &columnString : columnStrings)
|
||||||
|
columnData << columnString;
|
||||||
|
|
||||||
if (position > indentations.last()) {
|
if (position > indentations.last()) {
|
||||||
// The last child of the current parent is now the new parent
|
// 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.
|
// Append a new item to the current parent's list of children.
|
||||||
parents.last()->appendChild(new TreeItem(columnData, parents.last()));
|
parents.last()->appendChild(new TreeItem(columnData, parents.last()));
|
||||||
}
|
}
|
||||||
|
|
||||||
++number;
|
++number;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ class TreeModel : public QAbstractItemModel
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit TreeModel(const QString &data, QObject *parent = 0);
|
explicit TreeModel(const QString &data, QObject *parent = nullptr);
|
||||||
~TreeModel();
|
~TreeModel();
|
||||||
|
|
||||||
QVariant data(const QModelIndex &index, int role) const override;
|
QVariant data(const QModelIndex &index, int role) const override;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user