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
This commit is contained in:
2025-12-02 15:59:34 +01:00
parent 301d143b37
commit 6a3725bde7
5 changed files with 76 additions and 1 deletions

View File

@ -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})

View File

@ -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<QAbstractItemModel> GenericCore::getModel() const { return m_mainModel; }
void GenericCore::setupModels() { m_mainModel = make_shared<TableModel>(this); }
QString GenericCore::getMaintenanceToolFilePath() const {
QString applicationDirPath = QCoreApplication::applicationDirPath();

View File

@ -3,8 +3,11 @@
#include <QObject>
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<QAbstractItemModel> getModel() const;
signals:
void displayStatusMessage(QString message);
private:
std::shared_ptr<TableModel> m_mainModel;
void setupModels();
QString getMaintenanceToolFilePath() const;
};

38
model/tablemodel.cpp Normal file
View File

@ -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();
}

18
model/tablemodel.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef TABLEMODEL_H
#define TABLEMODEL_H
#include <QAbstractTableModel>
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