Added a checkable item class to genus model

This commit is contained in:
Michael Mandl 2018-05-22 08:22:46 +02:00
parent de9518ae43
commit 59ff9712d8
2 changed files with 58 additions and 2 deletions

View file

@ -19,11 +19,21 @@ QVariant GenusModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole)
{
if (index.row() == 0 && m_tiere.find(index.column()) != m_tiere.end())
{
return m_tiere.find(index.column())->second.getText().c_str();
}
return "Hello";
}
if (role == Qt::CheckStateRole)
{
if (index.row() == 0 && m_tiere.find(index.column()) != m_tiere.end())
{
return m_tiere.find(index.column())->second.isChecked() ? Qt::Checked : Qt::Unchecked;
}
return Qt::Unchecked;
}
@ -39,12 +49,21 @@ Qt::ItemFlags GenusModel::flags(const QModelIndex &index) const
bool GenusModel::setData(
const QModelIndex &index, const QVariant &value, int role)
{
if (role != Qt::EditRole)
if (role != Qt::CheckStateRole)
{
return QAbstractTableModel::setData(index, value, role);
}
else
{
if (index.row() == 0 && m_tiere.find(index.column()) != m_tiere.end())
{
m_tiere.find(index.column())->second.setState(value.toBool());
return true;
return true;
}
}
return false;
}
QVariant GenusModel::headerData(

View file

@ -1,5 +1,35 @@
#pragma once
#include <string>
class CheckableItem
{
private:
bool m_checked = false;
std::string m_text;
public:
CheckableItem(const std::string &text)
: m_text(text)
{
}
std::string getText() const
{
return m_text;
}
bool isChecked() const
{
return m_checked;
}
void setState(bool checked)
{
m_checked = checked;
}
};
#include <QAbstractTableModel>
#include <QJsonObject>
@ -7,6 +37,13 @@ class GenusModel : public QAbstractTableModel
{
Q_OBJECT
private:
std::map<size_t, CheckableItem> m_tiere =
{
{0, {"null"}},
{1, {"eins"}}
};
public:
GenusModel(QObject *parent);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;