From e1bc779791614b57e3a540aad5d16ef924e72078 Mon Sep 17 00:00:00 2001 From: Bent Witthold Date: Mon, 19 Jan 2026 18:43:13 +0100 Subject: [PATCH] Added QAbstractItemModelTester to main and proxy model. And fixing errors in functions flags(...), rowCount(...) and columnCount(...). --- CMakeLists.txt | 3 +++ genericcore.cpp | 15 ++++++++++++++- genericcore.h | 3 +++ model/tablemodel.cpp | 11 +++++++++-- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 71c0612..a275e08 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(QT NAMES Qt6 REQUIRED COMPONENTS Core LinguistTools) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core LinguistTools Gui) +find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Test) configure_file(CoreConfig.h.in CoreConfig.h) @@ -36,6 +37,8 @@ add_library(${TARGET_APP} STATIC model/generalsortfiltermodel.h model/generalsortfiltermodel.cpp ) + +target_link_libraries(GenericCore PRIVATE Qt${QT_VERSION_MAJOR}::Test) include_directories(${CMAKE_CURRENT_BINARY_DIR}) target_link_libraries(${TARGET_APP} PRIVATE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Gui) diff --git a/genericcore.cpp b/genericcore.cpp index 9a29e04..38046a3 100644 --- a/genericcore.cpp +++ b/genericcore.cpp @@ -1,5 +1,6 @@ #include "genericcore.h" +#include #include #include #include @@ -135,7 +136,19 @@ void GenericCore::setupModels() { m_mainModel = make_shared(m_modelUndoStack, this); m_sortFilterModel = make_shared(m_mainModel); - // TODO add QAbstractItemModelTester + /// QAbstractItemModelTester +#ifdef QT_DEBUG + m_mainModelTester = make_unique( + m_mainModel.get(), QAbstractItemModelTester::FailureReportingMode::Fatal); + m_proxyModelTester = make_unique( + m_sortFilterModel.get(), QAbstractItemModelTester::FailureReportingMode::Fatal); +#else + m_mainModelTester = make_unique( + m_mainModel.get(), QAbstractItemModelTester::FailureReportingMode::Warning); + m_modelTester = make_unique( + m_sortFilterModel.get(), QAbstractItemModelTester::FailureReportingMode::Warning); +#endif + initModelData(); } diff --git a/genericcore.h b/genericcore.h index f4a78b0..a7df414 100644 --- a/genericcore.h +++ b/genericcore.h @@ -5,6 +5,7 @@ class QUndoStack; class QAbstractItemModel; +class QAbstractItemModelTester; class QString; class TableModel; @@ -38,6 +39,8 @@ class GenericCore : public QObject { QUndoStack* m_modelUndoStack; std::shared_ptr m_mainModel; std::shared_ptr m_sortFilterModel; + std::unique_ptr m_mainModelTester; + std::unique_ptr m_proxyModelTester; void setupModels(); void initModelData(); diff --git a/model/tablemodel.cpp b/model/tablemodel.cpp index 8519e52..08941b6 100644 --- a/model/tablemodel.cpp +++ b/model/tablemodel.cpp @@ -39,18 +39,25 @@ TableModel::TableModel(QUndoStack* undoStack, QObject* parent) , m_undoStack(undoStack) {} Qt::ItemFlags TableModel::flags(const QModelIndex& index) const { + if (!index.isValid()) { + return QAbstractTableModel::flags(index); + } return Qt::ItemIsEditable | QAbstractTableModel::flags(index); } QHash TableModel::roleNames() const { return ROLE_NAMES; } int TableModel::rowCount(const QModelIndex& parent) const { - Q_UNUSED(parent); + if (parent.isValid()) { + return 0; // no children + } return m_items.size(); } int TableModel::columnCount(const QModelIndex& parent) const { - Q_UNUSED(parent); + if (parent.isValid()) { + return 0; // no children + } return ROLE_NAMES.size(); }