From 169d8f9f1e27cba9ded7195811708d2b4c307bc5 Mon Sep 17 00:00:00 2001 From: Bent Witthold Date: Mon, 8 Dec 2025 14:50:13 +0100 Subject: [PATCH] Items can be deleted from the model. --- model/tablemodel.cpp | 19 +++++++++++++++++++ model/tablemodel.h | 3 +++ 2 files changed, 22 insertions(+) diff --git a/model/tablemodel.cpp b/model/tablemodel.cpp index dc5fab7..551e125 100644 --- a/model/tablemodel.cpp +++ b/model/tablemodel.cpp @@ -88,6 +88,25 @@ bool TableModel::setData(const QModelIndex& index, const QVariant& value, int ro // bool TableModel::setItemData(const QModelIndex& index, const QMap& roles) {} +bool TableModel::removeRows(int position, int rows, const QModelIndex& parentIndex) { + if (parentIndex != QModelIndex()) { + qWarning() << "Removing of child rows is not supported yet!"; + return false; + } + + const int endPosition = position + rows; + if (position < 0 || endPosition >= m_items.size()) { + qWarning() << "Trying to remove rows is out of bounds!"; + return false; + } + + beginRemoveRows(QModelIndex(), position, position + rows - 1); + m_items.remove(position, rows); + endRemoveRows(); + + return true; +} + void TableModel::appendItems(const QByteArray& jsonDoc) { insertItems(-1, jsonDoc, QModelIndex()); } void TableModel::insertItems(int startPosition, diff --git a/model/tablemodel.h b/model/tablemodel.h index 63ca073..b105975 100644 --- a/model/tablemodel.h +++ b/model/tablemodel.h @@ -29,6 +29,9 @@ class TableModel : public QAbstractTableModel { // bool setItemData(const QModelIndex& index, const QMap& roles) override; public slots: + // bool insertRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex()) + // override; + bool removeRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex()) override; void appendItems(const QByteArray& jsonDoc); void insertItems(int startPosition, const QByteArray& jsonDoc, const QModelIndex& parentIndex);