Polish the EditableTreeModel example

- Fix/silence most clang-tidy/compiler warnings
  * unsigned/int comparison
  * Avoid repeating return / default parameter types
  * 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

Change-Id: I1721458ff66dcba123ff3a2cf50e7d15387a4e8b
Reviewed-by: Kai Köhne <kai.koehne@qt.io>
(cherry picked from commit d50b56b47544ad2b72811045f64df24eaaf0cf59)
This commit is contained in:
Friedemann Kleint 2023-11-28 13:42:00 +01:00
parent 48c78b2a3a
commit c75746a10a
6 changed files with 41 additions and 44 deletions

View File

@ -4,11 +4,14 @@
#include "mainwindow.h"
#include <QApplication>
#include <QScreen>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow window;
const auto screenSize = window.screen()->availableSize();
window.resize({screenSize.width() / 2, screenSize.height() * 2 / 3});
window.show();
return app.exec();
return QCoreApplication::exec();
}

View File

@ -6,6 +6,8 @@
#include <QFile>
using namespace Qt::StringLiterals;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
@ -13,14 +15,15 @@ MainWindow::MainWindow(QWidget *parent)
const QStringList headers({tr("Title"), tr("Description")});
QFile file(":/default.txt");
file.open(QIODevice::ReadOnly);
TreeModel *model = new TreeModel(headers, file.readAll(), this);
QFile file(":/default.txt"_L1);
file.open(QIODevice::ReadOnly | QIODevice::Text);
auto *model = new TreeModel(headers, QString::fromUtf8(file.readAll()), this);
file.close();
view->setModel(model);
for (int column = 0; column < model->columnCount(); ++column)
view->resizeColumnToContents(column);
view->expandAll();
connect(exitAction, &QAction::triggered, qApp, &QCoreApplication::quit);

View File

@ -10,24 +10,23 @@
#include "treeitem.h"
//! [0]
TreeItem::TreeItem(const QVariantList &data, TreeItem *parent)
: itemData(data), m_parentItem(parent)
TreeItem::TreeItem(QVariantList data, TreeItem *parent)
: itemData(std::move(data)), m_parentItem(parent)
{}
//! [0]
//! [1]
TreeItem *TreeItem::child(int number)
{
if (number < 0 || number >= qsizetype(m_childItems.size()))
return nullptr;
return m_childItems.at(number).get();
return (number >= 0 && number < childCount())
? m_childItems.at(number).get() : nullptr;
}
//! [1]
//! [2]
int TreeItem::childCount() const
{
return m_childItems.size();
return int(m_childItems.size());
}
//! [2]
@ -38,7 +37,7 @@ int TreeItem::row() const
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())
@ -51,16 +50,14 @@ int TreeItem::row() const
//! [4]
int TreeItem::columnCount() const
{
return itemData.count();
return int(itemData.count());
}
//! [4]
//! [5]
QVariant TreeItem::data(int column) const
{
if (column < 0 || column >= itemData.size())
return QVariant();
return itemData.at(column);
return itemData.value(column);
}
//! [5]

View File

@ -11,7 +11,7 @@
class TreeItem
{
public:
explicit TreeItem(const QVariantList &data, TreeItem *parent = nullptr);
explicit TreeItem(QVariantList data, TreeItem *parent = nullptr);
TreeItem *child(int number);
int childCount() const;

View File

@ -4,8 +4,6 @@
#include "treemodel.h"
#include "treeitem.h"
#include <QtWidgets>
using namespace Qt::StringLiterals;
//! [0]
@ -36,10 +34,10 @@ int TreeModel::columnCount(const QModelIndex &parent) const
QVariant TreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
return {};
if (role != Qt::DisplayRole && role != Qt::EditRole)
return QVariant();
return {};
TreeItem *item = getItem(index);
@ -60,8 +58,7 @@ Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
TreeItem *TreeModel::getItem(const QModelIndex &index) const
{
if (index.isValid()) {
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
if (item)
if (auto *item = static_cast<TreeItem*>(index.internalPointer()))
return item;
}
return rootItem.get();
@ -71,28 +68,25 @@ TreeItem *TreeModel::getItem(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]
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
{
if (parent.isValid() && parent.column() != 0)
return QModelIndex();
return {};
//! [5]
//! [6]
TreeItem *parentItem = getItem(parent);
if (!parentItem)
return QModelIndex();
return {};
TreeItem *childItem = parentItem->child(row);
if (childItem)
if (auto *childItem = parentItem->child(row))
return createIndex(row, column, childItem);
return QModelIndex();
return {};
}
//! [6]
@ -124,15 +118,13 @@ bool TreeModel::insertRows(int position, int rows, const QModelIndex &parent)
QModelIndex TreeModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
return {};
TreeItem *childItem = getItem(index);
TreeItem *parentItem = childItem ? childItem->parent() : nullptr;
if (parentItem == rootItem.get() || !parentItem)
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
return (parentItem != rootItem.get() && parentItem != nullptr)
? createIndex(parentItem->row(), 0, parentItem) : QModelIndex{};
}
//! [7]

View File

@ -16,9 +16,11 @@ class TreeModel : public QAbstractItemModel
Q_OBJECT
public:
Q_DISABLE_COPY_MOVE(TreeModel)
TreeModel(const QStringList &headers, const QString &data,
QObject *parent = nullptr);
~TreeModel();
~TreeModel() override;
//! [0] //! [1]
QVariant data(const QModelIndex &index, int role) const override;
@ -26,11 +28,11 @@ public:
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;
//! [1]
//! [2]
@ -41,13 +43,13 @@ public:
const QVariant &value, int role = Qt::EditRole) override;
bool insertColumns(int position, int columns,
const QModelIndex &parent = QModelIndex()) override;
const QModelIndex &parent = {}) override;
bool removeColumns(int position, int columns,
const QModelIndex &parent = QModelIndex()) override;
const QModelIndex &parent = {}) override;
bool insertRows(int position, int rows,
const QModelIndex &parent = QModelIndex()) override;
const QModelIndex &parent = {}) override;
bool removeRows(int position, int rows,
const QModelIndex &parent = QModelIndex()) override;
const QModelIndex &parent = {}) override;
private:
void setupModelData(const QStringList &lines);