Polish the SimpleTreeModel example
- Fix/silence most clang-tidy/compiler warnings * unsigned/int comparison * Avoid repeating return / default parameter types * Make functions static/use static invocations * Use string literals everywhere * Use auto * for pointers * Streamline code, use ternary operators * Move constructor parameters * Observe rule of 5 by using Q_DISABLE_COPY_MOVE - Add some bells && whistles, resize properly, expand all Complements 25027444a9b53d61a6257dc5f5ce0ffdb3b06f98. Pick-to: 6.6 Change-Id: I78f48d187981ecabf69a5d4d42715bad026fa9e6 Reviewed-by: Kai Köhne <kai.koehne@qt.io>
This commit is contained in:
parent
283cdcd3d5
commit
a3e20df03d
@ -145,8 +145,9 @@
|
||||
|
||||
\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:
|
||||
Column data is returned by the \c data() function. We use
|
||||
the QList::value() convenience function which checks the bounds
|
||||
and returns a default-constructed QVariant in case they are violated:
|
||||
|
||||
\snippet itemviews/simpletreemodel/treeitem.cpp 5
|
||||
|
||||
|
@ -5,20 +5,28 @@
|
||||
|
||||
#include <QApplication>
|
||||
#include <QFile>
|
||||
#include <QScreen>
|
||||
#include <QTreeView>
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
|
||||
QFile file(":/default.txt");
|
||||
file.open(QIODevice::ReadOnly);
|
||||
TreeModel model(file.readAll());
|
||||
QFile file(":/default.txt"_L1);
|
||||
file.open(QIODevice::ReadOnly | QIODevice::Text);
|
||||
TreeModel model(QString::fromUtf8(file.readAll()));
|
||||
file.close();
|
||||
|
||||
QTreeView view;
|
||||
view.setModel(&model);
|
||||
view.setWindowTitle(QObject::tr("Simple Tree Model"));
|
||||
view.setWindowTitle(TreeModel::tr("Simple Tree Model"));
|
||||
for (int c = 0; c < model.columnCount(); ++c)
|
||||
view.resizeColumnToContents(c);
|
||||
view.expandAll();
|
||||
const auto screenSize = view.screen()->availableSize();
|
||||
view.resize({screenSize.width() / 2, screenSize.height() * 2 / 3});
|
||||
view.show();
|
||||
return app.exec();
|
||||
return QCoreApplication::exec();
|
||||
}
|
||||
|
@ -10,8 +10,8 @@
|
||||
#include "treeitem.h"
|
||||
|
||||
//! [0]
|
||||
TreeItem::TreeItem(const QVariantList &data, TreeItem *parent)
|
||||
: m_itemData(data), m_parentItem(parent)
|
||||
TreeItem::TreeItem(QVariantList data, TreeItem *parent)
|
||||
: m_itemData(std::move(data)), m_parentItem(parent)
|
||||
{}
|
||||
//! [0]
|
||||
|
||||
@ -25,32 +25,28 @@ void TreeItem::appendChild(std::unique_ptr<TreeItem> &&child)
|
||||
//! [2]
|
||||
TreeItem *TreeItem::child(int row)
|
||||
{
|
||||
if (row < 0 || row >= m_childItems.size())
|
||||
return nullptr;
|
||||
return m_childItems.at(row).get();
|
||||
return row >= 0 && row < childCount() ? m_childItems.at(row).get() : nullptr;
|
||||
}
|
||||
//! [2]
|
||||
|
||||
//! [3]
|
||||
int TreeItem::childCount() const
|
||||
{
|
||||
return m_childItems.size();
|
||||
return int(m_childItems.size());
|
||||
}
|
||||
//! [3]
|
||||
|
||||
//! [4]
|
||||
int TreeItem::columnCount() const
|
||||
{
|
||||
return m_itemData.count();
|
||||
return int(m_itemData.count());
|
||||
}
|
||||
//! [4]
|
||||
|
||||
//! [5]
|
||||
QVariant TreeItem::data(int column) const
|
||||
{
|
||||
if (column < 0 || column >= m_itemData.size())
|
||||
return QVariant();
|
||||
return m_itemData.at(column);
|
||||
return m_itemData.value(column);
|
||||
}
|
||||
//! [5]
|
||||
|
||||
@ -64,11 +60,11 @@ TreeItem *TreeItem::parentItem()
|
||||
//! [7]
|
||||
int TreeItem::row() const
|
||||
{
|
||||
if (!m_parentItem)
|
||||
if (m_parentItem == nullptr)
|
||||
return 0;
|
||||
const auto it = std::find_if(m_parentItem->m_childItems.cbegin(), m_parentItem->m_childItems.cend(),
|
||||
[this](const std::unique_ptr<TreeItem> &treeItem) {
|
||||
return treeItem.get() == const_cast<TreeItem *>(this);
|
||||
return treeItem.get() == this;
|
||||
});
|
||||
|
||||
if (it != m_parentItem->m_childItems.cend())
|
||||
|
@ -11,7 +11,7 @@
|
||||
class TreeItem
|
||||
{
|
||||
public:
|
||||
explicit TreeItem(const QVariantList &data, TreeItem *parentItem = nullptr);
|
||||
explicit TreeItem(QVariantList data, TreeItem *parentItem = nullptr);
|
||||
|
||||
void appendChild(std::unique_ptr<TreeItem> &&child);
|
||||
|
||||
|
@ -40,14 +40,10 @@ int TreeModel::columnCount(const QModelIndex &parent) const
|
||||
//! [3]
|
||||
QVariant TreeModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid())
|
||||
return QVariant();
|
||||
|
||||
if (role != Qt::DisplayRole)
|
||||
return QVariant();
|
||||
|
||||
auto item = static_cast<TreeItem*>(index.internalPointer());
|
||||
if (!index.isValid() || role != Qt::DisplayRole)
|
||||
return {};
|
||||
|
||||
const auto *item = static_cast<const TreeItem*>(index.internalPointer());
|
||||
return item->data(index.column());
|
||||
}
|
||||
//! [3]
|
||||
@ -55,10 +51,8 @@ QVariant TreeModel::data(const QModelIndex &index, int role) const
|
||||
//! [4]
|
||||
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
|
||||
{
|
||||
if (!index.isValid())
|
||||
return Qt::NoItemFlags;
|
||||
|
||||
return QAbstractItemModel::flags(index);
|
||||
return index.isValid()
|
||||
? QAbstractItemModel::flags(index) : Qt::ItemFlags(Qt::NoItemFlags);
|
||||
}
|
||||
//! [4]
|
||||
|
||||
@ -66,10 +60,8 @@ Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
|
||||
QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
|
||||
int role) const
|
||||
{
|
||||
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
|
||||
return rootItem->data(section);
|
||||
|
||||
return QVariant();
|
||||
return orientation == Qt::Horizontal && role == Qt::DisplayRole
|
||||
? rootItem->data(section) : QVariant{};
|
||||
}
|
||||
//! [5]
|
||||
|
||||
@ -77,19 +69,15 @@ QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
|
||||
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
|
||||
{
|
||||
if (!hasIndex(row, column, parent))
|
||||
return QModelIndex();
|
||||
return {};
|
||||
|
||||
TreeItem *parentItem;
|
||||
TreeItem *parentItem = parent.isValid()
|
||||
? static_cast<TreeItem*>(parent.internalPointer())
|
||||
: rootItem.get();
|
||||
|
||||
if (!parent.isValid())
|
||||
parentItem = rootItem.get();
|
||||
else
|
||||
parentItem = static_cast<TreeItem*>(parent.internalPointer());
|
||||
|
||||
auto childItem = parentItem->child(row);
|
||||
if (childItem)
|
||||
if (auto *childItem = parentItem->child(row))
|
||||
return createIndex(row, column, childItem);
|
||||
return QModelIndex();
|
||||
return {};
|
||||
}
|
||||
//! [6]
|
||||
|
||||
@ -97,29 +85,25 @@ QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) con
|
||||
QModelIndex TreeModel::parent(const QModelIndex &index) const
|
||||
{
|
||||
if (!index.isValid())
|
||||
return QModelIndex();
|
||||
return {};
|
||||
|
||||
TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
|
||||
auto *childItem = static_cast<TreeItem*>(index.internalPointer());
|
||||
TreeItem *parentItem = childItem->parentItem();
|
||||
|
||||
if (parentItem == rootItem.get())
|
||||
return QModelIndex();
|
||||
|
||||
return createIndex(parentItem->row(), 0, parentItem);
|
||||
return parentItem != rootItem.get()
|
||||
? createIndex(parentItem->row(), 0, parentItem) : QModelIndex{};
|
||||
}
|
||||
//! [7]
|
||||
|
||||
//! [8]
|
||||
int TreeModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
TreeItem *parentItem;
|
||||
if (parent.column() > 0)
|
||||
return 0;
|
||||
|
||||
if (!parent.isValid())
|
||||
parentItem = rootItem.get();
|
||||
else
|
||||
parentItem = static_cast<TreeItem*>(parent.internalPointer());
|
||||
const TreeItem *parentItem = parent.isValid()
|
||||
? static_cast<const TreeItem*>(parent.internalPointer())
|
||||
: rootItem.get();
|
||||
|
||||
return parentItem->childCount();
|
||||
}
|
||||
|
@ -16,21 +16,23 @@ class TreeModel : public QAbstractItemModel
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Q_DISABLE_COPY_MOVE(TreeModel)
|
||||
|
||||
explicit TreeModel(const QString &data, QObject *parent = nullptr);
|
||||
~TreeModel();
|
||||
~TreeModel() override;
|
||||
|
||||
QVariant data(const QModelIndex &index, int role) const override;
|
||||
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||
QVariant headerData(int section, Qt::Orientation orientation,
|
||||
int role = Qt::DisplayRole) const override;
|
||||
QModelIndex index(int row, int column,
|
||||
const QModelIndex &parent = QModelIndex()) const override;
|
||||
const QModelIndex &parent = {}) const override;
|
||||
QModelIndex parent(const QModelIndex &index) const override;
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
int rowCount(const QModelIndex &parent = {}) const override;
|
||||
int columnCount(const QModelIndex &parent = {}) const override;
|
||||
|
||||
private:
|
||||
void setupModelData(const QStringList &lines, TreeItem *parent);
|
||||
static void setupModelData(const QStringList &lines, TreeItem *parent);
|
||||
|
||||
std::unique_ptr<TreeItem> rootItem;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user