implemented write support

This commit is contained in:
Michael Mandl 2018-05-17 19:42:35 +02:00
parent c1030accea
commit e50a77bb40
4 changed files with 70 additions and 49 deletions

View file

@ -40,12 +40,71 @@ QVariant MetaDataModel::data(const QModelIndex &index, int role) const
return QVariant();
}
Qt::ItemFlags MetaDataModel::flags(const QModelIndex &index) const
{
return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
}
bool MetaDataModel::setData(
const QModelIndex &index, const QVariant &value, int role)
{
if (role != Qt::EditRole)
{
return QAbstractTableModel::setData(index, value, role);
}
bool valueChanged = false;
switch (index.column())
{
case 0:
if (value.toString() != m_participant)
{
m_participant = value.toString();
valueChanged = true;
}
break;
case 1:
if (value.toString() != m_instructor)
{
m_instructor = value.toString();
valueChanged = true;
}
break;
case 2:
if (value.toDate() != m_dateOfTest)
{
m_dateOfTest = value.toDate();
valueChanged = true;
}
break;
case 3:
if (value.toDate() != m_dateOfBirth)
{
m_dateOfBirth = value.toDate();
valueChanged = true;
}
break;
case 4:
if (value.toString() != m_remarks)
{
m_remarks = value.toString();
valueChanged = true;
}
break;
default:
break;
}
return valueChanged;
}
void MetaDataModel::write(QJsonObject &json) const
{
json["participant name"] = m_participant;
json["instructor name"] = m_instructor;
json["date of birth"] = m_dateOfBirth.toString(Qt::TextDate);
json["date of test"] = m_dateOfTest.toString(Qt::TextDate);
json["date of birth"] = m_dateOfBirth.toString(Qt::ISODate);
json["date of test"] = m_dateOfTest.toString(Qt::ISODate);
json["remarks"] = m_remarks;
}
@ -66,13 +125,13 @@ void MetaDataModel::read(const QJsonObject &json)
const auto &dateOfBirth = json["date of birth"];
if (dateOfBirth.isString())
{
m_dateOfBirth.fromString(dateOfBirth.toString());
m_dateOfBirth = QDate::fromString(dateOfBirth.toString(), Qt::ISODate);
}
const auto &dateOfTest = json["date of test"];
if (dateOfTest.isString())
{
m_dateOfTest.fromString(dateOfTest.toString());
m_dateOfTest = QDate::fromString(dateOfTest.toString(), Qt::ISODate);
}
const auto &remarks = json["remarks"];

View file

@ -22,6 +22,9 @@ public:
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(
const QModelIndex &index, int role = Qt::DisplayRole) const override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
bool setData(const QModelIndex &index, const QVariant &value,
int role = Qt::EditRole) override;
void write(QJsonObject &json) const;
void read(const QJsonObject &json);

View file

@ -28,10 +28,8 @@ MainWindow::MainWindow(QWidget *parent)
m_widgetMapper->addMapping(ui->lineEdit_2, 1);
m_widgetMapper->addMapping(ui->dateEdit, 2);
m_widgetMapper->addMapping(ui->dateEdit_2, 3);
m_widgetMapper->addMapping(ui->textEdit, 4);
m_widgetMapper->addMapping(ui->plainTextEdit, 4);
m_widgetMapper->toFirst();
connect(ui->calendarWidget, SIGNAL(selectionChanged()), m_widgetMapper, SLOT(submit()));
}
MainWindow::~MainWindow()

View file

@ -42,45 +42,6 @@
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QCalendarWidget" name="calendarWidget_2">
<property name="selectedDate">
<date>
<year>2018</year>
<month>9</month>
<day>10</day>
</date>
</property>
<property name="firstDayOfWeek">
<enum>Qt::Monday</enum>
</property>
<property name="horizontalHeaderFormat">
<enum>QCalendarWidget::ShortDayNames</enum>
</property>
<property name="verticalHeaderFormat">
<enum>QCalendarWidget::NoVerticalHeader</enum>
</property>
</widget>
</item>
<item row="2" column="3">
<widget class="QCalendarWidget" name="calendarWidget">
<property name="firstDayOfWeek">
<enum>Qt::Monday</enum>
</property>
<property name="gridVisible">
<bool>false</bool>
</property>
<property name="verticalHeaderFormat">
<enum>QCalendarWidget::NoVerticalHeader</enum>
</property>
<property name="navigationBarVisible">
<bool>true</bool>
</property>
<property name="dateEditEnabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="3">
<widget class="QLineEdit" name="lineEdit_2"/>
</item>
@ -91,10 +52,10 @@
</property>
</widget>
</item>
<item row="3" column="1">
<item row="2" column="1">
<widget class="QDateEdit" name="dateEdit"/>
</item>
<item row="3" column="3">
<item row="2" column="3">
<widget class="QDateEdit" name="dateEdit_2"/>
</item>
</layout>
@ -108,7 +69,7 @@
</widget>
</item>
<item>
<widget class="QTextEdit" name="textEdit"/>
<widget class="QPlainTextEdit" name="plainTextEdit"/>
</item>
</layout>
</widget>