From 8c685b765bf4ceba3c4cf8fdd9c9d680f338b7a9 Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Sat, 20 Oct 2018 21:48:28 +0200 Subject: Itemviews: Cleanup examples MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cleanup some minor issues in the chart example: - remove unused members - use initializer list for members - pass a proper role to dataChanged() - honor roles parameter in PieView::dataChanged() - use nullptr instead 0 - use new-style connect - fix indentation and other whitespaces Change-Id: Idb212b07c006fe3ae31bee9cd9b1ba4d03043b5e Reviewed-by: André Hartmann Reviewed-by: Paul Wicking --- examples/widgets/itemviews/editabletreemodel/treemodel.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/widgets/itemviews/editabletreemodel') diff --git a/examples/widgets/itemviews/editabletreemodel/treemodel.cpp b/examples/widgets/itemviews/editabletreemodel/treemodel.cpp index dbd53df1e6..72818372d7 100644 --- a/examples/widgets/itemviews/editabletreemodel/treemodel.cpp +++ b/examples/widgets/itemviews/editabletreemodel/treemodel.cpp @@ -225,7 +225,7 @@ bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int rol bool result = item->setData(index.column(), value); if (result) - emit dataChanged(index, index); + emit dataChanged(index, index, {role}); return result; } -- cgit v1.2.3 From e0e22d56ac4a37a57d02383747fbacccaaa407bd Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Fri, 23 Nov 2018 19:45:56 +0100 Subject: Cleanup EditableItemModel example Cleanup the EditableItemModel example: - include own headers first - use nullptr - add sanity checks - use for loop instead foreach - use const where possible Change-Id: Ib36e5710c07979576b48a905ee50908a64dcb697 Reviewed-by: Paul Wicking --- .../widgets/itemviews/editabletreemodel/main.cpp | 4 +- .../itemviews/editabletreemodel/mainwindow.cpp | 32 +++++------ .../itemviews/editabletreemodel/mainwindow.h | 3 +- .../itemviews/editabletreemodel/treeitem.cpp | 22 ++++---- .../widgets/itemviews/editabletreemodel/treeitem.h | 5 +- .../itemviews/editabletreemodel/treemodel.cpp | 66 +++++++++++----------- .../itemviews/editabletreemodel/treemodel.h | 2 +- 7 files changed, 66 insertions(+), 68 deletions(-) (limited to 'examples/widgets/itemviews/editabletreemodel') diff --git a/examples/widgets/itemviews/editabletreemodel/main.cpp b/examples/widgets/itemviews/editabletreemodel/main.cpp index 80f6842730..5e9ff214a3 100644 --- a/examples/widgets/itemviews/editabletreemodel/main.cpp +++ b/examples/widgets/itemviews/editabletreemodel/main.cpp @@ -48,10 +48,10 @@ ** ****************************************************************************/ -#include - #include "mainwindow.h" +#include + int main(int argc, char *argv[]) { Q_INIT_RESOURCE(editabletreemodel); diff --git a/examples/widgets/itemviews/editabletreemodel/mainwindow.cpp b/examples/widgets/itemviews/editabletreemodel/mainwindow.cpp index 0c54d8930f..7faa325469 100644 --- a/examples/widgets/itemviews/editabletreemodel/mainwindow.cpp +++ b/examples/widgets/itemviews/editabletreemodel/mainwindow.cpp @@ -58,8 +58,7 @@ MainWindow::MainWindow(QWidget *parent) { setupUi(this); - QStringList headers; - headers << tr("Title") << tr("Description"); + const QStringList headers({tr("Title"), tr("Description")}); QFile file(":/default.txt"); file.open(QIODevice::ReadOnly); @@ -87,7 +86,7 @@ MainWindow::MainWindow(QWidget *parent) void MainWindow::insertChild() { - QModelIndex index = view->selectionModel()->currentIndex(); + const QModelIndex index = view->selectionModel()->currentIndex(); QAbstractItemModel *model = view->model(); if (model->columnCount(index) == 0) { @@ -99,10 +98,10 @@ void MainWindow::insertChild() return; for (int column = 0; column < model->columnCount(index); ++column) { - QModelIndex child = model->index(0, column, index); - model->setData(child, QVariant("[No data]"), Qt::EditRole); + const QModelIndex child = model->index(0, column, index); + model->setData(child, QVariant(tr("[No data]")), Qt::EditRole); if (!model->headerData(column, Qt::Horizontal).isValid()) - model->setHeaderData(column, Qt::Horizontal, QVariant("[No header]"), Qt::EditRole); + model->setHeaderData(column, Qt::Horizontal, QVariant(tr("[No header]")), Qt::EditRole); } view->selectionModel()->setCurrentIndex(model->index(0, 0, index), @@ -127,7 +126,7 @@ bool MainWindow::insertColumn() void MainWindow::insertRow() { - QModelIndex index = view->selectionModel()->currentIndex(); + const QModelIndex index = view->selectionModel()->currentIndex(); QAbstractItemModel *model = view->model(); if (!model->insertRow(index.row()+1, index.parent())) @@ -136,19 +135,18 @@ void MainWindow::insertRow() updateActions(); for (int column = 0; column < model->columnCount(index.parent()); ++column) { - QModelIndex child = model->index(index.row()+1, column, index.parent()); - model->setData(child, QVariant("[No data]"), Qt::EditRole); + const QModelIndex child = model->index(index.row() + 1, column, index.parent()); + model->setData(child, QVariant(tr("[No data]")), Qt::EditRole); } } bool MainWindow::removeColumn() { QAbstractItemModel *model = view->model(); - int column = view->selectionModel()->currentIndex().column(); + const int column = view->selectionModel()->currentIndex().column(); // Insert columns in each child of the parent item. - bool changed = model->removeColumn(column); - + const bool changed = model->removeColumn(column); if (changed) updateActions(); @@ -157,7 +155,7 @@ bool MainWindow::removeColumn() void MainWindow::removeRow() { - QModelIndex index = view->selectionModel()->currentIndex(); + const QModelIndex index = view->selectionModel()->currentIndex(); QAbstractItemModel *model = view->model(); if (model->removeRow(index.row(), index.parent())) updateActions(); @@ -165,19 +163,19 @@ void MainWindow::removeRow() void MainWindow::updateActions() { - bool hasSelection = !view->selectionModel()->selection().isEmpty(); + const bool hasSelection = !view->selectionModel()->selection().isEmpty(); removeRowAction->setEnabled(hasSelection); removeColumnAction->setEnabled(hasSelection); - bool hasCurrent = view->selectionModel()->currentIndex().isValid(); + const bool hasCurrent = view->selectionModel()->currentIndex().isValid(); insertRowAction->setEnabled(hasCurrent); insertColumnAction->setEnabled(hasCurrent); if (hasCurrent) { view->closePersistentEditor(view->selectionModel()->currentIndex()); - int row = view->selectionModel()->currentIndex().row(); - int column = view->selectionModel()->currentIndex().column(); + const int row = view->selectionModel()->currentIndex().row(); + const int column = view->selectionModel()->currentIndex().column(); if (view->selectionModel()->currentIndex().parent().isValid()) statusBar()->showMessage(tr("Position: (%1,%2)").arg(row).arg(column)); else diff --git a/examples/widgets/itemviews/editabletreemodel/mainwindow.h b/examples/widgets/itemviews/editabletreemodel/mainwindow.h index dbc745e25d..314b0cb1fa 100644 --- a/examples/widgets/itemviews/editabletreemodel/mainwindow.h +++ b/examples/widgets/itemviews/editabletreemodel/mainwindow.h @@ -54,14 +54,13 @@ #include "ui_mainwindow.h" #include -#include class MainWindow : public QMainWindow, private Ui::MainWindow { Q_OBJECT public: - MainWindow(QWidget *parent = 0); + MainWindow(QWidget *parent = nullptr); public slots: void updateActions(); diff --git a/examples/widgets/itemviews/editabletreemodel/treeitem.cpp b/examples/widgets/itemviews/editabletreemodel/treeitem.cpp index 8fbe86ad1e..027a5f8849 100644 --- a/examples/widgets/itemviews/editabletreemodel/treeitem.cpp +++ b/examples/widgets/itemviews/editabletreemodel/treeitem.cpp @@ -56,14 +56,11 @@ #include "treeitem.h" -#include - //! [0] TreeItem::TreeItem(const QVector &data, TreeItem *parent) -{ - parentItem = parent; - itemData = data; -} + : itemData(data), + parentItem(parent) +{} //! [0] //! [1] @@ -76,7 +73,9 @@ TreeItem::~TreeItem() //! [2] TreeItem *TreeItem::child(int number) { - return childItems.value(number); + if (number < 0 || number >= childItems.size()) + return nullptr; + return childItems.at(number); } //! [2] @@ -92,7 +91,6 @@ int TreeItem::childNumber() const { if (parentItem) return parentItem->childItems.indexOf(const_cast(this)); - return 0; } //! [4] @@ -107,7 +105,9 @@ int TreeItem::columnCount() const //! [6] QVariant TreeItem::data(int column) const { - return itemData.value(column); + if (column < 0 || column >= itemData.size()) + return QVariant(); + return itemData.at(column); } //! [6] @@ -136,7 +136,7 @@ bool TreeItem::insertColumns(int position, int columns) for (int column = 0; column < columns; ++column) itemData.insert(position, QVariant()); - foreach (TreeItem *child, childItems) + for (TreeItem *child : qAsConst(childItems)) child->insertColumns(position, columns); return true; @@ -171,7 +171,7 @@ bool TreeItem::removeColumns(int position, int columns) for (int column = 0; column < columns; ++column) itemData.remove(position); - foreach (TreeItem *child, childItems) + for (TreeItem *child : qAsConst(childItems)) child->removeColumns(position, columns); return true; diff --git a/examples/widgets/itemviews/editabletreemodel/treeitem.h b/examples/widgets/itemviews/editabletreemodel/treeitem.h index fd65f12268..867be5f380 100644 --- a/examples/widgets/itemviews/editabletreemodel/treeitem.h +++ b/examples/widgets/itemviews/editabletreemodel/treeitem.h @@ -51,7 +51,6 @@ #ifndef TREEITEM_H #define TREEITEM_H -#include #include #include @@ -59,7 +58,7 @@ class TreeItem { public: - explicit TreeItem(const QVector &data, TreeItem *parent = 0); + explicit TreeItem(const QVector &data, TreeItem *parent = nullptr); ~TreeItem(); TreeItem *child(int number); @@ -75,7 +74,7 @@ public: bool setData(int column, const QVariant &value); private: - QList childItems; + QVector childItems; QVector itemData; TreeItem *parentItem; }; diff --git a/examples/widgets/itemviews/editabletreemodel/treemodel.cpp b/examples/widgets/itemviews/editabletreemodel/treemodel.cpp index 72818372d7..836c2e8651 100644 --- a/examples/widgets/itemviews/editabletreemodel/treemodel.cpp +++ b/examples/widgets/itemviews/editabletreemodel/treemodel.cpp @@ -48,21 +48,21 @@ ** ****************************************************************************/ -#include - -#include "treeitem.h" #include "treemodel.h" +#include "treeitem.h" + +#include //! [0] TreeModel::TreeModel(const QStringList &headers, const QString &data, QObject *parent) : QAbstractItemModel(parent) { QVector rootData; - foreach (QString header, headers) + for (const QString &header : headers) rootData << header; rootItem = new TreeItem(rootData); - setupModelData(data.split(QString("\n")), rootItem); + setupModelData(data.split('\n'), rootItem); } //! [0] @@ -74,8 +74,9 @@ TreeModel::~TreeModel() //! [1] //! [2] -int TreeModel::columnCount(const QModelIndex & /* parent */) const +int TreeModel::columnCount(const QModelIndex &parent) const { + Q_UNUSED(parent); return rootItem->columnCount(); } //! [2] @@ -97,7 +98,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 Qt::ItemIsEditable | QAbstractItemModel::flags(index); } @@ -133,21 +134,20 @@ QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) con //! [6] TreeItem *parentItem = getItem(parent); + if (!parentItem) + return QModelIndex(); TreeItem *childItem = parentItem->child(row); if (childItem) return createIndex(row, column, childItem); - else - return QModelIndex(); + return QModelIndex(); } //! [6] bool TreeModel::insertColumns(int position, int columns, const QModelIndex &parent) { - bool success; - beginInsertColumns(parent, position, position + columns - 1); - success = rootItem->insertColumns(position, columns); + const bool success = rootItem->insertColumns(position, columns); endInsertColumns(); return success; @@ -156,10 +156,13 @@ bool TreeModel::insertColumns(int position, int columns, const QModelIndex &pare bool TreeModel::insertRows(int position, int rows, const QModelIndex &parent) { TreeItem *parentItem = getItem(parent); - bool success; + if (!parentItem) + return false; beginInsertRows(parent, position, position + rows - 1); - success = parentItem->insertChildren(position, rows, rootItem->columnCount()); + const bool success = parentItem->insertChildren(position, + rows, + rootItem->columnCount()); endInsertRows(); return success; @@ -172,9 +175,9 @@ QModelIndex TreeModel::parent(const QModelIndex &index) const return QModelIndex(); TreeItem *childItem = getItem(index); - TreeItem *parentItem = childItem->parent(); + TreeItem *parentItem = childItem ? childItem->parent() : nullptr; - if (parentItem == rootItem) + if (parentItem == rootItem || !parentItem) return QModelIndex(); return createIndex(parentItem->childNumber(), 0, parentItem); @@ -183,10 +186,8 @@ QModelIndex TreeModel::parent(const QModelIndex &index) const bool TreeModel::removeColumns(int position, int columns, const QModelIndex &parent) { - bool success; - beginRemoveColumns(parent, position, position + columns - 1); - success = rootItem->removeColumns(position, columns); + const bool success = rootItem->removeColumns(position, columns); endRemoveColumns(); if (rootItem->columnCount() == 0) @@ -198,10 +199,11 @@ bool TreeModel::removeColumns(int position, int columns, const QModelIndex &pare bool TreeModel::removeRows(int position, int rows, const QModelIndex &parent) { TreeItem *parentItem = getItem(parent); - bool success = true; + if (!parentItem) + return false; beginRemoveRows(parent, position, position + rows - 1); - success = parentItem->removeChildren(position, rows); + const bool success = parentItem->removeChildren(position, rows); endRemoveRows(); return success; @@ -210,9 +212,9 @@ bool TreeModel::removeRows(int position, int rows, const QModelIndex &parent) //! [8] int TreeModel::rowCount(const QModelIndex &parent) const { - TreeItem *parentItem = getItem(parent); + const TreeItem *parentItem = getItem(parent); - return parentItem->childCount(); + return parentItem ? parentItem->childCount() : 0; } //! [8] @@ -225,7 +227,7 @@ bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int rol bool result = item->setData(index.column(), value); if (result) - emit dataChanged(index, index, {role}); + emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole}); return result; } @@ -236,7 +238,7 @@ bool TreeModel::setHeaderData(int section, Qt::Orientation orientation, if (role != Qt::EditRole || orientation != Qt::Horizontal) return false; - bool result = rootItem->setData(section, value); + const bool result = rootItem->setData(section, value); if (result) emit headerDataChanged(orientation, section, section); @@ -246,8 +248,8 @@ bool TreeModel::setHeaderData(int section, Qt::Orientation orientation, void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent) { - QList parents; - QList indentations; + QVector parents; + QVector indentations; parents << parent; indentations << 0; @@ -261,14 +263,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); + const QStringList columnStrings = lineData.split('\t', QString::SkipEmptyParts); QVector columnData; - for (int column = 0; column < columnStrings.count(); ++column) - columnData << columnStrings[column]; + columnData.reserve(columnStrings.size()); + for (const QString &columnString : columnStrings) + columnData << columnString; if (position > indentations.last()) { // The last child of the current parent is now the new parent @@ -291,7 +294,6 @@ void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent) for (int column = 0; column < columnData.size(); ++column) parent->child(parent->childCount() - 1)->setData(column, columnData[column]); } - ++number; } } diff --git a/examples/widgets/itemviews/editabletreemodel/treemodel.h b/examples/widgets/itemviews/editabletreemodel/treemodel.h index d76d138831..8419f9cb9d 100644 --- a/examples/widgets/itemviews/editabletreemodel/treemodel.h +++ b/examples/widgets/itemviews/editabletreemodel/treemodel.h @@ -64,7 +64,7 @@ class TreeModel : public QAbstractItemModel public: TreeModel(const QStringList &headers, const QString &data, - QObject *parent = 0); + QObject *parent = nullptr); ~TreeModel(); //! [0] //! [1] -- cgit v1.2.3