Added model for meta-data, connected to widgets via mapper

This commit is contained in:
Michael Mandl 2018-05-15 14:24:15 +02:00
parent dd28d94043
commit 8550316388
5 changed files with 84 additions and 0 deletions

View file

@ -13,6 +13,7 @@ qt5_wrap_ui(UI_HEADERS
add_executable(${PROJECT_NAME}
LogoTest.cpp
testdata.cpp
MetaDataModel.cpp
mainwindow.cpp
${UI_HEADERS}
)

41
source/MetaDataModel.cpp Normal file
View file

@ -0,0 +1,41 @@
#include "MetaDataModel.h"
#include <QDebug>
MetaDataModel::MetaDataModel(QObject *parent) : QAbstractTableModel(parent)
{
}
int MetaDataModel::rowCount(const QModelIndex &parent) const
{
return 1;
}
int MetaDataModel::columnCount(const QModelIndex &parent) const
{
return 5;
}
QVariant MetaDataModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole || role == Qt::EditRole)
{
switch (index.column())
{
case 0:
return m_participant;
case 1:
return m_instructor;
case 2:
return m_dateOfBirth;
case 3:
return m_dateOfTest;
case 4:
return m_remarks;
default:
return QVariant();
}
}
return QVariant();
}

24
source/MetaDataModel.h Normal file
View file

@ -0,0 +1,24 @@
#pragma once
#include <QAbstractTableModel>
#include <QString>
#include <QDate>
class MetaDataModel : public QAbstractTableModel
{
Q_OBJECT
private:
QString m_participant;
QString m_instructor;
QDate m_dateOfBirth;
QDate m_dateOfTest;
QString m_remarks;
public:
MetaDataModel(QObject *parent);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(
const QModelIndex &index, int role = Qt::DisplayRole) const override;
};

View file

@ -2,10 +2,12 @@
#include "ui_mainwindow.h"
#include "testdata.h"
#include "MetaDataModel.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QFile>
#include <QFileDialog>
#include <QDataWidgetMapper>
MainWindow::MainWindow(QWidget *parent)
@ -15,6 +17,19 @@ MainWindow::MainWindow(QWidget *parent)
ui->actionSave_as->setIcon(QIcon::fromTheme("document-save-as", QIcon("/usr/share/gtk-doc/html/gtk2/document-save-as.png")));
connect(ui->actionSave_as, SIGNAL(triggered()), this, SLOT(saveAs()));
MetaDataModel *metaDataModel = new MetaDataModel(this);
QDataWidgetMapper *mapper = new QDataWidgetMapper(this);
mapper->setModel(metaDataModel);
mapper->addMapping(ui->lineEdit, 0);
mapper->addMapping(ui->lineEdit_2, 1);
mapper->addMapping(ui->calendarWidget, 2);
mapper->addMapping(ui->calendarWidget_2, 3);
mapper->addMapping(ui->textEdit, 4);
mapper->toFirst();
//ui->tableView->setModel(metaDataModel);
}
MainWindow::~MainWindow()

View file

@ -104,6 +104,9 @@
<item>
<widget class="QTextEdit" name="textEdit"/>
</item>
<item>
<widget class="QTableView" name="tableView"/>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">