From 6a3725bde77dbef87fe1061d312875d199f30f08 Mon Sep 17 00:00:00 2001 From: Bent Witthold Date: Tue, 2 Dec 2025 15:59:34 +0100 Subject: [PATCH] Added a simple read-only TableModel and made it accessible via GenericCore. Basically like in this tutorial: https://doc.qt.io/qt-6/modelview.html#2-1-a-read-only-table --- CMakeLists.txt | 1 + genericcore.cpp | 11 ++++++++++- genericcore.h | 9 +++++++++ model/tablemodel.cpp | 38 ++++++++++++++++++++++++++++++++++++++ model/tablemodel.h | 18 ++++++++++++++++++ 5 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 model/tablemodel.cpp create mode 100644 model/tablemodel.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ef633b..008325c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ add_library(${TARGET_APP} STATIC ${TS_FILES} constants.h data/settingshandler.h data/settingshandler.cpp + model/tablemodel.h model/tablemodel.cpp ) include_directories(${CMAKE_CURRENT_BINARY_DIR}) diff --git a/genericcore.cpp b/genericcore.cpp index 7ec4636..57fb303 100644 --- a/genericcore.cpp +++ b/genericcore.cpp @@ -10,10 +10,15 @@ #include "../../ApplicationConfig.h" #include "CoreConfig.h" #include "constants.h" +#include "model/tablemodel.h" using namespace std; -GenericCore::GenericCore() { qDebug() << "Creating core..."; } +GenericCore::GenericCore() { + qDebug() << "Creating core..."; + + setupModels(); +} GenericCore::~GenericCore() { qDebug() << "Destroying core..."; } @@ -65,6 +70,10 @@ void GenericCore::triggerApplicationUpdate() { QProcess::startDetached(toolFilePath, args); } +std::shared_ptr GenericCore::getModel() const { return m_mainModel; } + +void GenericCore::setupModels() { m_mainModel = make_shared(this); } + QString GenericCore::getMaintenanceToolFilePath() const { QString applicationDirPath = QCoreApplication::applicationDirPath(); diff --git a/genericcore.h b/genericcore.h index bef0ac0..e017fb3 100644 --- a/genericcore.h +++ b/genericcore.h @@ -3,8 +3,11 @@ #include +class QAbstractItemModel; class QString; +class TableModel; + class GenericCore : public QObject { Q_OBJECT @@ -18,10 +21,16 @@ class GenericCore : public QObject { bool isApplicationUpdateAvailable(); void triggerApplicationUpdate(); + std::shared_ptr getModel() const; + signals: void displayStatusMessage(QString message); private: + std::shared_ptr m_mainModel; + + void setupModels(); + QString getMaintenanceToolFilePath() const; }; diff --git a/model/tablemodel.cpp b/model/tablemodel.cpp new file mode 100644 index 0000000..0681c5f --- /dev/null +++ b/model/tablemodel.cpp @@ -0,0 +1,38 @@ +#include "tablemodel.h" + +TableModel::TableModel(QObject* parent) + : QAbstractTableModel{parent} {} + +int TableModel::rowCount(const QModelIndex& parent) const { return 5; } + +int TableModel::columnCount(const QModelIndex& parent) const { return 5; } + +QVariant TableModel::data(const QModelIndex& index, int role) const { + const int row = index.row(); + const int column = index.column(); + + if (!index.isValid()) { + return QVariant(); + } + if (row >= 5 || column >= 5) { + return QVariant(); + } + + switch (role) { + case Qt::DisplayRole: + return QString("Data %1/%2").arg(row).arg(column); + } + + return QVariant(); +} + +QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const { + if (role == Qt::DisplayRole) { + if (orientation == Qt::Horizontal) { + return QString("Section %1").arg(section); + } else { + return QString("%1").arg(section); + } + } + return QVariant(); +} diff --git a/model/tablemodel.h b/model/tablemodel.h new file mode 100644 index 0000000..64523f0 --- /dev/null +++ b/model/tablemodel.h @@ -0,0 +1,18 @@ +#ifndef TABLEMODEL_H +#define TABLEMODEL_H + +#include + +class TableModel : public QAbstractTableModel { + public: + explicit TableModel(QObject* parent = nullptr); + + /// QAbstractItemModel interface + public: + int rowCount(const QModelIndex& parent) const override; + int columnCount(const QModelIndex& parent) const override; + QVariant data(const QModelIndex& index, int role) const override; + QVariant headerData(int section, Qt::Orientation orientation, int role) const override; +}; + +#endif // TABLEMODEL_H