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

View File

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

View File

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

View File

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

View File

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

View File

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